All cheat sheets

Git reference

Git Commands Cheat Sheet

Everyday and advanced Git commands for commits, branches, remotes, worktrees, recovery, configuration, and maintenance.

View source repository
121 command references13 topic sections

13 of 13 sections

Basic Commands

CommandDescription
git initInitialize a new Git repository.
git clone <repo>Clone a repository.
git statusShow working tree status.
git add <file>Stage a file.
git add .Stage all changes.
git add -pInteractively stage parts of files.
git commit -m "message"Commit staged changes.
git commit -a -m "message"Stage tracked files and commit.
git commit --amendModify the previous commit.
git commit --amend --no-editAdd staged changes to the previous commit without changing its message.
git logView commit history.
git log --onelineCompact commit history.
git log --graph --all --decorateVisualize the complete commit graph.
git diffShow unstaged changes.
git diff --cachedShow staged changes.
git diff HEADShow all changes since last commit.
git diff <commit1> <commit2>Compare two commits.
git shortlog -snShow commit counts per contributor.

Branching

CommandDescription
git branchList branches.
git branch -aList local and remote branches.
git branch <branch>Create a branch.
git checkout <branch>Switch branches.
git checkout -b <branch>Create and switch to a branch.
git switch <branch>Switch branches (modern alternative).
git switch -c <branch>Create and switch to a new branch.
git merge <branch>Merge a branch.
git merge --no-ff <branch>Force a merge commit.
git rebase <branch>Rebase onto another branch.
git rebase -i HEAD~5Interactive rebase of the last 5 commits.
git cherry-pick <commit>Apply a specific commit.
git branch -d <branch>Delete a merged branch.
git branch -D <branch>Force delete a branch.
git branch -m <new-name>Rename current branch.

Remote Repositories

CommandDescription
git remote -vShow remotes.
git remote add <name> <url>Add a remote.
git remote remove <name>Remove a remote.
git remote rename <old> <new>Rename a remote.
git remote show <remote>Show remote details.
git fetchFetch all remotes.
git fetch --all --pruneFetch all remotes and remove deleted remote branches.
git pullFetch and merge.
git pull --rebaseFetch and rebase instead of merge.
git pushPush current branch.
git push -u origin <branch>Push and set upstream branch.
git push --force-with-leaseSafer force push.
git push origin --delete <branch>Delete remote branch.

Git Worktrees

Git Worktrees allow you to work on multiple branches simultaneously without creating multiple clones.

CommandDescription
git worktree listList all worktrees.
git worktree add ../feature feature-branchCreate a worktree for an existing branch.
git worktree add -b feature ../featureCreate a new branch and worktree.
git worktree remove ../featureRemove a worktree.
git worktree pruneRemove stale worktree metadata.
git worktree lockPrevent a worktree from being pruned.
git worktree unlockUnlock a worktree.
git worktree repairRepair broken worktree metadata.

Example

git checkout main

git worktree add ../feature-login feature/login

cd ../feature-login

# main and feature/login are now open simultaneously

Benefits

  • Work on multiple branches simultaneously.
  • No need to stash changes when switching tasks.
  • Faster than cloning repositories.
  • Great for code reviews and hotfixes.

Stashing Changes

CommandDescription
git stashStash current changes.
git stash push -m "message"Stash with a message.
git stash -uInclude untracked files.
git stash listList stashes.
git stash showShow stash summary.
git stash show -pShow full stash diff.
git stash applyApply a stash.
git stash popApply and remove latest stash.
git stash dropDelete a stash.
git stash clearDelete all stashes.

Viewing Changes

CommandDescription
git show <commit>Show commit details.
git show HEADShow latest commit.
git blame <file>Show line authorship.
git log --statShow commit statistics.
git log --name-onlyShow modified files.
git log --follow <file>Track file history across renames.
git diff --word-diffShow word-level changes.
git whatchangedShow changed files for each commit.

Undoing Changes

CommandDescription
git restore <file>Restore a file.
git restore --staged <file>Unstage a file.
git checkout -- <file>Discard changes (legacy).
git reset <file>Unstage a file.
git reset --soft HEAD~1Undo last commit and keep changes staged.
git reset HEAD~1Undo last commit and keep changes unstaged.
git reset --hard HEADReset everything to the latest commit.
git revert <commit>Undo a commit with a new commit.
git clean -fdRemove untracked files and directories.
git clean -fdxRemove ignored files as well.

Tags

CommandDescription
git tagList tags.
git tag <tag>Create a lightweight tag.
git tag -a <tag> -m "message"Create an annotated tag.
git show <tag>Show tag details.
git push origin <tag>Push a tag.
git push --tagsPush all tags.
git tag -d <tag>Delete a local tag.
git push origin --delete <tag>Delete a remote tag.

Advanced Commands

CommandDescription
git reflogView all HEAD movements (excellent for recovering lost commits).
git bisect startStart binary search for a bug.
git bisect goodMark a commit as good.
git bisect badMark a commit as bad.
git bisect resetFinish bisect session.
git cherry -vShow commits not yet merged upstream.
git describe --tagsShow the nearest tag to the current commit.
git archiveCreate a ZIP/TAR archive of the repository.
git fsckVerify repository integrity.
git gcOptimize and clean the repository.
git maintenance runRun Git maintenance tasks.
git sparse-checkout initEnable sparse checkout.
git sparse-checkout set <dir>Check out only selected directories.
git submodule update --init --recursiveInitialize and update submodules.
git submodule foreach git pullPull updates in all submodules.

Configuration

CommandDescription
git config --global user.name "Name"Set your username.
git config --global user.email "email@example.com"Set your email.
git config --listDisplay current configuration.
git config --global alias.st statusCreate a Git alias.
git config --global core.editor "code --wait"Set default editor.
git config --global init.defaultBranch mainSet default branch name.

Helpful Aliases

AliasExpands To
git stgit status
git cogit checkout
git swgit switch
git brgit branch
git cigit commit
git lggit log --graph --decorate --oneline --all
git lastgit log -1 HEAD

Configure aliases

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.sw switch
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.last "log -1 HEAD"
git config --global alias.lg "log --graph --decorate --oneline --all"

Help

CommandDescription
git helpShow Git help.
git <command> --helpShow help for a specific command.
git config --helpConfiguration documentation.

Professional Tips

  • Use git pull --rebase to maintain a cleaner commit history.
  • Use git push --force-with-lease instead of --force.
  • Learn git reflog—it's one of Git's most valuable recovery tools.
  • Use Git Worktrees to work on multiple branches simultaneously.
  • Clean up your commit history with git rebase -i before opening a pull request.
  • Use git bisect to quickly identify the commit that introduced a bug.
  • Consider sparse checkout when working in very large monorepositories.