If you’ve been making changes to your Git repo and made a mistake, it’s always nice to know you have a way to rollback your commits to get your workflow back on track. In this guide, we’ll look at the git revert command for local and remote commits to a repository.
It’s important to understand the difference between git reset and git revert before diving in.
Advantages of git revert Over git reset
Reverting has two important advantages over resetting:
- Reverting does not change the project history, which makes it a safer operation for commits that have already been pushed to a shared repository.
- Reverting is able to target an individual commit at an arbitrary point in the history, while if you use git reset, it can only work backward from the current commit.
Using the git revert Command to Rollback a Commit
The command syntax for git revert is:
git revert
Where the
You can use git reflog to see the local Git history with its shorthand commit IDs, or use git log to find the longer-form SHA-1 commit IDs for the whole committed Git history.
$ git reflog
bc32b0d HEAD@{0}: commit: Added contact-us.htm
ead83d9 HEAD@{1}: commit: Added blog.htm
$ git log
commit bc32b0d8a583ccc3b0bcc9fbdfe3c7bbd1b4f0d1
Author: User
Date: Tue Feb 10 12:38:32 2015 -0500
Added contact-us.htm
commit ead83d9f1d800de241580070d22a17f769ea7866
Author: User
Date: Tue Feb 10 12:38:00 2015 -0500
Added blog.htm
The git reflog command records a chronological history of everything you have done in your local repository. Its full output might look like this:
bc32b0d HEAD@{1}: commit: Added contact-us.htm
ead83d9 HEAD@{2}: commit: Added blog.htm
1a890e7 HEAD@{3}: commit: Added about-us.htm
bf58ea1 HEAD@{4}: commit: Added index.htm
5c66257 HEAD@{5}: commit (initial): Added README.md
Case 1: Reverting a Single, Local Git Commit
Now let’s say since you just added a contact-us.htm file to your project, you’ve realized you don’t really need the about-us.htm file anymore.
You can revert to the time when you made that commit and keep all changes after that by doing the following:
git revert 1a890e7
Git will prompt you with a new note for this revert commit, giving you a default of:
Revert "Added about-us.htm"
This reverts commit 1a890e7980283e348cde0444cabe709f6342a851.
Go ahead and save that note, or create your own to complete the revert:
[master 2bbaed3] Revert "Added about-us.htm"
1 file changed, 1 deletion(-)
delete mode 100644 about-us.htm
This will revert a specific, local commit. Newer commits and the Git history are preserved.
$ git reflog
2bbaed3 HEAD@{0}: revert: Revert "Added about-us.htm"
bc32b0d HEAD@{1}: commit: Added contact-us.htm
ead83d9 HEAD@{2}: commit: Added blog.htm
1a890e7 HEAD@{3}: commit: Added about-us.htm
bf58ea1 HEAD@{4}: commit: Added index.htm
5c66257 HEAD@{5}: commit (initial): Added README.md
$ ls
blog.htm contact-us.htm index.htm README.md
Case 2: Reverting a Range of Local Git Commits
To revert all the local actions from “1a890e7” up to “HEAD,” use the following:
git revert 1a890e7..HEAD
Case 3: Reverting a Git Commit That Was Pushed
After you check out the remote repository, you can first use git revert and then push as usual:
git revert 1a890e7980283e348cde0444cabe709f6342a851
git push origin
Case 4: Reverting a Range of Git Commits That Were Pushed
We can also undo a set of pushed commits:
git revert 1a890e7980283e348cde0444cabe709f6342a851..ead83d9f1d800de241580070d22a17f769ea7866
git push origin
Just Need to Undo A Quick Local Commit? Use git reset
Sometimes if you just make a quick, local mistake you might not want to use git revert, and instead you might just want to use git reset.
Either way, you should hopefully now understand how to fix a bad commit that you accidentally made with Git. The process might seem complicated and scary at first, but it’s actually pretty simple to understand once you’ve gone through the process a few times yourself.
HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.
Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.