Saturday, July 8, 2017

Undo the last commits in Git



I committed the wrong files to Git. I haven't yet pushed the commit to the server.
How can I undo those commits?

Undo a commit and redo

$ git commit -m "Something terribly misguided"     
$ git reset HEAD~                                                     
<< edit files as necessary >>                                    
$ git add ...                                                                
$ git commit -c ORIG_HEAD                                

This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ but leaves your existing changes staged.

Make corrections to working tree files.

Git add anything that you want to include in your new commit.


Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.


 Note,be that as it may, that you don't have to reset to a before submit in the event that you simply committed an error in your confer message. The less demanding alternative is to git reset (to unstage any progressions you've made since) and afterward git confer - revise, which will open your default submit message manager pre-populated with the last confer message.

Beware however that if you have added any new changes to the index, using commit --amend will add them to your previous commit.

Undoing Multiple Commits

The same technique allows you to return to any previous revision:


$ git reset --hard 0ad5a7a6

undoes all commits that came after the one you returned to:


Other links you may like 

1 comment:

  1. It was really useful on behalf of American state. Keep sharing such ideas among the long run equally. This was really what i wont to be probing for, which i'm glad to came here! Thanks for sharing the such knowledge with USA.

    ReplyDelete