As a software developer, you may have come across the situation where you made a mistake in your last commit and want to undo it.
In Git, it is possible to uncommit your last commit and keep your changes in the working tree for further changes.
In this tutorial, we will take a look at how to uncommit your last commit in Git.
Check the Current Status of Your Repository
Before you uncommit your last commit, it is important to check the current status of your repository.
You can do this by running the following command in the terminal:
$ git status
This command will give you an overview of the current state of your repository, including the branch you are on, the files that have been changed, and whether they have been staged or not.
Reset the Head to the Previous Commit
To uncommit your last commit, you need to reset the head of your repository to the previous commit.
You can do this by running the following command in the terminal:
$ git reset --soft HEAD^
The HEAD^ argument tells Git to move the head to the previous commit.
The --soft option tells Git to reset the head but keep the changes in the working tree.
Modify Your Changes
Now that the head of your repository has been reset to the previous commit, you can modify your changes.
You can use your preferred text editor to make the necessary changes to your files.
Commit Your Changes Again
Once you have made the necessary changes to your files, you need to commit them again.
You can do this by running the following commands in the terminal:
$ git add .
$ git commit -m "Fixed my last commit"
The first command, git add ., stages all the changes you have made in the working tree.
The second command, git commit -m "Fixed my last commit", creates a new commit with the changes.
Make sure to add a meaningful message to the commit to describe the changes you have made.
Conclusion
In this tutorial, we have looked at how to uncommit your last commit in Git.
By resetting the head to the previous commit, modifying your changes, and committing them again, you can undo your last commit and keep your changes in the working tree.
Remember to always check the current status of your repository before making any changes to ensure you are on the right track.




