Undo a Git Add before commit

To undo git add before a commit, run  git reset <file> or git reset to unstage all changes.

// to unstage a specific file 
git reset <file> 

//or to unstage all changes
git reset 

In older versions of Git, the commands were git reset HEAD <file> and git reset HEAD respectively. This was changed in Git 1.8.2

Undo git commit before push

  • Undo commit and unstage all files
git reset HEAD~;

//or 

git reset --mixed HEAD^

This resets the HEAD to the previous commit but removes the changes from the staging area. However, our changes remain in the working directory.

  • Undo commit and keep all files staged

In case we just want to undo the commit and change nothing more, we can use

git reset --soft HEAD~

This will reset the HEAD to the previous commit but keep our changes in the staging area.

This is most often used to make a few changes to your latest commit and/or fix your commit message. Leaves working tree as it was before reset.

  • Undo the commit and completely remove all changes
git reset --hard HEAD~;

This resets the HEAD to the previous commit and discards all changes. Be careful with this command as our local changes will be permanently deleted.

hard resets the index and working tree. Any changes to tracked files in the working tree since the previous commit are discarded

PS:  In case you just want to rewrite the commit message, you could use git –amend instead.

git commit --amend -m "new message"

How do I modify a specific commit?

Use git rebase. For example, to modify commit bbc643cd, run:

$ git rebase --interactive 'bbc643cd^'

Please note the caret ^ at the end of the command, because you need actually to rebase back to the commit before the one you wish to modify.

In the default editor, modify pick to edit in the line mentioning bbc643cd.

Save the file and exit. git will interpret and automatically execute the commands in the file. You will find yourself in the previous situation in which you just had created commit bbc643cd.

At this point, bbc643cd is your last commit and you can easily amend it. Make your changes and then commit them with the command:

$ git commit --all --amend --no-edit

Use git rebase. For example, to modify commit bbc643cd, run:

$ git rebase --interactive 'bbc643cd^'

Please note the caret ^ at the end of the command, because you need actually to rebase back to the commit before the one you wish to modify.

In the default editor, modify pick to edit in the line mentioning bbc643cd.

Save the file and exit. git will interpret and automatically execute the commands in the file. You will find yourself in the previous situation in which you just had created commit bbc643cd.

At this point, bbc643cd is your last commit and you can easily amend it. Make your changes and then commit them with the command:

$ git commit --all --amend --no-edit

After that, return back to the previous HEAD commit using:

$ git rebase --continue

By Shabazz

Software Engineer, MCSD, Web developer & Angular specialist

Leave a Reply

Your email address will not be published. Required fields are marked *