Introduction
It is very common to make mistakes while working on a project. Sometimes, you may have made a commit with the wrong changes. In such cases, you may want to undo the commit and make a new commit with the correct changes. In this post, we will see how to undo a commit in your local and remote repositories.
Undo a commit in your local repository
The word ‘undo’ here means that:
- Remove the wrong commit from your local repository
- But keep the changes that you made in that commit
This allows you to create a new commit with the correct changes.
To undo a commit in your local repository, you can use the following command:
git reset --soft HEAD~1
Explanation of the command:
git reset
- This command is used to reset the current HEAD to a previous state. In simple words, reset means to undo a commit.--soft
- This option tells git to reset the current branch head to the specified commit. But it does not change the working directory or the staging area. In simple words, it does not remove the changes that you made since that commit.HEAD~1
- This option tells git to reset the current branch head to the previous commit. Here,HEAD
is a pointer to the current commit.~1
means one commit before the current commit. So,HEAD~1
means the previous commit. In simple words, it tells Git to go back one step(commit) back in the commit history.
If you want to undo more than one commit, you can change the ~1
to ~2
or ~3
and so on.
If you want to discard the changes i.e. remove the changes that you made since the commit, you can use the following command:
git reset --hard HEAD~1
Here --hard
option tells git to reset the current branch head to the specified commit. It also changes the working directory and the staging area. In simple words, it removes the changes that you made since that commit.
Push the changes to remote repository
After undoing the commit in your local repository, you may need to push the changes to your remote repository. To do that, you can use the following command:
git push origin <branch-name> --force
Here, we are using the --force
option to force push the changes to the remote repository. The --force
option is required since you have changed the commit history.
Without the --force
option, you will get an error similar to the following:
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to <remote>
More on force push
When you push the changes to the remote repository, git checks if the commit history of the remote repository is the same as the commit history of your local repository. If the commit history is the same, git will allow you to push the changes. But if the commit history is different, git will not allow you to push the changes to the remote repository.
But when you use the --force
option, git will ignore the commit history and push the changes to the remote repository.
In simple words, it will overwrite the commit history of the remote repository with the commit history of your local repository. So, you should use the --force
option only when you are sure that you want to overwrite the commit history of the remote repository.
Conclusion
In this post, you learnt how to undo a commit in your local repository and push the changes to your remote repository. I hope you found this post useful.