Skip to content

Undo commits & changes


Revert a commit (Doesn't change history)

Revert every changes made in a commit with a new 'revert' commit

git revert 7921a1e
git revert 7921a1e index.html

Revert changes made after last commit

Revert every changes made after last commit. 3 modes available :

  • Soft : Keep changes, Unstage changes
  • Mixed(default) : Keep changes, Unstage changes
  • Hard : Delete changes
git reset --mixed
git reset --soft
git reset --hard

Warning

You permanently lose every changes made locally since last commit

Revert to specific commit

Use Case #1 : How to reset last terrible commit (Last good commit is 32e4fc4)) ?
Use Case #2 : How to rewrite history since commit 47e9cf6 ?

1
2
3
git reset 32e4fc4
# Edit files as necessary
git commit -a -m "Fix terrible bug"

Info

Go in the past, make changes to fix bug and then commit to rewrite history.

git reset 47e9cf6
git commit -a -m "History rewritten, changes retains"

Info

We go back to commit 47e9cf6. History is removed but files are not modified locally. Then we commit changes, history between commit and master has been rewritten.

Remove a specific commit

Use Case : You leaked information in a specific commit and you want to completely remove this commit ?

1
2
3
4
5
git rebase -i HEAD~x
# x is the number of commits
# Enter "drop" besides the commit you want to drop

git  push --force-with-lease

Danger

Don't forget to first copy changes you made in the commit if you want to reupoad a fixed version in a new commit after

Amend

Combine few commits

git commit --amend
git commit --amend -m "New commit message"

Warning

Only do this locally on commits we haven't push to remote yet !