Git Revert

Divya Srinivasan

 What Does git revert Do?

The git revert command undoes a previous commit by adding a new commit that reverses the changes.
This keeps your history clean and is the safest way to undo changes, especially in shared repositories.

Common git revert Commands

  • git revert HEAD
Revert the latest commit
  • git revert <commit>
Revert a specific commit by its hash
  • git revert HEAD~2
Revert the commit two steps back in history
  • git revert --no-edit
Revert without opening the commit message editor
  • git log --oneline
View a simplified commit history

How to Find the Commit to Revert

Before you can revert a commit, you need to find its hash (the short ID).
Use this command to view your commit history in a concise format:

git log --oneline

Example output:

52418f7 (HEAD -> master) Just a regular update
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from update-readme
836e5bf (origin/update-readme, update-readme) Updated readme
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/user/repo
e7de78f Updated index.html. Resized image
5a04b6f Added a line about focus to README.md
d29d69f Added a line about GitHub to README.md
e0b6038 Merged with hello-world-images
1f1584e Added new image
dfa79db Emergency fix to index.html
0312c55 Added image to Hello World
09f4acd Added new line to index.html
221ec6e First release of Hello World!

Run Git Revert

After you’ve identified the commit to undo, use git revert to create a new commit that cancels out its changes.

Example:

git revert HEAD --no-edit

 Output:

[main e56ba1f] Revert "Just a regular update, definitely no accidents here..."
 Date: Thu Apr 22 10:50:13 2021 +0200
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 img_hello_git.jpg

 Review Changes After Running git revert

After you run git revert, it’s a good idea to check the commit history to confirm everything looks right.

Example:

git log --oneline
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 Merged with hello-world-images after fixing conflicts
1f1584e Added new image
dfa79db Updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

 Best Practices for git revert

  • Prefer git revert over git reset when you want to undo a commit but keep the project history visible.
  • Use git log --oneline to quickly find the commit you want to revert.
  • Use git revert HEAD --no-edit to automatically create a new commit that reverses the last commit without opening the editor.

Troubleshooting Tips

  • Revert failed?
Use git revert --abort to cancel the revert process.
  • Conflict during revert?
Resolve conflicts, then run git revert --continue to finish the process.


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send