This guide explores different methods to undo changes in Git, including when to use each approach and practical examples. By mastering these techniques, you can confidently handle any situation that arises during version control.

1. Amending a Commit

If you’ve made a commit but want to correct the message or add files, use git commit --amend. This modifies the most recent commit without creating a new one.

Correcting a Commit Message

To change the message of your last commit:

git commit --amend -m "Updated commit message"

Adding Files to the Last Commit

If you forgot to include some changes, stage the files and amend the commit:

git add 
git commit --amend

This updates the last commit to include the newly staged changes.

2. Resetting Changes

The git reset command lets you undo changes in the staging area or even roll back commits. Reset comes in three modes:

Soft Reset

Moves the head pointer but keeps changes in the working directory and staging area:

git reset --soft 

Mixed Reset

Unstages changes but keeps them in the working directory (default behavior):

git reset 

Hard Reset

Discards all changes and resets the working directory to match the specified commit:

git reset --hard 

Warning: Hard reset permanently deletes changes, so use it with caution.

3. Reverting Changes

To undo a commit while preserving history, use git revert. This creates a new commit that reverses the changes from a previous commit.

Revert a Specific Commit

To revert changes from a commit:

git revert 

Reverting Multiple Commits

Revert a range of commits by specifying the commit hashes:

git revert ..

This generates individual revert commits for each commit in the range.

4. Using Checkout to Undo Changes

The git checkout command can be used to discard changes in a specific file or switch branches. Note that this command has been replaced by git restore for undoing changes in newer Git versions.

Discard Changes in a File

To discard changes in a file and restore it to the last committed state:

git checkout -- 

Switch to Another Branch

Use checkout to switch to a different branch:

git checkout 

Example: Undoing Changes in a .NET Project

Suppose you’re working on a .NET project and accidentally committed changes with a typo in the message:

// Program.cs
using System;

namespace UndoChangesExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Initial code for undo example.");
        }
    }
}

To fix the commit message:

git commit --amend -m "Corrected commit message"

If you need to revert a specific commit that introduced an error:

git revert 

If you staged the wrong files, unstage them with:

git reset 

Best Practices for Undoing Changes

  • Use amend for small fixes: Correct minor mistakes before pushing commits.
  • Use revert for shared branches: Preserve history when undoing changes in a collaborative environment.
  • Be cautious with reset: Understand the implications of each reset mode to avoid losing work.

Conclusion

Undoing changes in Git is a fundamental skill that ensures flexibility and confidence during development. By understanding how to use amend, reset, revert, and checkout effectively, you can maintain a clean commit history and adapt to unexpected situations seamlessly.