Have you ever made a mistake in your code and committed it to your local repository? Or have you ever committed your changes to the wrong branch? If so, you might want to know how to undo your last commit and restore your project to its previous state.
In this article, we will show you how to use some git commands to undo your last commit and keep or discard the changes you made. We will also explain the difference between reverting, resetting, and cleaning your git repository.
Explore My Other Channel for More Cool and Valuable Insights
π Youtube Learn Tech Tipsπ Tiktok
π Facebook:Reverting a commit
Reverting a commit means creating a new commit that undoes the changes of a previous commit. This way, you can preserve the history of your project and avoid rewriting it. To revert a commit, you can use the git revert command followed by the hash of the commit you want to undo. For example:
git revert a8172f36
This will create a new commit with a message like "Revert 'some commit message'". If you have already pushed your commit publicly, reverting is the safest option to undo it.
Resetting a commit
Resetting a commit means moving the current branch pointer to a different commit. This way, you can erase the history of your project and rewrite it. To reset a commit, you can use the git reset command followed by the hash or the relative position of the commit you want to go back to. For example:
git reset HEAD~1
This will move the current branch pointer to the previous commit and unstage any changes you made. By default, git reset uses the --mixed option, which means that it will keep the changes in your working directory. However, you can also use other options, such as:
• --soft: This will keep the changes in your staging area, ready to be committed again.
• --hard: This will discard any changes in your staging area and working directory, restoring them to the state of the previous commit.
git reset HEAD --hard
If you have not pushed your commit publicly yet, resetting is a quick way to undo it.
Cleaning untracked files
Sometimes, you might want to remove untracked files from your working directory. These are files that are not part of your git repository and are usually ignored by git. To clean untracked files, you can use the git clean command followed by some options. For example:
git clean -fd
This will delete any untracked files and directories from your working directory. However, be careful with this command, as it can also delete ignored files that you might want to keep. To avoid this, you can use other options, such as:
• -n: This will show what files would be deleted without actually deleting them.
• -i: This will prompt you before deleting each file.
• -x: This will delete both untracked and ignored files.
Cleaning untracked files can help you keep your working directory tidy and avoid cluttering it with unnecessary files.
Conclusion
In this article, we learned how to undo our last commit and keep or discard the changes we made. We also learned how to revert, reset, and clean our git repository. We hope this article was helpful for you and that you learned something new