Blog Git tricks for untangling a messy branch

Git tricks for untangling a messy branch

2 min read

Git tricks for untangling a messy branch
TL;DR When a branch gets tangled, a few Git commands fix most of it: restore to discard file changes, reset to move the branch pointer, rebase to tidy history, cherry-pick to grab one commit, and reflog to recover anything you thought you lost.

Every so often a branch turns into a knot. Wrong commit, staged the wrong files, history you want to tidy before a pull request. Git has a small set of tools that handle almost all of it, once you know which one fits the mess in front of you.

Discard file changes: git restore

You edited a file and want it back the way it was.

git restore path/to/file        # discard working-tree changes
git restore --staged path/to/file   # unstage, keep the edits

restore touches files, not history. It is the safe first reach when only your working copy is wrong.

Move the branch pointer: git reset

You committed too early, or want to undo the last commit but keep the work.

git reset --soft HEAD~1    # undo the commit, keep changes staged
git reset HEAD~1           # undo the commit, keep changes unstaged
git reset --hard HEAD~1    # undo the commit and discard the changes

--soft and the default are gentle. --hard throws away the working changes, so use it deliberately.

Tidy history before sharing: git rebase

Before opening a pull request, you can clean up a messy series of commits.

git rebase -i HEAD~4   # squash, reword, or reorder the last 4 commits

Rebase local commits freely. Avoid rebasing anything you have already pushed to a shared branch, since it rewrites history others may have.

Grab one commit: git cherry-pick

You need a single commit from another branch, not the whole thing.

git cherry-pick <commit-hash>

Handy for pulling a fix onto a release branch without merging everything around it.

Recover what you lost: git reflog

The one that saves you after a bad reset. reflog records where HEAD has been.

git reflog                 # find the commit you reset away from
git reset --hard <hash>    # go back to it

Most "I deleted my work" moments are really "I moved the pointer," and reflog moves it back.

The mindset

The trick is to name the problem before you type. Is it a file, the branch pointer, the history, or a lost commit? Each has its own tool. Reach for the gentlest one that fits, and remember that reflog is your safety net when a bigger move goes wrong.

FAQ

What is the difference between git reset and git restore?

restore changes files in your working tree or staging without moving the branch. reset moves the branch pointer, and depending on the flag, updates the staging area and working tree too. Use restore to undo file edits, reset to change which commit the branch is on.

Is it safe to rebase?

Rebasing local, unpushed commits is safe and keeps history clean. Rebasing commits you have already shared can rewrite history others depend on, so avoid it on shared branches unless the team agrees. When in doubt, rebase only what has not left your machine.

Can I recover commits after a bad reset?

Usually yes. git reflog lists where HEAD has been, including the commit you just reset away from. Find its hash in the reflog and you can reset or cherry-pick back to it. Git rarely throws work away immediately.