Developer and Text Tools

Git Commands by Task: Undo, Branch, Rebase and Recover Lost Work

Find the right Git command for everyday tasks and emergencies: undo commits, fix branches, rebase safely, stash work and recover lost commits with reflog.

Saizul Amin
Saizul Amin30 Oct 2026 · 3 min read
Git Commands by Task: Undo, Branch, Rebase and Recover Lost Work

Git Commands by Task: Undo, Branch, Rebase and Recover Lost Work

Git's power comes with a vocabulary problem: there are several commands that sound like they undo things, and they do very different things. The free Git Command Reference is organised by task, so you can search "undo last commit" or "who changed this line" and get the exact command with a warning when it rewrites history. This guide covers the tasks people search for most.

Undoing things

Throw away changes in a file you have not staged: git restore file. The changes are gone.

Unstage a file but keep the changes: git restore --staged file.

Undo the last commit but keep the work: git reset --soft HEAD~1 leaves the changes staged; --mixed (the default) leaves them unstaged. Use this when you committed too early or to the wrong branch.

Undo a commit that is already pushed: git revert commit. It adds a new commit that reverses the old one, so nobody else's history changes. This is the safe choice on shared branches.

Throw away the last commit completely: git reset --hard HEAD~1. Flagged as destructive, because uncommitted work is lost too.

The reflog: your safety net

Git rarely deletes commits immediately. git reflog lists every position your HEAD has been at, including commits no branch points to any more. After a bad reset or rebase, find the line from before the mistake and run git reset --hard to that commit, or create a branch there with git branch rescue commit. Unreachable commits stay for weeks before cleanup.

Branches

  • Create and switch: git switch -c feature/login.
  • Rename the current branch: git branch -m new-name.
  • Delete a merged branch: git branch -d name; -D forces it.
  • See branches with their upstreams: git branch -vv.

Merge or rebase?

Merging keeps both histories and adds a merge commit. Rebasing replays your commits on top of another branch for a straight line of history. Rebase your own unpublished work freely; avoid rebasing commits others have already pulled. git pull --rebase keeps your local commits on top of the latest remote ones without merge bubbles. If a rebase goes wrong, git rebase --abort returns to where you started.

Pushing safely

git push --force-with-lease replaces the remote branch only if nobody pushed in the meantime, unlike --force, which overwrites blindly. Use it after rebasing your own feature branch.

Finding things

  • git log -S "text" finds the commits that added or removed some text.
  • git blame -w -C file shows who last changed each line, ignoring whitespace and following moved code.
  • git bisect finds the commit that introduced a bug by testing the middle of the range repeatedly.

Stash

git stash push -m "note" puts unfinished work aside so you can switch branches; git stash pop brings it back. Give stashes messages, or you will not remember what stash@{3} was.

Tags and releases

git tag -a v1.2.0 -m "Release 1.2.0" creates an annotated tag with a message and author, which is what release tooling expects; git push origin --tags sends tags to the remote, because a normal push does not.

Working on two branches at once

git worktree add ../hotfix hotfix-branch checks out another branch in a second folder that shares the same repository. You can fix a production bug there without stashing or committing half-finished work in your main folder.

Using the reference

Search in plain words or pick a section. The careful column marks commands that rewrite history or delete work. Replace the placeholders in angle brackets, and when in doubt, try the command in a fresh clone first. Download the list as CSV for your team's wiki.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages