Reverting or undoing git commits

Undoing git commits is almost always a bad idea. The only time you should contemplate really undoing a commit is if the commit contains information that should never ever be seen in the repository. For example if you accidentally commit a file containing sensitive password information.

In all other cases, let the history stay as history. If the commit needs to be 'undone', create a new commit with the appropriate correction. This approach will keep things simple and stop you or other users of the repository getting into trouble. There are lots of articles available covering using git reset to achieve this sort of thing but we do not want to lose the history, we just want to return the branch to an earlier state as its 'latest' revision:

So, how do you undo a change without rewriting history?

  1. 'git log' - Use the log to find the commits you need to undo.
  2. 'git revert <commit>' Undo the relevant commit (you can specify a range). If the commits involve merges you may need to specify the merge parent (typically 1, eg 'git revert dd33a78 -m 1')

This way the history remains consistent so you can recover from mistakes with correcting the mistakes. You can also relax knowing that only normal 'git pulls' are needed by other repository users to bring their working copies in line.


Comments

There are currently no comments

New Comment

required

required (not published)

optional

Australia: 07 3103 2894

International: +61 410 545 357