Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git basics
  6. Undoing changes
  7. Reverting commits
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Reverting commits

Use the git revert command to undo previous commits. This is the most common method of undoing changes.

The revert command creates a new commit that reverts the changes made by a previous commit. It allows you to undo unwanted changes without removing the commit entirely or modifying the repository's history. It’s a useful tool for managing changes to a Git repository while preserving its history.

While you can delete a prior commit from the history using the commands git reset or git rebase -i, it is generally not recommended because it causes the remote repository to diverge from the local repositories of other members.

Diagram of reverting commits.
Git revert is the safest method of undoing changes.

When to revert commits

Knowing when to undo commits in Git is crucial for maintaining project integrity and collaboration.

One key scenario is when working with public or shared branches. It's advisable to use the git revert command in these situations to avoid rewriting history, which can lead to conflicts among team members. By using git revert, you create a new commit that effectively undoes the changes introduced by a previous commit, thus preserving the existing commit history and ensuring a smoother collaborative workflow.

Another important use case is to undo specific changes that have been committed. This approach is particularly useful when addressing errors, bugs, or unwanted modifications without losing the context provided by the original commit. Each operation generates a new commit that acts as a reverse of the targeted commit, maintaining a clear record of changes made over time.

Ultimately, the goal of using git revert is to preserve the commit history of your project. This history serves as a comprehensive log that documents the evolution of your codebase, including the reasons behind each change.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life