Git Revert and Reset

Understand how to revert (undo) changes made in commits using 'git revert'. Learn the different types of reset (soft, mixed, hard) and their effects using 'git reset'.


Understanding Git Revert

What is Git Revert?

Git revert is a powerful command used to undo changes made in a specific commit. Unlike git reset, which modifies the project history, git revert creates a new commit that contains the inverse of the changes introduced by the target commit. This means your project history remains intact, making revert a safer option for shared repositories.

In-depth Explanation of 'git revert'

How it Works

When you run git revert <commit-hash>, Git performs the following steps:

  1. Identifies the Target Commit: Git uses the provided <commit-hash> to locate the commit you want to undo.
  2. Calculates the Inverse Changes: Git analyzes the differences introduced by the target commit. It then calculates the changes needed to effectively "undo" those changes. This might involve deleting lines that were added, adding lines that were deleted, or modifying existing lines to their previous state.
  3. Creates a New Commit: Git automatically creates a new commit containing these inverse changes. This new commit has a message indicating that it's a revert of the specified commit. You can also edit this message to provide more context.
  4. Applies the Changes to the Working Directory and Index: The changes included in the new revert commit are then applied to your working directory and staged in the index (ready to be committed).

In essence, git revert doesn't erase the past. It adds a new chapter to your project's history that logically undoes a previous chapter.

Example

Let's say you have a commit with the hash a1b2c3d4 that introduced a bug. To revert this commit, you would run:

git revert a1b2c3d4

This will open your text editor (configured in Git) with a pre-populated commit message like:

Revert "Add buggy feature"

This reverts commit a1b2c3d4.

You can modify this message or accept it as is. After saving and closing the editor, Git will create the revert commit.

Advantages of Using 'git revert'

  • Preserves History: Unlike git reset, revert doesn't rewrite history. This is crucial when working on shared repositories, as rewriting history can cause significant problems for other developers who have based their work on the original history.
  • Safe for Public Branches: Because it creates a new commit, revert is safe to use on public branches like main or develop. It doesn't disrupt the workflow of other developers.
  • Easy to Undo a Revert: If you revert a commit by mistake, you can simply revert the revert commit itself! This makes it a very forgiving operation.
  • Clear Audit Trail: The history clearly shows which commits were reverted and when, providing a clear audit trail of changes.

Conflicts during Revert

Sometimes, reverting a commit can lead to conflicts. This happens when the changes introduced by the commit you're reverting are intertwined with changes made in later commits. Git will halt the revert process and present you with the conflicts to resolve.

To resolve these conflicts, you'll need to:

  1. Open the conflicted files in your editor.
  2. Examine the conflict markers (<<<<<<< HEAD, =======, and >>>>>>> commit-hash).
  3. Decide which changes to keep (or create a new combination of changes).
  4. Remove the conflict markers.
  5. Stage the resolved files using git add.
  6. Continue the revert process with git revert --continue.