5 Git Mistakes Every Developer Makes (And How to Fix Them)

Everyone commits the wrong file. Everyone pushes to the wrong branch. The commands to undo these things exist — they are just buried under a mental model nobody fully explains. Here are the five most common Git mistakes and the exact commands to recover from them.

  • 01
    Committed to the wrong branch

    You wrote code on main that was meant for a feature branch. Fix it without losing your work:

    git branch feature/my-work # create the right branch git reset HEAD~1 --soft # undo the commit, keep staged changes git stash # stash those changes git checkout feature/my-work # switch to correct branch git stash pop # restore your work git commit -m "where this belongs"

    The key is --soft: it rewinds the commit pointer but leaves your changes staged so nothing is lost.

  • 02
    Committed a file you should not have (env vars, secrets)

    The file is staged and committed. Remove it from the commit without deleting it from disk:

    git rm --cached .env # unstage from tracking echo ".env" >> .gitignore # make sure it stays out git commit --amend --no-edit # rewrite the last commit
    Warning

    If you have already pushed this commit, rotate the secrets. The commit history is not safely rewritable once others have pulled it — treat any pushed credential as compromised.

  • 03
    Pushed a bad commit to a shared branch

    Do not rewrite history on a shared branch. Use a revert commit instead — it creates a new commit that undoes the damage cleanly:

    git log --oneline # find the bad commit hash git revert abc1234 # creates an undo commit git push

    Revert is safe for shared branches because it adds to history rather than rewriting it. Every collaborator sees what happened.

  • 04
    Accidentally deleted a branch with unmerged work

    Git keeps commits for 30–90 days in the reflog even after you delete the branch. Find and recover them:

    git reflog # find the last commit hash of your branch git checkout -b recovered abc1234 # recreate it

    The reflog is your safety net. It records every HEAD movement — even across branch deletes and hard resets.

  • 05
    Messed up a merge and want to start over

    Mid-merge, your working tree is a conflict graveyard. To abort and return to the pre-merge state:

    git merge --abort

    If you already finished the merge commit and want to revert it:

    git revert -m 1 HEAD # -m 1 = keep the first parent (your branch)
Git Unfucked — the full recovery reference

30+ scenarios. Every reset, revert, reflog, and stash pattern documented with context on when to use each. One PDF, buy once.

Get Git Unfucked →

More from the blog

The Git Recovery Cheatsheet → How to Undo a Git Commit (The Right Way) → How to Recover a Deleted Git Branch →

Get notified when we ship

New Unfucked references and dev tips. No spam.