Git command finder
Describe what you want to do in Git — undo a commit, recover a branch, squash, remove a secret — and get the command and its risks.
Everything here runs in your browser. Nothing you type is sent to D's Labs or anyone else.
Undo the last commit but keep the changes
git reset --soft HEAD~1Moves the branch back one commit; the changes stay staged, ready to commit again.
⚠ Rewrites history if the commit was already pushed.
Throw away the last commit and its changes
git reset --hard HEAD~1Moves the branch back one commit and resets the files to match it.
⚠ The changes are gone from the working tree (git reflog can still find the commit for a while).
Undo a commit that is already pushed
git revert <commit>Adds a new commit that reverses the old one. History is kept, so it is safe on shared branches.
Change the last commit message
git commit --amend -m "New message"Replaces the last commit with one that has the new message.
⚠ Rewrites history if already pushed.
Add a forgotten file to the last commit
git add <file>git commit --amend --no-editFolds the staged file into the last commit and keeps its message.
⚠ Rewrites history if already pushed.
Unstage a file (keep my edits)
git restore --staged <file>Takes the file out of the next commit; your edits stay in the working tree.
Discard my changes to one file
git restore <file>Puts the file back to its last committed state.
⚠ Uncommitted edits to that file are lost.
Put my work aside and switch branches
git stash push -m "wip"git switch <branch>git stash popSaves uncommitted changes on a stack, cleans the tree, and brings them back later.
Start a new branch from here
git switch -c <name>Creates the branch at the current commit and moves to it.
Rename the current branch
git branch -m <new-name>Renames the local branch. Push the new name and delete the old one on the remote if it was pushed.
Delete a branch locally and on the remote
git branch -d <name>git push origin --delete <name>-d refuses if the branch is not merged; -D forces it.
Get back a commit or branch I lost
git refloggit switch -c rescue <sha>The reflog lists every position HEAD has been at; branch from the one you need.
Squash my last few commits into one
git reset --soft HEAD~3git commit -m "One commit"Moves back three commits keeping all changes staged, then commits them together. (git rebase -i does the same with more control.)
⚠ Rewrites history if already pushed.
Bring my branch up to date with main
git fetch origingit rebase origin/mainReplays your commits on top of the latest main. Use git merge origin/main instead to keep history as it happened.
⚠ Rebase rewrites your branch's commits; force-push with --force-with-lease afterwards.
Force-push without clobbering someone else's work
git push --force-with-leaseRefuses if the remote branch moved since you last fetched.
Copy one commit onto another branch
git switch <target>git cherry-pick <commit>Applies the changes of that one commit as a new commit.
Give up on a merge or rebase with conflicts
git merge --abortgit rebase --abortReturns to the state before the merge or rebase started.
Find which commit introduced a bug
git bisect startgit bisect badgit bisect good <old-commit>git bisect resetBinary search through history: mark each checkout good or bad until Git names the first bad commit.
See who last changed each line
git blame <file>Shows the commit and author for every line.
Find when some text was added or removed
git log -S "text" --onelineLists commits that changed how many times the text appears.
See a file as it was in an older commit
git show <commit>:<path>Prints the file's content at that commit without changing anything.
See what I am about to commit
git diff --stagedShows staged changes; plain git diff shows unstaged ones.
Stop tracking a file but keep it on disk
git rm --cached <file>echo '<file>' >> .gitignoreRemoves it from the repository from the next commit on; the file stays on your disk.
Delete untracked files
git clean -ngit clean -fd-n lists what would go; -fd removes untracked files and directories.
⚠ Removed files are not in Git, so they cannot be recovered.
Change where the remote points
git remote set-url origin <url>Updates the URL of origin, for example after a repository moves.
Tag a release
git tag -a v1.0.0 -m "Release 1.0.0"git push origin v1.0.0An annotated tag carries a message, author and date.
See the history as a graph
git log --oneline --graph --decorate --allOne line per commit, with branches drawn.
I committed a password or key
# 1. Revoke the secret first — it is already exposed.git rm --cached <file>git commit -m "Remove secret"Rotating the secret is the real fix: once pushed, assume it was copied. Rewriting history (git filter-repo) only helps if nobody fetched it.
⚠ Removing it from the latest commit does not remove it from history.
Rewriting history
Commands marked with a warning rewrite commits. That is fine on a branch only you use, and a problem on one others have pulled: they end up with commits that no longer exist upstream. When in doubt on a shared branch, prefer git revert, and remember git reflog — almost nothing committed is ever truly lost.