How to Fix Broken Git History with Rebase, Reset, and Reflog

You committed to the wrong branch. You pushed a secret. You merged the wrong thing, or you rebased and lost a commit you needed. Git feels unforgiving in that moment, but almost everything is recoverable, because Git keeps far more than the log shows. Learning how to fix broken Git history comes down to three tools, rebase, reset, and reflog, and one rule: do not force push until you know exactly what you are replacing.

How to fix broken Git history without panic

Before you fix anything, find out where you are. Run git status and git log to see the current branch and the last few commits. Then decide what kind of breakage you have. A bad commit that only exists on your machine is easy. A bad commit already pushed to a shared branch needs a different approach, because other people may have pulled it. The fixes below are ordered from safest to most destructive, and that order is deliberate. Start at the top and stop as soon as the problem is solved.

Undo the last commit, keep the changes

The most common fix is also the gentlest. If your last commit has the wrong message or the wrong files, undo it without losing the work. A soft reset moves the branch pointer back one commit and leaves the changes staged, ready to be committed again properly. This is the fix for a typo in a commit message, a file that should not have been included, or a commit made on the wrong branch that you want to move. Nothing is deleted. The changes are still in your working tree, and you can commit them again in the right form.

git reset --soft HEAD~1
# moves the branch back one commit, keeps changes staged

Reset moves the branch pointer

Reset moves the branch pointer, and the mode you choose decides what happens to your files. Soft keeps everything staged. Mixed, the default, keeps the changes in your working tree but unstages them. Hard throws the changes away entirely, which is why you should only use it when you are sure the work is saved somewhere else or genuinely unwanted. A hard reset to an earlier commit is how you discard a bad experiment on a private branch. The commits you move past are not immediately deleted, which is where the reflog comes in.

git reset --mixed HEAD~1   # keep changes, unstage them
git reset --hard HEAD~1    # discard the last commit and its changes

Rebase to rewrite commits

Use rebase to rewrite commits that are already in your history. Interactive rebase opens a list of commits and lets you edit, squash, reorder, or drop them. This is how you split one large commit into two clear ones, fold a fix-up commit into the commit it fixes, or rewrite a commit message from three days ago. The rule from the rebase article applies here too: rewrite only commits that have not been pushed to a shared branch, or commits everyone agrees to replace. If the bad commit is already on main and other people have built on it, do not rewrite it. Revert it instead, with a new commit that undoes the change and leaves a readable record.

git rebase -i HEAD~3
# edit, squash, or drop the last three commits

Reflog finds lost commits

Reflog finds lost commits. Every time a branch pointer moves, Git records the old position in the reflog, including after resets and rebases that seem to have destroyed work. Run git reflog and you will see a list of where HEAD has been, with short descriptions. Find the entry from before the mistake, note its hash, and check it out or reset to it. This is the safety net that makes the other tools usable: even a hard reset can be undone if you have not run the commands that prune old objects. The habit to build is simple. Before any destructive command, run git reflog once, so you know the entry you will need if the command goes wrong.

git reflog
git reset --hard abc1234
# returns the branch to the commit recorded in the reflog

Stop before you force push

Stop before you force push and check three things. First, is the branch shared? If anyone else has pulled it, a force push rewrites their history too, and you owe them a warning first. Second, do you know exactly which commits you are replacing? Read the diff between your branch and the remote before you push, not after. Third, is the remote protected? Branch protection exists for this moment, and if it blocks your push, that is the system working. Prefer git push –force-with-lease over a plain force push, because it refuses to overwrite work that appeared on the remote since you last fetched. The difference between the two has saved many teams from losing a colleague’s commits.

The fixes, in order of safety

When history breaks, work through this order. First, undo a local mistake with a soft or mixed reset. Second, rewrite local commits with interactive rebase. Third, recover anything you lost with the reflog. Fourth, revert a pushed mistake with a new commit instead of rewriting it. Force push only as a last step, only on a branch you own, and only after warning anyone who might have pulled it. Keep a habit that makes all of this easier: small commits, pushed often, on branches with clear names. The smaller each commit is, the smaller the blast radius when one of them is wrong. And if the repository holds anything sensitive, check how to secure your Git repositories before you push again, because a leaked secret in history needs rotating, not just deleting. For a plain recovery of work rather than history, keeping a knowledge base in sync with Git shows the same reflog habits applied to everyday files. And when you choose where the repository lives, comparing Git hosting in 2026 covers the branch protection settings that stop force pushes from becoming accidents.

Broken history feels permanent and almost never is. Reset carefully, rebase locally, lean on the reflog, and keep force pushes rare and announced.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.