Course lesson

Undo a Commit that has Already Been Pushed

Once a commit is pushed, you do NOT want to use git reset to undo it - because reset will rewrite the history tree, and anyone who has already pulled that branch will have a bad tree.

Duration
2 min
Access
Free
Transcript
Retained from source evidence

Once a commit is pushed, you do NOT want to use git reset to undo it - because reset will rewrite the history tree, and anyone who has already pulled that branch will have a bad tree.

Instead, we'll use git revert to make a "revert commit" (like a merge commit), which will "undo" a specific commit. So the syntax is:

git revert [HASH-TO-UNDO]

If we just made a push to Github, we can use git log --oneline to see our local branch is up to date with our origin. We are presented with a problem when we try to undo a commit that's already been pushed. We have to be careful because if we undo a commit already pushed, we would affect those who pulled the changes, effectively rewriting history.

We are going to use git revert {HASH} with the hash being the commit we want to revert. After doing so, it'll want you to make a revert commit. It's similar to a merge commit because it adds another commit to the tree, plus we have to give it a message. After entering the message, running git log --oneline we find that it successfully reverted the last commit.

The important thing is that when using git revert, the history is still there. You can go back to an earlier commit to revive previous work/code. In addition, anyone who pulled the origin/master branch during the time it took to revert will have a clean history tree because we used git revert {HASH} instead of git reset --hard {HASH}.

✏️ Edit on GitHub