How to Undo git add and git Commit Before Push

Undo Changes in Git

by Dharmesh Patel

Sometimes, you may find yourself in a situation where you've added and committed changes in Git, but you realise you need to undo those actions before pushing them to the remote repository. This guide walks you through the steps to unstage files and undo commits in Git.

Here's how you can undo these actions:

1. Undo git add (Unstage files)

If you’ve added files using git add but haven’t committed yet, you can unstage them:

To unstage all files:

git reset

To unstage specific files:

git reset <file>

2. Undo git commit (Uncommit changes)

If you’ve already committed the changes but haven’t pushed, you can undo the commit. The changes will remain in your working directory, so you can modify them or add more changes.

To undo the last commit but keep the changes in your working directory:

git reset --soft HEAD~1

If you want to undo the commit and also unstage the changes:

git reset HEAD~1

3. Undo commit and discard changes

If you want to undo the commit and discard the changes completely:

git reset --hard HEAD~

Note: This will delete the changes, so use it carefully.

Summary
  • Use git reset HEAD~1 to undo the last commit but keep changes staged.
  • Use git reset --soft HEAD~1 to undo the last commit and keep changes unstaged.
  • Use git reset --hard HEAD~1 to completely remove changes from the last commit.

Consider checking your branch's status with git status after performing these operations to ensure you understand the current state of your files.

Need more development tips?


Stay tuned to our blog.

Implementing Advanced Odoo Workflow Logic with Python