Search code examples
gitgit-commit

Undo a git commit


Can you undo a past commit, that has long been merged into your git repository, via a git command? Or do you have to manually undo all the changes made in that commit?


Solution

  • You can always just revert the changes from a single commit by doing:

    git revert <commit-id>
    

    note that this creates a new commit, undoing just those changes

    E.g. git log --oneline

    d806fc9 two
    18cdfa2 bye
    62c332e hello
    c7811ee initial
    

    Say I want to revert changes in commit 18cdfa2:

    git revert 18cdfa2
    

    We now have: git log -1 -p

    commit fb9d6627a71636503fc9a1eb4f725937c783ecee
    Author: Seth <sehe@mint12.(none)>
    Date:   Wed Oct 3 10:32:46 2012 +0200
    
        Revert "bye"
    
        This reverts commit 18cdfa27c964b66b624be1030250757b745d6866.
    
    diff --git a/a b/a
    index 0907563..3b18e51 100644
    --- a/a
    +++ b/a
    @@ -1 +1 @@
    -bye world
    +hello world