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.
Link to thisUndo 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.
Link to thisUndo 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.
Link to thisChange 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.
Link to thisAdd 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.
Link to thisUnstage 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.
Link to thisThrow away my changes to a fileRewrites or deletes
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.
Link to thisMove my uncommitted changes to a new branch
git switch -c new-branch
Uncommitted changes come with you to the new branch automatically.
Link to thisCreate a branch and switch to it
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.
Link to thisDelete 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.
Link to thisPut 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.
Link to thisSee what I changed
git diff git diff --staged
The first shows unstaged changes, the second what is about to be committed.
Link to thisGet 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.
Link to thisSquash 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.
Link to thisUpdate 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.
Link to thisPull 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.
Link to thisCancel a merge or rebase that went wrong
git merge --abort git rebase --abort
Puts everything back to how it was before you started.
Link to thisCopy one commit to another branch
git switch target-branch git cherry-pick <commit-sha>
Applies that single commit on top of the current branch.
Link to thisStop 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.
Link to thisRemove 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.
Link to thisChange 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.
Link to thisSee the history as a compact graph
git log --oneline --graph --decorate --all
One line per commit with branches drawn as a graph.
Link to thisFind who changed a line
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.
Link to thisTag 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.
Link to thisUndo a git pullRewrites or deletes
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.
Link to thisSet 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.
Link to thisDelete untracked filesRewrites or deletes
git clean -n -d git clean -f -d
The first line previews what would be deleted. The second deletes it.
Link to thisResolve 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.
Link to thisUndo a git commit --amend
git reset --soft HEAD@{1}
Returns to the commit as it was before the amend, keeping the new changes staged.
Link to thisHow 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.
online now · members