Skip to main content
imvinojanv.dev
  • About
  • Blog
  • Projects
  • Snippets

Command Palette

Search for a command to run...

imvinojanv.dev
Open to Hire

I'm always open to discussing software engineering work or partnership.

HomeAboutResumeUses
BlogProjectsSnippets
© 2026 Vinojan Veerapathirathasan —— Colombo, Sri Lanka.
MediumGitHubLinkedInRSS
Back

Common Git Mistakes and How to Fix Them

A practical guide to fixing common Git errors using commands like reflog, amend, cherry-pick, and more — with clean, professional explanations to help you regain control of your repository.

Published on May 26, 2025
3 min read
0 views

Git is a powerful version control system, but its complexity can lead to mistakes that are challenging to rectify. This guide provides solutions to common Git errors, helping you recover from various situations.

Recovering from Mistakes Using Git's Reflog

If you've made a significant error and need to revert to a previous state:

git reflog
# Displays a list of all actions you've performed in Git across all branches.
# Each entry has an index in the format HEAD@{index}.
# Identify the entry before the mistake occurred.
 
git reset HEAD@{index}
# Resets your repository to the selected state.

This method is useful for recovering deleted commits, undoing problematic merges, or returning to a stable state.([ohshitgit.com][2])

Amending the Most Recent Commit

If you've just committed changes and realize you need to make a small adjustment:

# Make your changes
git add . # or specify individual files
git commit --amend --no-edit
# Incorporates the changes into the previous commit without altering the commit message.

Note: Avoid amending commits that have already been pushed to shared or public branches to prevent complications for others.([ohshitgit.com][3])

Modifying the Last Commit Message

To change the message of your most recent commit:

git commit --amend
# This command opens your default editor, allowing you to edit the commit message.

Moving a Commit to a New Branch

If you've committed changes to the master branch that should be on a new branch:([ohshitgit.com][3])

git branch new-branch-name
# Creates a new branch from the current state of master.
 
git reset HEAD~ --hard
# Removes the last commit from the master branch.
 
git checkout new-branch-name
# Switches to the new branch where your commit now resides.

Note: This approach is effective only if the commit hasn't been pushed to a shared repository.([ohshitgit.com][3])

Transferring a Commit to the Correct Branch

If you've committed to the wrong branch:([ohshitgit.com][3])

git reset HEAD~ --soft
# Reverts the last commit but keeps the changes staged.
 
git stash
# Stashes the changes.
 
git checkout correct-branch
# Switches to the correct branch.
 
git stash pop
# Applies the stashed changes.
 
git add .
git commit -m "Your commit message"
# Commits the changes to the correct branch.

Alternatively, you can use cherry-pick:([ohshitgit.com][4])

git checkout correct-branch
git cherry-pick master
# Applies the last commit from master to the current branch.
 
git checkout master
git reset HEAD~ --hard
# Removes the last commit from master.

Understanding Empty Diff Outputs

If you run git diff and receive no output despite making changes:([ohshitgit.com][3])

git diff --staged
# Shows differences between the staged changes and the last commit.

This situation occurs when changes have been staged using git add.

Reverting a Specific Commit

To undo a commit from several commits ago:([ohshitgit.com][3])

git log
# Locate the commit hash you wish to revert.
 
git revert [commit-hash]
# Creates a new commit that reverses the changes from the specified commit.

This method is preferable to manually editing files, as it cleanly undoes specific commits.

0
0
0
0
0