Git Command Finder — “How Do I…” → the Exact Command
Forget which git incantation undoes a commit or unstages a file? Search what you want to do and get the exact command, with a plain-English note on what it does. No signup.
Undo & fix
git reset --soft HEAD~1 Moves the branch back one commit; your changes stay staged, ready to re-commit.
git reset --hard HEAD~1 Deletes the last commit and its changes. There's no undo — be sure.
git restore --staged <file> Takes the file out of staging but keeps your edits. Older git: git reset <file>.
git restore <file> Reverts the file to the last committed version. The edits are gone.
git commit --amend -m "new message" Rewrites the most recent commit's message. Don't do this after pushing shared history.
git commit --amend --no-edit Stage the file first, then this folds it into the previous commit without changing the message.
git revert <commit> Creates a new commit that reverses the changes — safe for shared branches since it doesn't rewrite history.
Branches
git switch -c <branch> The modern way (Git 2.23+). Old style: git checkout -b <branch>.
git switch <branch> Modern equivalent of git checkout <branch>.
git branch -d <branch> Use -D (capital) to force-delete a branch that isn't fully merged.
git push origin --delete <branch> Removes the branch from the remote; the local copy stays until you delete it too.
git branch -m <new-name> Renames the branch you're on.
git branch -a Shows local and remote-tracking branches.
Commits & history
git log --oneline --graph --all One line per commit with a branch graph across all branches.
git show <commit> Shows the commit message and its full diff.
git log --grep="text" Searches commit messages for the text.
git blame <file> Annotates every line with the commit and author that touched it.
Stash
git stash Saves your uncommitted changes and gives you a clean working tree.
git stash pop Re-applies the most recent stash and removes it from the stash list.
git stash -u Plain git stash ignores untracked files; -u includes them.
git stash list Shows all saved stashes.
Remote & push
git remote add origin <url> Adds 'origin' as the remote; then push with -u the first time.
git push -u origin <branch> Sets the upstream so future git push / git pull need no arguments.
git remote set-url origin <url> Repoints origin — e.g. when switching from HTTPS to SSH.
git push --force-with-lease Safer than --force: it refuses if the remote has commits you haven't seen.
Sync
git pull --rebase Replays your local commits on top of the fetched ones instead of merging.
git reset --hard origin/main Makes your branch identical to origin/main. Local changes are lost.
git fetch upstream && git merge upstream/main Pulls in upstream's latest, after adding it as a remote named 'upstream'.
Files & ignore
git rm --cached <file> Removes it from git without deleting the local file — then add it to .gitignore.
git rm -r --cached . && git add . Clears the index and re-adds everything, so newly-ignored files drop out. Commit after.
Recovery
git reflog Lists every position HEAD has had; find the hash, then git checkout <hash> or git branch <name> <hash>.
git checkout <commit>~1 -- <file> Grabs the file as it was just before <commit> and restores it.
reset vs restore vs revert — the three “undos”
Most git confusion comes from having three different undo verbs:
git restoreworks on files — discard edits or unstage something. It doesn't touch history.git resetmoves the current branch to an earlier commit.--softkeeps your changes,--hardthrows them away. Rewrites local history — fine before you push, risky after.git revertmakes a new commit that undoes an old one. It's the safe choice for commits you've already pushed, because it adds history instead of rewriting it.
The golden rule of rewriting history
Anything that rewrites commits — reset --hard, commit --amend,
rebase, push --force — is safe on commits only you have, and dangerous on
commits others have pulled. When in doubt on a shared branch, prefer revert and
--force-with-lease over --force.
Frequently asked questions
How do I undo my last commit in git?
git reset --soft HEAD~1 removes the commit but keeps your changes staged.
git reset --hard HEAD~1 also discards the changes. If it's already pushed, use
git revert <commit> instead.
How do I unstage a file?
git restore --staged <file> takes a file out of staging while keeping your
edits. (Older git: git reset <file>.)
How do I delete a git branch?
Local: git branch -d <branch> (use -D to force). Remote:
git push origin --delete <branch>.
How do I recover a deleted branch or lost commit?
git reflog lists every position HEAD has had; find the hash and run
git checkout <hash> or git branch <name> <hash>.