Set your name and email
git config --global user.name <name>git config --global user.email <email>Sets the author recorded on every commit you make on this machine. Drop --global inside a repository to override it for that repository only.
Describe what you want to do and find the Git command for it, with an explanation and a warning when the command rewrites history.
Steps for using the Git Command Helper.
<branch> or <commit> in the small inputs on the card. The command updates live; branch and tag names are checked against Git's naming rules and values are shell-quoted when needed.Search for a task
undo last commitUndo the last commit but keep the changes staged
git reset --soft HEAD~1
Undo the last commit and unstage its changes
git reset HEAD~1
Delete the last commit and its changes
git reset --hard HEAD~1git add without losing work.git bisect, git remote set-url or git push origin --delete.Git has several commands that look similar but behave very differently, and the right choice usually depends on one question: have the commits been pushed? Commands like git commit --amend, git reset, git rebase and squashing rewrite commits — they create new commit IDs. That is harmless on a private branch but causes diverged histories for anyone who already pulled the old commits. For commits on a shared branch, git revert is the safe way to undo, because it adds a new commit instead of removing old ones.
Modern Git (2.23+) split the overloaded git checkout into two clearer commands: git switch changes branches (git switch -c creates one) and git restore changes files (git restore --staged unstages, git restore <file> discards edits). The catalog uses these where they apply, and mentions the older equivalents in the explanations so you recognise them in existing documentation.
The three modes of git reset are a frequent source of lost work. --soft moves the branch but keeps changes staged, the default --mixed keeps them unstaged in your working tree, and --hard discards them. Committed work is rarely truly lost: the reflog records every position HEAD has had for about 90 days, so git reflog followed by git switch -c <new-branch> <commit> or git reset --hard HEAD@{1} can recover from most mistakes. Uncommitted changes and untracked files deleted by git clean are not recorded anywhere, which is why those entries carry a warning.
When you must overwrite a remote branch after rewriting history, use git push --force-with-lease rather than --force: it refuses to push if the remote has commits you have not seen, so you cannot silently delete a colleague's work. Placeholders filled in on this page are validated — branch and tag names follow the rules of git check-ref-format (no spaces, no .., no leading -, no ~ ^ : ? * [ \, no .lock suffix) — and any value with special characters is wrapped in single quotes so it is passed to Git as one argument.
git help <command> for every option.git switch, git restore); git init -b needs 2.28+.cmd.exe, use double quotes instead of single quotes.Run git reset --soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them unstaged. If the commit was already pushed to a shared branch, use git revert <commit> instead.
git reset moves the branch pointer backwards, removing commits from the branch history. git revert creates a new commit that undoes an earlier one, leaving history intact — the safe choice for commits others already have.
Yes, on shared branches: it replaces the remote history with yours. Prefer git push --force-with-lease, which fails if someone else pushed in the meantime, and avoid force-pushing to main without agreement.
Usually. git branch -D prints the commit hash it deleted, and git reflog lists recent HEAD positions. Recreate the branch with git branch <branch> <commit>.
Git reference names cannot contain spaces, .., ~, ^, :, ?, *, [, backslashes or control characters, cannot start with - or ., and cannot end with /, . or .lock. Use hyphens or slashes instead, e.g. feature/new-login.
59 commands
git config --global user.name <name>git config --global user.email <email>Sets the author recorded on every commit you make on this machine. Drop --global inside a repository to override it for that repository only.
git init -b mainTurns the current directory into a Git repository with main as the initial branch (-b needs Git 2.28 or newer).
git clone <url>Downloads the repository with its full history into a new folder named after the repository and checks out the default branch.
git clone --depth 1 <url>Fetches only the most recent commit, which is much faster for large repositories and CI jobs. Run git fetch --unshallow later if you need the full history.
git config --global init.defaultBranch mainMakes git init create main instead of master in every new repository.
git switch -c <new-branch>Creates the branch from your current commit and switches to it. Equivalent to the older git checkout -b.
git switch <branch>Checks out the branch. If only origin/<branch> exists, Git creates a local tracking branch for it automatically.
git branch -agit branch -vv-a shows local and remote-tracking branches; -vv shows each local branch's upstream and whether it is ahead or behind.
git branch -m <new-branch>Renames the branch you are on. If it was already pushed, push the new name with git push -u origin <new-branch> and delete the old remote branch.
git branch -m <old-branch> <new-branch>Renames a branch you are not currently on. Its reflog is renamed too.
Careful: git branch -D deletes unmerged work. The commits stay recoverable through git reflog for a while (90 days by default), but are otherwise lost.
git branch -d <branch>Deletes the branch only if it has been merged. Use -D to force-delete an unmerged branch.
Careful: Removes the branch for everyone using the remote. Make sure it is merged or no longer needed.
git push origin --delete <branch>Removes the branch from the remote. Other clones keep their remote-tracking ref until they run git fetch --prune.
git switch -c <new-branch> <commit>Starts a new branch at any commit, tag or other branch instead of your current position.
git add -Agit commit -m <message>git add -A stages new, modified and deleted files across the whole working tree; the commit records them with your message.
git add -p <file>Walks through each change (hunk) and asks whether to stage it — useful for splitting unrelated edits into separate commits.
Careful: Rewrites commit history. Only do this on commits you have not pushed, or on a branch nobody else uses — otherwise collaborators end up with diverged histories.
git commit --amend -m <message>Replaces the most recent commit with one that has the new message (and anything currently staged).
Careful: Rewrites commit history. Only do this on commits you have not pushed, or on a branch nobody else uses — otherwise collaborators end up with diverged histories.
git add <file>git commit --amend --no-editStages the file and folds it into the previous commit while keeping its message.
git commit --allow-empty -m <message>Records a commit with no file changes, which is a common way to start a new CI run without touching code.
git restore --staged <file>Removes the file from the staging area. Your edits stay in the working tree. Older equivalent: git reset HEAD <file>.
Careful: Uncommitted changes in the file are discarded permanently — Git has no copy of them.
git restore <file>Restores the file to its last staged (or committed) version.
Careful: Rewrites commit history. Only do this on commits you have not pushed, or on a branch nobody else uses — otherwise collaborators end up with diverged histories.
git reset --soft HEAD~1Moves the branch back one commit; the commit's changes stay staged so you can edit and recommit them.
Careful: Rewrites commit history. Only do this on commits you have not pushed, or on a branch nobody else uses — otherwise collaborators end up with diverged histories.
git reset HEAD~1The default (mixed) reset moves the branch back one commit and leaves the changes in your working tree, unstaged.
Careful: Discards uncommitted work permanently and rewrites history. The removed commit can be recovered with git reflog for a while; uncommitted changes cannot.
git reset --hard HEAD~1Moves the branch back one commit and resets the working tree to match, deleting the commit's changes and any uncommitted work.
git revert <commit>Creates a new commit that applies the inverse of the given commit. History is not rewritten, so it is safe on shared branches. For a merge commit add -m 1.
Careful: Deletes local commits that were not pushed and all uncommitted changes. Consider git branch backup first.
git fetch origingit reset --hard origin/<branch>Throws away local commits and changes on the current branch and points it at the remote's version.
Careful: Overwrites uncommitted changes to that file.
git restore --source <commit> <file>Overwrites the file in your working tree with its content at the given commit. Add --staged --worktree to stage it too.
git log --oneline --graph --decorate --allOne line per commit with an ASCII graph of branches and merges, including all branches and tags.
git log --follow -p -- <file>Lists every commit that changed the file, with diffs. --follow continues through renames.
git blame <file>Annotates each line with the commit, author and date that last modified it. Add -w to ignore whitespace changes.
git log --grep=<text>git log -S <text> --oneline--grep searches commit messages; -S (the pickaxe) finds commits that added or removed the given text in the code.
git diffgit diff --stagedgit diff shows unstaged changes; --staged shows what the next commit will contain.
git log --oneline <other-branch>..<branch>git diff <other-branch>...<branch>The first command lists commits reachable from the branch but not from the other; the three-dot diff shows changes since the branches diverged.
git bisect startgit bisect badgit bisect good <commit>git bisect resetBinary-searches history: mark the current commit bad and a known-good commit, then test each commit Git checks out and answer git bisect good or git bisect bad until it names the culprit. git bisect reset returns you to where you started.
git remote -vLists each remote with its fetch and push URLs.
git remote add <remote> <url>Registers another repository under a short name. A common pattern for forks is adding the original as upstream.
git remote set-url <remote> <url>Points an existing remote at a new URL — needed after a repository moves or when switching from HTTPS to SSH.
git push -u origin <branch>Pushes the branch and remembers origin/<branch> as its upstream, so later git push and git pull need no arguments.
git fetch --pruneDownloads new commits and deletes remote-tracking refs (like origin/old-feature) whose branch was deleted on the remote.
git pull --rebaseFetches and replays your unpushed local commits on top of the remote branch, avoiding a merge commit. Make it the default with git config --global pull.rebase true.
Careful: Replaces the remote branch's history. Never force-push shared branches like main without agreement from your team.
git push --force-with-leaseOverwrites the remote branch, but refuses if someone else pushed commits you have not fetched. Prefer it over --force.
git stash push -m <message>git stash push -u -m <message>Saves uncommitted changes and cleans the working tree. -u also stashes untracked files.
git stash listgit stash apply <stash>git stash popapply re-applies a stash and keeps it; pop applies the most recent stash and removes it if there are no conflicts.
Careful: Dropped stashes are not in the reflog. They can sometimes be found with git fsck, but treat this as permanent.
git stash drop <stash>Removes one stash entry. git stash clear removes all of them.
git merge --no-ff <branch>Merges the branch and always records a merge commit, keeping the feature visible as a unit in history.
Careful: Rewrites the branch's commits. Do not rebase a branch others have based work on; afterwards you will need git push --force-with-lease.
git fetch origingit rebase origin/mainReplays your branch's commits on top of the current origin/main, giving a linear history. Resolve conflicts, git add them and run git rebase --continue.
Careful: Rewrites commit history. Only do this on commits you have not pushed, or on a branch nobody else uses — otherwise collaborators end up with diverged histories.
git rebase -i HEAD~<n>Opens an editor listing the last N commits; change pick to squash (or fixup) on every line except the first. Non-interactive alternative: git reset --soft HEAD~<n> then commit.
git merge --abortgit rebase --abortReturns the branch to the state before the merge or rebase started.
git cherry-pick <commit>Applies the changes of one commit on top of the current branch as a new commit. Add -x to record the original commit hash in the message.
Careful: Discards the other side's changes to that file.
git checkout --theirs -- <file>git add <file>During a merge, --theirs takes the incoming branch's version and --ours keeps yours. During a rebase the meanings are swapped.
git tag -a <tag> -m <message>git push origin <tag>Annotated tags store the tagger, date and message — use them for releases. Tags are not pushed by git push unless you push them explicitly.
git tag -lgit tag -l "v1.*"Lists tags alphabetically; pass a pattern to filter. git describe --tags shows the closest tag to the current commit.
Careful: Anyone who already fetched the tag keeps it; re-using a tag name for a different commit confuses consumers.
git tag -d <tag>git push origin --delete <tag>Removes the tag from your repository and from the remote.
Careful: Untracked files are deleted permanently — they are not in Git, so they cannot be recovered. Always run the dry run first.
git clean -ndgit clean -fd-n is a dry run that lists what would be deleted; -f deletes, -d includes directories. Add -x to also delete ignored files.
git fetch --prunegit branch --merged maingit branch -d <branch>Lists local branches fully merged into main; delete each with -d, which refuses to delete unmerged work.
git rm --cached <file>Removes the file from the index so the next commit deletes it from the repository, while leaving it on disk. Add it to .gitignore so it is not re-added.
git refloggit switch -c <new-branch> <commit>The reflog records every position HEAD has had. Find the hash of the lost commit and create a branch at it.
git refloggit branch <branch> <commit>Look up the last commit of the deleted branch in the reflog (or in the output of git branch -D, which prints it) and recreate the branch there.
Careful: reset --hard discards uncommitted changes in your working tree.
git refloggit reset --hard HEAD@{1}HEAD@{1} is where HEAD was before the last operation. Check the reflog first to pick the right entry.
git fsck --no-reflog --unreachable | grep commitgit stash apply <commit>Dropped stashes remain as unreachable commits until garbage collection. Inspect candidates with git show <commit> and apply the right one.