Shoot! I don’t know how to revert Git commit. While working on one of my projects, I committed some files I shouldn’t have. Luckily, I learned about it before merging to the master (which contains auto-deployment to the live environment).
For those who just would like to copy/paste the code.
Revert a Git commit (can be any commit):
git revert {commit_id}
Revert the last Git commit:
git reset --soft HEAD~1
In this case, we’ll revert the whole commit. Otherwise, you’ll have to go through all the changes manually. It doesn’t matter if it’s the last commit or a commit you did earlier.
By reverting a commit, you’ll create a new commit. The new commit undoes all the changes that were made within the commit that you’re reverting.
git revert {commit_id}
Before we can revert the commit, we’ll need to know its ID. For this example, I’ll use GitHub to show where it is.
1. Go to the home screen of your repository
2. Use the dropdown to change from the master (or main) branch to the branch where you have made the commit you would like to revert
3. Now there’s a block that says: This branch is X commit ahead of master. Click on the X commit ahead.
4. Click on the copy icon ‘Copy the full SHA’, which is the commit ID
5. Now paste it into the line of code shared at the beginning of the article
git revert 5cfc208de9dd834e4625c42d29639840c910f969
Paste the line in the terminal, and it should all work fine.
To undo your last commit on the branch you’re on, there is an easier code. You don’t need the commit ID as you did in the steps before.
git reset --soft HEAD~1
That’s all! Additional resources can be found on Stack Overflow and in this GitHub repository.
Working remotely from Groningen, the Netherlands. Get in touch and let’s schedule a meeting, no strings attached.
Get in touch