How to Undo Recent Commits in Git
One of the greatest things about Git is that you can undo almost anything. Let’s have a look at how we can undo recent commits in Git.
How to Undo Commits with git reset Command
- git reset --soft HEAD~x (or git reset --soft commit hash).
git reset --soft HEAD~x
Note: Put the corresponding number instead of ~x. For example, if you specify ~3, the command will affect the third commit from the bottom. If you do not determine ~x, git reset --soft HEAD command will change your latest commit.
The git reset --soft HEAD command rewinds your current HEAD to the specified revision. When you use this command you just undo the last commit, but the changes you have made will be stored in your working tree, and on your index, so git commit command will create a commit with the same changes as the commit you "undo" before.git reset --hard HEAD~x (or git reset --hard commit hash).
Git reset --hard HEAD~x
How to Undo Commits with git revert Command
git revert HEAD~x (git reset --hard commit hash) reverts the changes specified by the last commit in HEAD and create a new commit with the reverted changes.
Git revert HEAD ~x
For example, if your history is as follows:
git log --oneline 872fa7e try something crazy a1e8fb5 make some important changes to hello.txt
Although 872fa7e commit still exists in history, it is technically “undone”. The new e2f9a78 commit an inverse of the changes in 872fa7e.
It's the best method for undoing commits when working with public shared repositories.
How to Undo Commits with git amend Command
To make changes, stage them, and commit again to run the following command:
git commit --amend
This command will open your $EDITOR and let you change the message. The new changes will be added to the amended commit.
If you want to make changes in the commit message, you "commit again" - without any staged changes but with the correct message:
git commit --amend -m "This is the correct message"
If there is anything else to add to that last commit, stage changes as usual and then commit again:
git add some/changed/file.ext git commit --amend -m "commit message"
git commit --amend git push origin master --force
The --force feature forces pushing your commit after making changes to update the remote history.
Note: Be careful with running this command, as this will very likely prevent others from syncing with the repository if they already pulled a copy. Check with them first.
How to Undo Commits with Git git checkout Command
Checking out the previous commit withgit checkout command you put the repository in a state before your last commit (the one you want to undo) happened. The repo will be set in a "detached HEAD" state, and all your new commits will be orphaned when you change branches back to an established branch. Orphaned commits are being deleted automatically by Git garbage collector.
From the detached HEAD state, you can run
git checkout -b new_branch_without_my_last_commit
This will create a new branch named new_branch_without_my_last_commit and switch to that state. The repo will be on a new history timeline where your last commit doesn’t exist.
Note: This method is inappropriate in case if the previous branch that you need is your master branch.