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

Undo the last commit but keep my changes
git reset --soft HEAD~1

Moves the branch back one commit; your changes stay staged, ready to re-commit.

Undo the last commit and throw away the changes
git reset --hard HEAD~1

Deletes the last commit and its changes. There's no undo — be sure.

Unstage a file (undo git add)
git restore --staged <file>

Takes the file out of staging but keeps your edits. Older git: git reset <file>.

Discard uncommitted changes in a file
git restore <file>

Reverts the file to the last committed version. The edits are gone.

Fix the last commit message
git commit --amend -m "new message"

Rewrites the most recent commit's message. Don't do this after pushing shared history.

Add a forgotten file to the last commit
git commit --amend --no-edit

Stage the file first, then this folds it into the previous commit without changing the message.

Undo a commit that's already pushed (safely)
git revert <commit>

Creates a new commit that reverses the changes — safe for shared branches since it doesn't rewrite history.

Branches

Create a new branch and switch to it
git switch -c <branch>

The modern way (Git 2.23+). Old style: git checkout -b <branch>.

Switch to an existing branch
git switch <branch>

Modern equivalent of git checkout <branch>.

Delete a local branch
git branch -d <branch>

Use -D (capital) to force-delete a branch that isn't fully merged.

Delete a remote branch
git push origin --delete <branch>

Removes the branch from the remote; the local copy stays until you delete it too.

Rename the current branch
git branch -m <new-name>

Renames the branch you're on.

List all branches including remotes
git branch -a

Shows local and remote-tracking branches.

Commits & history

See a compact, visual commit history
git log --oneline --graph --all

One line per commit with a branch graph across all branches.

See what changed in a specific commit
git show <commit>

Shows the commit message and its full diff.

Find commits whose message contains text
git log --grep="text"

Searches commit messages for the text.

See who last changed each line of a file
git blame <file>

Annotates every line with the commit and author that touched it.

Stash

Temporarily shelve my changes
git stash

Saves your uncommitted changes and gives you a clean working tree.

Bring my stashed changes back
git stash pop

Re-applies the most recent stash and removes it from the stash list.

Stash including new untracked files
git stash -u

Plain git stash ignores untracked files; -u includes them.

List my stashes
git stash list

Shows all saved stashes.

Remote & push

Connect a local repo to a remote
git remote add origin <url>

Adds 'origin' as the remote; then push with -u the first time.

Push a new branch and track it
git push -u origin <branch>

Sets the upstream so future git push / git pull need no arguments.

Change the remote URL
git remote set-url origin <url>

Repoints origin — e.g. when switching from HTTPS to SSH.

Force push without clobbering others' work
git push --force-with-lease

Safer than --force: it refuses if the remote has commits you haven't seen.

Sync

Pull and avoid a merge commit
git pull --rebase

Replays your local commits on top of the fetched ones instead of merging.

Throw away local changes and match the remote
git reset --hard origin/main

Makes your branch identical to origin/main. Local changes are lost.

Update my fork from the original repo
git fetch upstream && git merge upstream/main

Pulls in upstream's latest, after adding it as a remote named 'upstream'.

Files & ignore

Stop tracking a file but keep it on disk
git rm --cached <file>

Removes it from git without deleting the local file — then add it to .gitignore.

Apply a new .gitignore to already-tracked files
git rm -r --cached . && git add .

Clears the index and re-adds everything, so newly-ignored files drop out. Commit after.

Recovery

Recover a deleted branch or lost commit
git reflog

Lists every position HEAD has had; find the hash, then git checkout <hash> or git branch <name> <hash>.

Restore a file deleted in a past commit
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:

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>.

More shell tools: curl, find, ssh, or all on the home page.