Git Command Cheat Sheet
Nine scenarios from staging to undo, each with a ready-to-use example
69 commands
git initSetupCreate a repository in the current directory.
git init
git clone <url>SetupClone a remote repository.
git clone git@github.com:user/repo.git
git config --global user.name "Ada"SetupSet the committer name once for all repositories.
git config --global user.name "Ada"
git config --global user.email "ada@example.com"SetupSet the committer email shown in every commit.
git config --global user.email "ada@example.com"
git config --global alias.st statusSetupDefine a command alias to save typing.
git config --global alias.lg "log --oneline --graph"
git remote add origin <url>SetupAttach a remote URL to the local repository.
git remote add origin git@github.com:user/repo.git
git statusStage and commitShow the state of the working tree and index.
git status -sb
git add <file>Stage and commitStage changes of the given file.
git add src/app.vue
git add -pStage and commitStage selected hunks interactively, handy for splitting commits.
git add -p
git commit -m "msg"Stage and commitCommit what is staged.
git commit -m "fix: 修正登录跳转"
git commit --amendStage and commitAmend the most recent commit.
git commit --amend --no-edit
git restore --staged <file>Stage and commitUnstage a file while keeping the changes.
git restore --staged src/app.vue
git rm --cached <file>Stage and commitStop tracking a file while keeping it on disk.
git rm --cached .env
git mv <old> <new>Stage and commitRename a file and record it in Git.
git mv old.ts new.ts
git branchBranches and mergesList local branches; the starred one is current.
git branch -vv
git switch -c <name>Branches and mergesCreate and switch to a new branch.
git switch -c feature/login
git switch <name>Branches and mergesSwitch to an existing branch.
git switch main
git merge <branch>Branches and mergesMerge the given branch into the current one.
git merge feature/login
git merge --no-ff <branch>Branches and mergesDisable fast-forward so the merge commit is kept.
git merge --no-ff feature/login
git merge --squash <branch>Branches and mergesSquash all commits of a branch into one.
git merge --squash feature/login
git rebase <branch>Branches and mergesReplay your commits on top of another branch for a linear history.
git rebase main
git rebase -i HEAD~3Branches and mergesInteractively edit the last three commits: squash, reorder, reword.
git rebase -i HEAD~3
git rebase --onto <new> <old> <branch>Branches and mergesMove a range of commits onto a new base.
git rebase --onto main old-base topic
git cherry-pick <commit>Branches and mergesApply a single commit onto the current branch.
git cherry-pick a1b2c3d
git branch -d <name>Branches and mergesDelete a merged branch; -D forces deletion.
git branch -d feature/login
git restore <file>Undo and rollbackDiscard working tree changes to a file.
git restore src/app.vue
git reset --soft HEAD~1Undo and rollbackUndo the last commit but keep changes staged.
git reset --soft HEAD~1
git reset HEAD~1Undo and rollbackUndo the last commit and unstage the changes.
git reset HEAD~1
git reset --hard HEAD~1Undo and rollbackDiscard the last commit and all changes. Destructive.
git reset --hard HEAD~1
git revert <commit>Undo and rollbackCreate an inverse commit instead of rewriting history.
git revert a1b2c3d
git revert -m 1 <merge-commit>Undo and rollbackRevert a merge commit; -m picks the retained parent.
git revert -m 1 8f3c2a1
git checkout <commit> -- <file>Undo and rollbackRestore a single file from a past commit.
git checkout HEAD~2 -- config/nginx.conf
git reflogUndo and rollbackShow where HEAD has been; the way to recover lost commits.
git reflog
git clean -fdUndo and rollbackRemove untracked files and directories. Preview with -n first.
git clean -fdn
git stashStash and cleanStash the current changes to get a clean tree.
git stash push -m "wip: 登录页"
git stash listStash and cleanList all stash entries.
git stash list
git stash popStash and cleanRestore the latest stash and drop it.
git stash pop
git stash apply stash@{2}Stash and cleanApply a specific stash without dropping it.
git stash apply stash@{2}git stash dropStash and cleanDrop the latest stash.
git stash drop
git fetch --pruneRemotesFetch updates and prune deleted remote branches.
git fetch --all --prune
git pull --rebaseRemotesPull with rebase to avoid merge commits.
git pull --rebase
git push -u origin <branch>RemotesPush a new branch and set its upstream.
git push -u origin feature/login
git push --force-with-leaseRemotesForce push after a rebase, refusing if the remote moved.
git push --force-with-lease
git push origin --delete <branch>RemotesDelete a remote branch.
git push origin --delete feature/login
git remote -vRemotesShow remote repository URLs.
git remote -v
git remote set-url origin <url>RemotesChange the remote URL, for example when switching protocols.
git remote set-url origin git@github.com:user/repo.git
git log --oneline --graphHistoryOne-line graph of history.
git log --oneline --graph --decorate --all
git log -S "text"HistoryFind commits that added or removed a given string.
git log -S "checkToken" --oneline
git show <commit>HistoryShow a commit with its diff.
git show a1b2c3d --stat
git diffHistoryDiff between working tree and index.
git diff --stat
git diff --stagedHistoryDiff between the index and the last commit.
git diff --staged
git blame <file>HistoryShow the last author and commit for each line.
git blame -L 20,40 src/app.vue
git shortlog -snHistoryCount commits per author.
git shortlog -sn --no-merges
git bisect startHistoryBinary search for the commit that introduced a bug.
git bisect start # git bisect bad / git bisect good <commit>
git tag -a v1.0.0 -m "release"Tags and releaseCreate an annotated tag.
git tag -a v1.0.0 -m "first release"
git tagTags and releaseList all tags.
git tag -l "v1.*"
git push origin <tag>Tags and releasePush a single tag to the remote.
git push origin v1.0.0
git worktree add ../hotfix mainRecoveryCheck out extra working directories from one repository.
git worktree add ../repo-hotfix main
git submodule update --init --recursiveRecoveryInitialize and fetch submodules.
git submodule update --init --recursive
git archive --format=zip HEAD -o src.zipRecoveryExport a snapshot without the .git directory.
git archive --format=zip HEAD -o src.zip
git gcRecoveryGarbage-collect and compress the repository.
git gc --aggressive --prune=now
git fsckRecoveryVerify object integrity to spot corruption.
git fsck --full
git range-diffRecoveryCompare two versions of a rebased series.
git range-diff main old-topic topic
git format-patch <range>RecoveryExport commits as patch files.
git format-patch HEAD~3
git apply <patch>RecoveryApply a patch file without committing.
git apply --check fix.patch
git filter-repo --path <dir>RecoveryRewrite history to drop paths; needs git-filter-repo installed.
git filter-repo --path secrets/ --invert-paths
Something broken or missing?
Send feedback