10 Common Git Mistakes Beginners Make and How to Undo Them
October 17, 2026

10 Common Git Mistakes Beginners Make and How to Undo Them

Most Git disasters are not disasters. They are reversible states that look permanent because the terminal is quiet and the error message is cryptic. The tool keeps almost everything you do for months, so the commit you regret and the branch you "deleted forever" are usually a couple of commands away from coming back.

What follows are the ten mistakes beginners hit most often, paired with the recovery steps. Run them in order, read the output, and don't panic-clear your working directory before you've had a look at what's recoverable.

1. You committed to the wrong branch

You finished a feature, staged it, committed, and then noticed the prompt says main instead of feature/login. This is a two-step fix, and the order matters.

  1. Copy the commit hash from git log --oneline -1.
  2. Switch to the branch that should have received it: git switch feature/login
  3. Bring the commit over: git cherry-pick <hash>
  4. Go back to the wrong branch and undo the commit: git switch main, then git reset --hard HEAD~1

If the wrong-branch commit is unpushed and you simply want it gone with the changes still staged, git reset --soft HEAD~1 is gentler: it deletes the commit and leaves your edits in the index, ready to commit again on the right branch.

Be careful with --hard on a shared branch. If main is already on the remote, don't rewrite it — reverse the commit with git revert <hash> instead, which adds a new commit rather than deleting history other people may already have.

2. The commit contains a password, a key, or a 400 MB file

If the commit has not been pushed, you can edit it. Remove the file from tracking while keeping it on disk, then amend:

git rm --cached .env
git commit --amend --no-edit

Add the file to .gitignore in the same commit or the next one, otherwise Git will keep offering it back.

If the commit has been pushed, editing the commit is only half the job. Treat any credential that reached a remote as exposed: rotate the key or token first, then clean history if your team agrees it's worth the disruption. For a large binary that's already in history, tools like git filter-repo handle the rewrite, but that is a coordinated operation with your team, not a solo cleanup.

3. A merge went sideways

Still in the middle of the merge

Conflicts everywhere, nothing committed yet: git merge --abort puts you back exactly where you started. Same idea for a rebase that has turned into a nightmare: git rebase --abort.

The merge already committed

Git saved the pre-merge state for you. git reset --hard ORIG_HEAD returns the branch to where it was before the merge. If you only want to back out the merge while keeping your uncommitted work, git reset --merge ORIG_HEAD is the safer option.

If the bad merge is already published, revert it: git revert -m 1 <merge-hash>. The -m 1 tells Git to keep the first parent — the branch you merged into. Do note that re-merging the same branch later won't bring its commits back automatically; you'll need to revert the revert first. That quirk has confused better developers than you.

4. A branch you deleted (or never named) is missing

Deleted a branch with unmerged commits? Git's reflog remembers where it pointed.

git reflog

Scan the left column for the commit you lost, then recreate the branch at that hash:

git switch -c recovered-work <hash>

Reflog entries stick around for 90 days by default, so you have time. If the reflog has been pruned, git fsck --lost-found can list dangling commits that were never attached to a branch.

Detached HEAD

That "You are in 'detached HEAD' state" warning means you checked out a commit rather than a branch. Any commit you make there belongs to no branch and disappears from view once you leave. Save it with git switch -c experiment <hash>, or abandon it with git switch main.

5. Force-push panic

Someone — possibly you — ran git push --force and flattened commits that were on the remote. The recovery depends entirely on whether a copy still exists locally.

  • You have the commits locally: find them with git reflog, recreate the branch, and push again with git push --force-with-lease.
  • Only a teammate has them: their local branch still points at the lost commits. Ask them to push it back; it takes seconds and saves hours.
  • Nobody has them: this is the one case where work is genuinely gone. It is rare, and it is why --force-with-lease exists.

Force-with-lease refuses the push if the remote has moved since your last fetch, which catches exactly the situation where you'd overwrite someone else's work. Make it a habit, and protect shared branches on the hosting side so nobody can force-push to main at all.

6. Changes that vanished between commands

git checkout -- file or git restore file discards uncommitted edits with no confirmation. If the file was ever staged, you can often get it back: staged content lives in the object database, so git fsck --lost-found may surface the blob, or your editor's local history feature might have it. If it was never staged and never committed, there is nothing for Git to recover — this is why committing small and often beats committing perfectly.

Two more that catch beginners: git reset --hard throws away uncommitted changes along with the commits, and git stash is easy to forget. Run git stash list periodically; an old stash you thought was lost is usually sitting right there.

7. The habit that prevents most of these

Before any risky command — reset, rebase, force-push — run git status and git log --oneline -5. Know what you're changing. If the operation feels large, create a safety branch first: git branch backup-before-rebase. It costs nothing and gives you a named, obvious place to return to.

A five-minute recovery drill

In a throwaway repository, deliberately make each mistake: commit to main, reset --hard, delete a branch with -D, force-push over a commit. Then recover each one using the reflog. Doing this once, calmly, means that when it happens in real work you'll recognize the shape of the problem instead of guessing at commands.

Git isn't asking you to be careful every second. It's asking you to know where the undo button is — and now you do.

Photo: MART PRODUCTION / Pexels

Previous Post

Your First 30 Days Learning to Code: A Week-by-Week Beginner Roadmap

Next Post

Build a Personal Expense Tracker with Python and SQLite

Related Posts