EasyDebug.NET

Git Command Cheat Sheet

Nine scenarios from staging to undo, each with a ready-to-use example

69 commands

git initSetup

Create a repository in the current directory.

git init
git clone <url>Setup

Clone a remote repository.

git clone git@github.com:user/repo.git
git config --global user.name "Ada"Setup

Set the committer name once for all repositories.

git config --global user.name "Ada"
git config --global user.email "ada@example.com"Setup

Set the committer email shown in every commit.

git config --global user.email "ada@example.com"
git config --global alias.st statusSetup

Define a command alias to save typing.

git config --global alias.lg "log --oneline --graph"
git remote add origin <url>Setup

Attach a remote URL to the local repository.

git remote add origin git@github.com:user/repo.git
git statusStage and commit

Show the state of the working tree and index.

git status -sb
git add <file>Stage and commit

Stage changes of the given file.

git add src/app.vue
git add -pStage and commit

Stage selected hunks interactively, handy for splitting commits.

git add -p
git commit -m "msg"Stage and commit

Commit what is staged.

git commit -m "fix: 修正登录跳转"
git commit --amendStage and commit

Amend the most recent commit.

git commit --amend --no-edit
git restore --staged <file>Stage and commit

Unstage a file while keeping the changes.

git restore --staged src/app.vue
git rm --cached <file>Stage and commit

Stop tracking a file while keeping it on disk.

git rm --cached .env
git mv <old> <new>Stage and commit

Rename a file and record it in Git.

git mv old.ts new.ts
git branchBranches and merges

List local branches; the starred one is current.

git branch -vv
git switch -c <name>Branches and merges

Create and switch to a new branch.

git switch -c feature/login
git switch <name>Branches and merges

Switch to an existing branch.

git switch main
git merge <branch>Branches and merges

Merge the given branch into the current one.

git merge feature/login
git merge --no-ff <branch>Branches and merges

Disable fast-forward so the merge commit is kept.

git merge --no-ff feature/login
git merge --squash <branch>Branches and merges

Squash all commits of a branch into one.

git merge --squash feature/login
git rebase <branch>Branches and merges

Replay your commits on top of another branch for a linear history.

git rebase main
git rebase -i HEAD~3Branches and merges

Interactively edit the last three commits: squash, reorder, reword.

git rebase -i HEAD~3
git rebase --onto <new> <old> <branch>Branches and merges

Move a range of commits onto a new base.

git rebase --onto main old-base topic
git cherry-pick <commit>Branches and merges

Apply a single commit onto the current branch.

git cherry-pick a1b2c3d
git branch -d <name>Branches and merges

Delete a merged branch; -D forces deletion.

git branch -d feature/login
git restore <file>Undo and rollback

Discard working tree changes to a file.

git restore src/app.vue
git reset --soft HEAD~1Undo and rollback

Undo the last commit but keep changes staged.

git reset --soft HEAD~1
git reset HEAD~1Undo and rollback

Undo the last commit and unstage the changes.

git reset HEAD~1
git reset --hard HEAD~1Undo and rollback

Discard the last commit and all changes. Destructive.

git reset --hard HEAD~1
git revert <commit>Undo and rollback

Create an inverse commit instead of rewriting history.

git revert a1b2c3d
git revert -m 1 <merge-commit>Undo and rollback

Revert a merge commit; -m picks the retained parent.

git revert -m 1 8f3c2a1
git checkout <commit> -- <file>Undo and rollback

Restore a single file from a past commit.

git checkout HEAD~2 -- config/nginx.conf
git reflogUndo and rollback

Show where HEAD has been; the way to recover lost commits.

git reflog
git clean -fdUndo and rollback

Remove untracked files and directories. Preview with -n first.

git clean -fdn
git stashStash and clean

Stash the current changes to get a clean tree.

git stash push -m "wip: 登录页"
git stash listStash and clean

List all stash entries.

git stash list
git stash popStash and clean

Restore the latest stash and drop it.

git stash pop
git stash apply stash@{2}Stash and clean

Apply a specific stash without dropping it.

git stash apply stash@{2}
git stash dropStash and clean

Drop the latest stash.

git stash drop
git fetch --pruneRemotes

Fetch updates and prune deleted remote branches.

git fetch --all --prune
git pull --rebaseRemotes

Pull with rebase to avoid merge commits.

git pull --rebase
git push -u origin <branch>Remotes

Push a new branch and set its upstream.

git push -u origin feature/login
git push --force-with-leaseRemotes

Force push after a rebase, refusing if the remote moved.

git push --force-with-lease
git push origin --delete <branch>Remotes

Delete a remote branch.

git push origin --delete feature/login
git remote -vRemotes

Show remote repository URLs.

git remote -v
git remote set-url origin <url>Remotes

Change the remote URL, for example when switching protocols.

git remote set-url origin git@github.com:user/repo.git
git push --tagsRemotes

Push all local tags; tags are not pushed by default.

git push origin --tags
git log --oneline --graphHistory

One-line graph of history.

git log --oneline --graph --decorate --all
git log -S "text"History

Find commits that added or removed a given string.

git log -S "checkToken" --oneline
git log --author=ada --since=2.weeksHistory

Filter commits by author and date range.

git log --author=ada --since=2.weeks --oneline
git show <commit>History

Show a commit with its diff.

git show a1b2c3d --stat
git diffHistory

Diff between working tree and index.

git diff --stat
git diff --stagedHistory

Diff between the index and the last commit.

git diff --staged
git blame <file>History

Show the last author and commit for each line.

git blame -L 20,40 src/app.vue
git shortlog -snHistory

Count commits per author.

git shortlog -sn --no-merges
git bisect startHistory

Binary 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 release

Create an annotated tag.

git tag -a v1.0.0 -m "first release"
git tagTags and release

List all tags.

git tag -l "v1.*"
git describe --tagsTags and release

Describe the current version from the nearest tag.

git describe --tags --always
git push origin <tag>Tags and release

Push a single tag to the remote.

git push origin v1.0.0
git worktree add ../hotfix mainRecovery

Check out extra working directories from one repository.

git worktree add ../repo-hotfix main
git submodule update --init --recursiveRecovery

Initialize and fetch submodules.

git submodule update --init --recursive
git archive --format=zip HEAD -o src.zipRecovery

Export a snapshot without the .git directory.

git archive --format=zip HEAD -o src.zip
git gcRecovery

Garbage-collect and compress the repository.

git gc --aggressive --prune=now
git fsckRecovery

Verify object integrity to spot corruption.

git fsck --full
git range-diffRecovery

Compare two versions of a rebased series.

git range-diff main old-topic topic
git format-patch <range>Recovery

Export commits as patch files.

git format-patch HEAD~3
git apply <patch>Recovery

Apply a patch file without committing.

git apply --check fix.patch
git filter-repo --path <dir>Recovery

Rewrite history to drop paths; needs git-filter-repo installed.

git filter-repo --path secrets/ --invert-paths

Something broken or missing?

Send feedback
Author's Blog
Share an idea