AlgoMaster Logo

Fixing Mistakes

14 min readUpdated December 6, 2025
Listen to this chapter
Unlock Audio

Mistakes are an inevitable part of any development process, especially when working with version control like Git. Whether you accidentally committed the wrong file, merged branches incorrectly, or made a mess during a rebase, knowing how to fix these mistakes is essential for maintaining a clean and functional repository.

In this chapter, we'll explore various strategies for fixing common mistakes in Git. You'll learn about undoing commits, resetting changes, using the reflog to recover lost references, and handling unintended merges. By the end of this chapter, you should feel empowered to tackle and fix your Git errors confidently.

Undoing Commits

Sometimes you commit changes that you later regret. Fortunately, Git provides several ways to undo commits, depending on whether you want to keep or discard your changes.

Using git reset

The git reset command is a versatile tool for undoing commits. It operates with three primary modes: --soft, --mixed, and --hard.

  • Soft Reset: This option undoes the commit but keeps your changes staged in the index.
  • Mixed Reset: This is the default mode of git reset. It undoes the commit and unstages your changes, keeping them in your working directory.
  • Hard Reset: This option is the most destructive. It removes the commit and any changes in your working directory, effectively reverting to the state of the previous commit.

Example Scenario: Accidental Commit

Imagine you committed a sensitive file by mistake:

To remove this commit and keep your changes staged, use:

You can then clean up the sensitive files before re-committing.

Reverting Commits

In scenarios where you want to keep the commit history intact but need to negate the effects of a previous commit, git revert is your friend.

This command creates a new commit that reverses the changes made by the specified commit.

Using git revert

To revert a commit, you need its commit hash:

Then, run:

Example Scenario: Undoing a Buggy Commit

If you deployed a buggy feature and want to revert it:

This method maintains your commit history while effectively fixing the mistake, making it particularly useful in collaborative environments.

Handling Merge Mistakes

Merging branches can sometimes lead to mishaps, especially when conflicts arise or the wrong branch is merged.

Fixing Merge Conflicts

When you encounter a merge conflict, Git will halt the merge process and let you resolve the conflicts manually. You'll see conflict markers in the affected files, showing you the changes from both branches.

Open the conflicted files and look for the markers:

Edit the file to resolve the conflict, then add it to the staging area:

Complete the merge:

Example Scenario: Merging the Wrong Branch

If you accidentally merge the wrong branch, you can undo the merge with:

However, if you've already committed the merge, use git revert as described earlier to negate the merge commit.

Using the Reflog

Sometimes, mistakes lead to lost commits or branches, but Git keeps track of all actions through the reflog. This is a powerful feature that allows you to retrieve lost references.

What is Reflog?

The reflog records updates to your branches and HEAD. This means that even if you've removed a commit or reset your branch, you can retrieve its previous state.

Finding Lost Commits

To view your reflog, use:

You will see entries like this:

You can revert to any of these previous HEAD states using:

Example Scenario: Recovering a Lost Commit

Suppose you reset your branch and want to recover the lost commit:

  1. Run git reflog to find the commit hash.
  2. Use git cherry-pick to apply the changes to your current branch:

This approach prevents data loss and helps maintain your commit history.

Fixing Staging Area Issues

Sometimes, you may accidentally stage the wrong files or want to unstage changes. Git provides simple commands for these situations.

Unstaging Changes

To unstage a file that you've added to the staging area:

If you want to unstage all files:

Example Scenario: Accidental Staging

You added multiple files but realized one should not be included in the commit:

Resetting Changes in the Working Directory

If you want to discard uncommitted changes in a file, use:

This command can help you quickly fix mistakes without affecting your commit history.

Summary of Fixing Mistakes

Mistakes are a natural part of working with Git, but understanding how to fix them will significantly improve your workflow. We've explored various methods to undo commits, revert changes, resolve merge conflicts, utilize reflog for lost commits, and address staging area issues.

By mastering these techniques, you can handle errors confidently and maintain a clean project history.

Now that you understand the various strategies for fixing mistakes in Git, you are ready to explore recovering lost work. In the next chapter, we will look at techniques for rescuing any work you might have lost, providing you with additional tools to safeguard your development process.