As a Git newbie doing things right has always been a difficult thing to do. Here are some commands to remove and local and remote commits in Git.
Prerequisites
- Git
Solution
To remove any local commits:
git reset --hard HEAD
Note(s): This is kinda equivalent of the famous Linux destructive command rm -rf
.
If you want to undo the last 2 commits, you’ll have something like:
git reset --hard HEAD~2
However, if you want to rollback until a specific commit, first find the commit ID and run:
git reset --hard <commit-SHA1>
List all commits in a Git repository.
Local commits only so, no biggie. But, what if the commit is pushed already to remote. Best thing to do is to use the git revert
command. For instance:
git pull
git revert --no-commit <commit-SHA1>
git commit -m "revert commit-SHA1"
git push origin main
Conclusion
To find more neat Git commands and hacks, browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.