How to use Git Revert

24    Asked by freema_7225 in Devops , Asked on Mar 18, 2025

git revert simply creates a new commit that is the opposite of an existing commit.

Answered by Owen Welch

git revert is used to undo a commit without losing changes or rewriting history. Instead of removing a commit, it creates a new commit that reverses the effects of the specified commit. This makes it a safer option, especially in shared repositories.

Why Use git revert?

  • Keeps commit history intact (unlike git reset).
  • Safe for teams working on the same branch.
  • Can be used to revert specific commits without affecting later ones.

How to Use git revert

Revert a single commit:

  git revert 

  • This creates a new commit that undoes the changes from the specified commit.
  • Git will open a text editor for the commit message; you can modify it or save it as is.

Revert multiple commits:

  git revert HEAD~3..HEAD

This undoes the last 3 commits one by one, creating a new commit for each.

Revert without opening a text editor:

  git revert -n 

This reverts the commit without creating a new commit immediately. You can review changes and commit later.

Push changes after revert:

  git push origin main

  • If you’ve already pushed the original commit, push the revert commit to update the remote branch.
  • git revert is a safe way to undo changes while preserving Git history, making it ideal for collaborative projects.



Your Answer

Interviews

Parent Categories