Undo commit before push in Git
Today we will show you how to undo commit before push in Git remote repository. We’ll provide you the three different ways to undo commit which has not been pushed.
In this article, we will create an example to show you a demo. Let’s assume that we have created a project repository and create a new file called
file1.txt. Also write some text in this file.
git status command to see the output.
Now let’s add it and commit these changes to the repository by running the following command.
git add file1.txt
git commit -m “first commit”
Check out the following screen after executing the above commands.
git status command to check the status before push to the remote repository.
So we will now undo the last commit using three different methods. You can choose anyone according to your need.
Ways to undo commit before push in Git
1. Undo commit and keep file staged
Let’s say if we want to undo the commit but keep all files staged then we should use the following command.
git reset --soft HEAD~1
Most of the time we should use this command to update the committed message without touching the file.
2. Undo commit and unstage file
If you want to undo the last commit and unstage all files then you can use the below command.
git reset HEAD~1
3. Undo commit and discard changes
In case you want to undo commit and discard all changes or in other words we can say hard reset the index and working tree then use the following command.
git reset --hard HEAD~1
Note: To change the commit message check out the article: How to change the commit message in git.
That’s it for today.
Thank you for reading. Happy Coding..!!