Git beyond push: rebase, stash and recovering lost commits
5 min readGit · Developer Tools · Guides
Most developers learn four Git commands, add, commit, push and pull, and stop there. That is enough until the day it is not. You are halfway through a change when an urgent bug arrives. Your branch has twelve messy commits and a reviewer asks for a clean history. You run one wrong command and a day of work seems to vanish. Each of these has a standard answer, and the answers are stash, rebase and reflog.
On this site I have written about using git push as a deploy button and about worktrees for parallel agents. Those posts assume you are comfortable with Git. This one fills the gap between beginner and comfortable, and its aim is to take the fear out of the commands that feel dangerous.
A quick model of how Git thinks
A commit is a snapshot with a pointer to its parent. A branch is just a movable label pointing at one commit. HEAD is a pointer to where you are now, usually a branch. Almost every scary Git command is really a way of moving these labels around. Commits themselves are very hard to destroy; they just become unreachable, and that is the fact that makes recovery possible.
Stash: park work without committing it
You have uncommitted changes and need a clean working directory, perhaps to switch branches. You do not want a half-finished commit. git stash takes your modifications, stores them on a stack and gives you back a clean tree.
git stash push -m "half-done search filter"
git switch main
# ...fix the urgent bug, commit, push...
git switch feature/search
git stash list
git stash popA few details save trouble. By default, stash ignores untracked files, so add -u if you created new ones. Give each stash a message, otherwise a list of stashes named after commits is useless. pop applies the stash and removes it, while apply keeps it in the list, which is safer when you are unsure whether it will apply cleanly. If you find yourself stashing for days, you probably want a real branch with a work-in-progress commit instead.
Merge vs rebase
Both combine work from two branches. Merge creates a new commit that joins the histories and preserves exactly what happened. Rebase takes your commits and replays them, one by one, on top of another branch, which gives a straight line of history as if you had started from the latest code.
# bring main's new commits under your feature branch
git fetch origin
git rebase origin/main
# if a conflict appears: fix files, then
git add path/to/file
git rebase --continue
# or bail out completely
git rebase --abortDuring a rebase Git applies your commits one at a time, so you may resolve conflicts several times, once per commit that touches the problem area. That feels worse than a single merge conflict, though the result is a cleaner log. git rebase --abort is your safety exit: it returns the branch exactly to where it was before you started, so trying a rebase costs you nothing.
The golden rule of rebasing
Rebasing rewrites commits. The new commits have different identifiers even if the content is the same. So never rebase commits that other people have already pulled and based work on, such as the shared main branch. If you do, their history and yours diverge and everyone gets to untangle it. Rewriting your own unpublished or personal feature branch is fine, and that is where rebase belongs.
When you do need to push a rebased branch you already pushed before, a normal push is rejected because history changed. Use git push --force-with-lease. Unlike plain --force, it refuses to overwrite the remote if someone else pushed something you have not seen.
Interactive rebase: tidy before review
This is the version that makes history readable. You choose how many recent commits to edit and Git opens a list. You can reorder them, reword messages, squash several into one and drop the ones that were mistakes.
git rebase -i HEAD~5
# in the editor, change the word at the start of a line:
# pick keep the commit as it is
# reword keep it, change the message
# squash melt it into the previous commit
# fixup like squash, discard its message
# drop delete the commitMy habit is to commit freely while working, with messages like wip and fix typo, and then squash into a handful of meaningful commits before opening a pull request. Reviewers read a story instead of an archaeological dig. If a project uses squash-merge on its platform, you may not need to do this at all, so follow what your team does.
Reflog: your undo history
Here is the safety net that makes everything above less scary. Git keeps a private log, the reflog, of everywhere HEAD has pointed in your local repository: every commit, checkout, reset, rebase and merge. Even when a commit is no longer on any branch, the reflog usually still remembers where it was.
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9f8e7d6 HEAD@{1}: commit: add search filter
# 5c4b3a2 HEAD@{2}: commit: add search index
# put your branch back where it was before the mistake
git reset --hard 9f8e7d6
# or keep the lost work on a new branch to be safe
git branch rescued 9f8e7d6Read it as a timeline, newest at the top. Find the line from before things went wrong, then point a branch at that commit. Creating a new branch at the old commit is the gentlest option, since it changes nothing else. The same trick rescues commits after an overly enthusiastic rebase, a bad reset --hard and a deleted branch.
There are limits. The reflog is local: it does not travel with a clone and it will not help you on a fresh machine. Entries expire after a period, typically weeks to months by default, and uncommitted changes that you discarded were never in the reflog at all. If the work was never committed or stashed, Git generally cannot bring it back.
Two small tools worth knowing
- cherry-pick -
git cherry-pick <commit>copies one commit onto your current branch, useful for pulling a single fix without merging everything. - restore and switch -
git restore filediscards changes to a file, andgit switch branchchanges branches. They replaced the overloadedcheckoutfor these jobs and are harder to misuse.
Where it bites
- reset --hard discards uncommitted work - there is no reflog entry for changes you never committed. Stash or commit first when in doubt.
- Rebasing shared branches - the cause of most force-push horror stories. Rebase your own work, merge shared work.
- Plain --force - it can erase a teammate's commits. Prefer
--force-with-lease. - Stash piles - old stashes are forgotten and later fail to apply. Treat the stash as a short-term shelf.
- Conflicts repeated during rebase - enable
git config rerere.enabled trueso Git remembers how you resolved a conflict. - Large squashes - squashing twenty commits hides useful history. Squash noise, not meaning.
The rules I keep
Commit often, so there is always something to recover. Branch before anything risky, because a branch name is a free bookmark. Rebase private branches and merge shared ones. Run git status and git log --oneline -5 before a destructive command, not after. And when something goes wrong, stop and read git reflog before trying anything else, because panic commands make recoveries harder.
Git rarely loses what you committed. It only makes it easy to lose track of where you put it.

Written by
Shihab Hossain — Full-Stack Developer
I build thoughtful digital products from Bangladesh — fast web experiences with Next.js, Android apps with Flutter, and the infrastructure behind them.
Work with me