Ronalds Vilciņš

A Guide to Fixing Common Git Mistakes

Git is a amazing tool, but it can be intimidating and frustrating, especially when things go wrong. The documentation often assumes you already know what you’re looking for, which isn’t helpful when you’re in a panic. To make matters worse, mistakes in Git can feel irreversible if you don’t know the right commands.

But don’t worry! This guide will walk you through some common Git mishaps and show you how to fix them in plain English. Whether you’ve committed to the wrong branch, lost changes, or just need to undo something, we’ve got you covered.

1. I Need a Time Machine

Sometimes, you make a mistake and wish you could go back in time. Git has a built-in time machine called reflog.

git reflog  
# This shows a list of everything you've done in Git, across all branches.  
# Each action has an index (e.g., HEAD@{index}).  
# Find the one before you broke everything.  
git reset HEAD@{index}  

When to use this:

Example:
You accidentally deleted a branch and lost some important changes. Use reflog to find the commit where the branch still existed and reset to it.

2. I Committed but Need to Make a Small Change

You’ve just committed your changes, only to realize you forgot something small, like fixing a typo or adding a missing file. Instead of creating a new commit, you can amend the last one.

# Make your changes.  
git add .  # or add specific files.  
git commit --amend --no-edit  
# Now your last commit includes the new changes!  

Warning: Never amend commits that have already been pushed to a public or shared branch. This can cause conflicts for others.

Example:
You committed your code but forgot to add a new file. Instead of creating a separate commit, you can amend the previous one to include the missing file.

3. I Need to Change My Last Commit Message

Commit messages are important, but sometimes they’re poorly written or don’t follow team guidelines. You can easily fix this.

git commit --amend  
# Follow the prompts to edit the commit message.  

Example:
You wrote “Fixed bug” as your commit message, but your team requires a more descriptive message like “Fix null pointer exception in user login flow.” Use --amend to update it.

4. I Committed to the Wrong Branch

You meant to commit to a feature branch, but accidentally committed to main or master. Here’s how to fix it:

# Create a new branch from the current state of master.  
git branch some-new-branch-name  
# Remove the last commit from the master branch.  
git reset HEAD~ --hard  
# Switch to the new branch.  
git checkout some-new-branch-name  
# Your commit now lives in this branch!  

Alternative: If you’ve already pushed the commit to a shared branch, you can use cherry-pick to move it:

git checkout name-of-the-correct-branch  
git cherry-pick master  # Grab the last commit from master.  
git checkout master  
git reset HEAD~ --hard  # Remove the commit from master.  

Example:
You committed a new feature to main instead of your feature-branch. Use the steps above to move the commit to the correct branch.

5. I Ran git diff but Nothing Happened!

If you’ve staged changes with git add but don’t see them when running git diff, you need to use the --staged flag.

git diff --staged  

Example:
You added a file with git add and want to review the changes before committing. Use git diff --staged to see the differences.

6. I Need to Undo a Commit from a While Ago!

If you’ve committed something you later realize was a mistake, you can undo it with git revert.

# Find the commit you need to undo.  
git log  
# Use the arrow keys to scroll through history.  
# Save the hash of the commit you want to undo.  
git revert [saved hash]  
# Git will create a new commit that undoes the changes.  

Example:
You committed a bug five commits ago. Instead of manually fixing the code, use git revert to undo the changes in a new commit.

7. I Need to Undo Changes to a File

If you’ve made changes to a file and want to revert it to its state in a previous commit, you can use git checkout.

# Find the commit hash before the file was changed.  
git log  
# Save the hash and restore the file.  
git checkout [saved hash] -- path/to/file  
# Commit the restored file.  
git commit -m "Restored file to previous state."  

Example:
You accidentally deleted a critical function in a file. Use git checkout to restore the file to its state in a previous commit.

8. My Repo Is Completely Broken

If your repository is in such a bad state that you’re tempted to delete it and start over, try this first:

# Get the latest state from the remote repository.  
git fetch origin  
# Reset your local branch to match the remote.  
git checkout main  
git reset --hard origin/main  
# Delete untracked files and directories.  
git clean -d --force  

Warning: These commands are destructive and cannot be undone. Use them with caution!

Example:
You’ve made a series of mistakes and your local repository is a mess. Use the steps above to reset it to match the remote repository.

Final Thoughts

Git can be tricky, but with the right commands, you can recover from almost any mistake. The key is to stay calm, take a deep breath, and remember that Git is designed to help you, not hinder you.

If all else fails, don’t be afraid to ask for help. The Git community is vast, and chances are someone else has faced the same issue. Happy coding!