Tools

Git command finder

Describe what you want to do in plain words, like "undo last commit" or "rename branch". Get the exact commands, what they do, and a warning when they rewrite history. 33 situations.

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

The commit is gone and its changes go back to staged, ready to commit again. Use HEAD~2 to undo two commits.

If you already pushed, prefer git revert so you do not rewrite shared history.

Undo the last commit and throw away its changesRewrites or deletes
git reset --hard HEAD~1

Removes the commit and every change in it from your working folder.

If you already pushed, use git revert HEAD instead.

Undo a commit that is already pushed
git revert <commit-sha>
git push

Creates a new commit that reverses the old one. History stays intact, which is safe for shared branches.

Change the last commit message
git commit --amend -m "Better message"

Replaces the most recent commit with one that has the new message.

Already pushed? Run git push --force-with-lease, and only on a branch nobody else is using.

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

Folds the file into the last commit and keeps its message.

Unstage a file (undo git add)
git restore --staged path/to/file

The file stays changed on disk; it just leaves the staging area. Use . to unstage everything.

Throw away my changes to a fileRewrites or deletes
git restore path/to/file

Puts the file back to how it was in the last commit.

I committed to the wrong branch
git reset --soft HEAD~1
git stash
git switch right-branch
git stash pop
git commit -m "Message"

Takes the commit off this branch, carries the changes over and commits them where they belong.

Move my uncommitted changes to a new branch
git switch -c new-branch

Uncommitted changes come with you to the new branch automatically.

Create a branch and switch to it
git switch -c feature/name

The modern form of git checkout -b feature/name.

Rename a branch, locally and on the remote
git branch -m old-name new-name
git push origin -u new-name
git push origin --delete old-name

Renames it locally, pushes the new name and removes the old one from the remote.

Delete a branch, locally and on the remote
git branch -d branch-name
git push origin --delete branch-name

Use -D instead of -d to delete a branch that is not merged yet.

Put my changes aside for a moment (stash)
git stash push -m "What I was doing"
git stash list
git stash pop

Saves your work, cleans the folder, and brings it back later with pop.

See what I changed
git diff
git diff --staged

The first shows unstaged changes, the second what is about to be committed.

Get back a lost commit or a deleted branch
git reflog
git branch recovered <commit-sha>

reflog lists everywhere HEAD has been, even after a reset. Pick the commit and put a branch on it.

Squash my last few commits into one
git reset --soft HEAD~3
git commit -m "One clear message"

Combines the last three commits. For more control, use git rebase -i HEAD~3.

If they were pushed, you will need git push --force-with-lease.

Update my fork from the original repository
git remote add upstream https://github.com/OWNER/REPO.git
git fetch upstream
git rebase upstream/main
git push --force-with-lease

Adds the original repo once, then replays your work on top of its latest main.

Pull without creating a merge commit
git pull --rebase
git config --global pull.rebase true

Replays your local commits on top of the remote. The second line makes it the default.

Cancel a merge or rebase that went wrong
git merge --abort
git rebase --abort

Puts everything back to how it was before you started.

Copy one commit to another branch
git switch target-branch
git cherry-pick <commit-sha>

Applies that single commit on top of the current branch.

> .gitignore git commit -m "Stop tracking file"">Stop tracking a file but keep it on disk
git rm --cached path/to/file
echo "path/to/file" >> .gitignore
git commit -m "Stop tracking file"

Removes it from the repository, keeps your local copy, and ignores it from now on.

Remove a leaked password or key from historyRewrites or deletes
pip install git-filter-repo
git filter-repo --invert-paths --path path/to/secret-file
git push --force --all

First rotate the secret: anyone may already have it. Then remove it from every commit.

Change the remote URL
git remote set-url origin git@github.com:OWNER/REPO.git
git remote -v

Points origin somewhere new, for example from HTTPS to SSH.

See the history as a compact graph
git log --oneline --graph --decorate --all

One line per commit with branches drawn as a graph.

Find who changed a line
git blame -L 10,20 path/to/file

Shows the last commit and author for lines 10 to 20.

Find the commit that introduced a bug
git bisect start
git bisect bad
git bisect good <last-good-sha>
git bisect reset

Git checks out commits halfway between good and bad; you mark each one until it finds the culprit.

Tag a release
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0

An annotated tag with a message, pushed to the remote.

Undo a git pullRewrites or deletes
git reset --hard ORIG_HEAD

Goes back to where you were before the pull.

Clone only the latest version of one branch
git clone --depth 1 --branch main https://github.com/OWNER/REPO.git

Much faster for big repositories when you do not need the history.

Set my name and email for commits
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Drop --global to set them for this repository only.

Delete untracked filesRewrites or deletes
git clean -n -d
git clean -f -d

The first line previews what would be deleted. The second deletes it.

Resolve a conflict by taking one side
git checkout --theirs path/to/file
git checkout --ours path/to/file
git add path/to/file

During a merge, theirs is the incoming branch and ours is yours. During a rebase the two are swapped. Add the file to mark it resolved.

Undo a git commit --amend
git reset --soft HEAD@{1}

Returns to the commit as it was before the amend, keeping the new changes staged.

How it works

  • Plain wordsType it the way you would ask a friend. The list narrows as you type and opens the only match.
  • Safe by defaultAnything that rewrites history or deletes work is marked, and pushed-branch cases point to the safe alternative.
  • Every situation has a linkShare the exact fix with a teammate: each one has its own link.

Questions

What is the difference between reset and revert?

reset moves your branch back and can drop commits; revert adds a new commit that undoes an old one. Use revert on anything already pushed.

What does --force-with-lease do?

It force-pushes only if nobody else pushed in the meantime, so you do not overwrite a teammate's work.

More tools

Built something with it?

Show it in the server, get a code review, or ask the people who use these tools every day.

Join on Discord