Git Cheat Sheet
Interactive reference for 73Git commands organized by category. Search, copy, and click “Build” to construct commands visually in the Git Command Builder.
Showing 73 of 73 commands
⚙️ Setup & Config
git initInitialize a new Git repository in the current directory
$ git init
Initialized empty Git repository in /project/.git/git clone <url>Clone a remote repository to your local machine
$ git clone https://github.com/user/repo.git
Cloning into 'repo'...git config user.name "<name>"Set your name for commits in this repository
$ git config user.name "Jane Doe"git config user.email "<email>"Set your email for commits in this repository
$ git config user.email "jane@example.com"git config --listList all Git configuration settings
$ git config --list
user.name=Jane Doe
user.email=jane@example.com
core.editor=vimgit config --global core.editor "<editor>"Set the default text editor for Git globally
$ git config --global core.editor "code --wait"git config --global init.defaultBranch <name>Set the default branch name for new repositories
$ git config --global init.defaultBranch maingit remote add <name> <url>Add a new remote repository connection
$ git remote add origin https://github.com/user/repo.git📦 Staging & Commits
git statusShow the working tree status — staged, unstaged, and untracked files
$ git status
On branch main
Changes not staged for commit:
modified: src/index.tsgit add -AStage all changes (new, modified, and deleted files)
$ git add -A
# Stages everything in the entire working treegit add -pInteractively stage hunks of changes — review each change before staging
$ git add -p
Stage this hunk [y,n,q,a,d,s,e,?]? ygit commit -m "<message>"Create a commit with a message describing the changes
$ git commit -m "feat: add user authentication"
[main a1b2c3d] feat: add user authenticationgit commit --amendModify the most recent commit (message or content)
$ git commit --amend
# Opens editor to modify the last commit messagegit diffShow unstaged changes between working directory and index
$ git diff
-const x = 1;
+const x = 2;git diff --stagedShow changes staged for the next commit
$ git diff --staged
# Shows only changes that have been git add'dgit reset HEAD <file>Unstage a file while keeping changes in the working directory
$ git reset HEAD src/index.ts
Unstaged changes after reset:
M src/index.tsgit rm --cached <file>Remove a file from staging/tracking without deleting it from disk
$ git rm --cached .env
rm '.env'
# File stays on disk but is untracked🌿 Branching
git branchList all local branches; the current branch is highlighted with *
$ git branch
* main
feature/login
bugfix/headergit branch <name>Create a new branch (does not switch to it)
$ git branch feature/auth
# Branch created but still on current branchgit branch -d <name>Delete a branch that has been fully merged
$ git branch -d feature/auth
Deleted branch feature/auth (was a1b2c3d).git branch -D <name>DestructiveForce-delete a branch regardless of merge status
$ git branch -D experiment
Deleted branch experiment (was e4f5g6h).git checkout <branch>Switch to an existing branch (legacy command)
$ git checkout main
Switched to branch 'main'git checkout -b <name>Create a new branch and switch to it immediately
$ git checkout -b feature/auth
Switched to a new branch 'feature/auth'git switch <branch>Switch to an existing branch (modern replacement for checkout)
$ git switch main
Switched to branch 'main'git switch -c <name>Create a new branch and switch to it (modern syntax)
$ git switch -c feature/auth
Switched to a new branch 'feature/auth'🔀 Merging & Rebasing
git merge <branch>Merge the specified branch into the current branch
$ git merge feature/auth
Merge made by the 'ort' strategy.
2 files changed, 45 insertions(+)git merge --no-ff <branch>Merge with a merge commit even if fast-forward is possible
$ git merge --no-ff feature/auth
Merge made by the 'ort' strategy.git merge --abortAbort a merge in progress and restore the pre-merge state
$ git merge --abort
# Working directory restored to state before mergegit rebase <branch>Replay current branch commits on top of the specified branch
$ git rebase main
Successfully rebased and updated refs/heads/feature.git rebase --onto <new> <old> <branch>Rebase a range of commits onto a new base
$ git rebase --onto main server client
# Replays client commits (since server) onto maingit rebase --abortAbort a rebase in progress and restore the original state
$ git rebase --abort
# Returns to the state before rebase startedgit cherry-pick <commit>Apply a specific commit from another branch to the current branch
$ git cherry-pick a1b2c3d
[main d4e5f6g] feat: add login
1 file changed, 12 insertions(+)git cherry-pick --no-commit <commit>Apply a commit's changes without creating a new commit
$ git cherry-pick --no-commit a1b2c3d
# Changes applied to working directory, not committed☁️ Remote
git remote -vList all remote connections with their URLs
$ git remote -v
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)git fetchDownload objects and refs from the default remote without merging
$ git fetch
remote: Enumerating objects: 5, done.
From https://github.com/user/repogit fetch --allFetch from all configured remote repositories
$ git fetch --all
Fetching origin
Fetching upstreamgit pullFetch from remote and merge into the current branch
$ git pull
Updating a1b2c3d..d4e5f6g
Fast-forward
2 files changed, 10 insertions(+)git pull --rebaseFetch and rebase (instead of merge) the current branch on top of remote
$ git pull --rebase
Successfully rebased and updated refs/heads/main.git pushUpload local commits to the default remote branch
$ git push
To https://github.com/user/repo.git
a1b2c3d..d4e5f6g main -> maingit push -u origin <branch>Push a branch and set the upstream tracking reference
$ git push -u origin feature/auth
Branch 'feature/auth' set up to track 'origin/feature/auth'.git push --force-with-leaseDestructiveForce-push only if the remote branch matches your local expectation
$ git push --force-with-lease
# Safer than --force: fails if someone else pushed📋 Stash
git stashTemporarily save uncommitted changes and clean the working directory
$ git stash
Saved working directory and index state WIP on main: a1b2c3dgit stash save "<message>"Stash changes with a descriptive message for easy identification
$ git stash save "WIP: auth refactor"
Saved working directory and index state On main: WIP: auth refactorgit stash listList all stashed change sets
$ git stash list
stash@{0}: WIP on main: a1b2c3d
stash@{1}: On main: WIP: auth refactorgit stash popApply the most recent stash and remove it from the stash list
$ git stash pop
On branch main
Changes not staged for commit:
modified: src/index.ts
Dropped refs/stash@{0}git stash applyApply the most recent stash but keep it in the stash list
$ git stash apply
# Changes restored, stash entry preservedgit stash dropRemove the most recent stash entry without applying it
$ git stash drop
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8)🔍 Inspection & Comparison
git logShow the commit history for the current branch
$ git log
commit a1b2c3d (HEAD -> main)
Author: Jane Doe
Date: Mon Mar 19
feat: add authgit log --oneline --graphCompact commit history with ASCII branch graph
$ git log --oneline --graph
* d4e5f6g (HEAD -> main) merge feature
|\
| * a1b2c3d feat: add login
|/
* 7h8i9j0 initial commitgit log -pShow commit history with the full diff for each commit
$ git log -p
commit a1b2c3d
--- a/index.ts
+++ b/index.ts
+const auth = true;git show <commit>Display details and diff of a specific commit
$ git show a1b2c3d
commit a1b2c3d
Author: Jane Doe
feat: add auth
diff --git ...git diff <branch1>..<branch2>Show differences between two branches
$ git diff main..feature/auth
-old code
+new codegit blame <file>Show who last modified each line of a file and when
$ git blame src/index.ts
a1b2c3d (Jane 2026-03-19 10:30) const app = express();git reflogShow a log of all reference updates (branches, HEAD moves, resets)
$ git reflog
a1b2c3d HEAD@{0}: commit: feat: add auth
7h8i9j0 HEAD@{1}: checkout: moving to maingit shortlog -snSummarize commits by author, sorted by number of contributions
$ git shortlog -sn
42 Jane Doe
28 John Smith
7 Bot↩️ Undo & Recovery
git reset --soft HEAD~1Undo the last commit but keep all changes staged
$ git reset --soft HEAD~1
# Commit undone, changes remain in staging areagit reset --mixed HEAD~1Undo the last commit and unstage changes (default reset mode)
$ git reset --mixed HEAD~1
Unstaged changes after reset:
M src/index.tsgit reset --hard HEAD~1DestructiveUndo the last commit and discard all changes permanently
$ git reset --hard HEAD~1
HEAD is now at 7h8i9j0 previous commitgit revert <commit>Create a new commit that undoes the specified commit's changes
$ git revert a1b2c3d
[main e5f6g7h] Revert "feat: add auth"git checkout -- <file>DestructiveDiscard changes in a file, restoring it to the last committed state
$ git checkout -- src/index.ts
# File restored to last committed versiongit restore <file>DestructiveDiscard working directory changes (modern replacement for checkout --)
$ git restore src/index.ts
# File restored to last committed versiongit clean -fdDestructiveRemove all untracked files and directories from the working tree
$ git clean -fd
Removing build/
Removing temp.loggit reflog + git reset --hard <hash>DestructiveRecover a lost commit by finding it in reflog and resetting to it
$ git reflog
a1b2c3d HEAD@{3}: commit: lost work
$ git reset --hard a1b2c3d
HEAD is now at a1b2c3d lost work🏷️ Tags
git tagList all tags in the repository
$ git tag
v1.0.0
v1.1.0
v2.0.0git tag -a <name> -m "<message>"Create an annotated tag with a message at the current commit
$ git tag -a v1.0.0 -m "Release 1.0.0"
# Annotated tag created with metadatagit push --tagsPush all local tags to the remote repository
$ git push --tags
* [new tag] v1.0.0 -> v1.0.0git tag -d <name>Delete a tag locally
$ git tag -d v1.0.0
Deleted tag 'v1.0.0' (was a1b2c3d)git push --delete origin <tag>DestructiveDelete a tag from the remote repository
$ git push --delete origin v1.0.0
To https://github.com/user/repo.git
- [deleted] v1.0.0📦 Submodules
git submodule add <url> <path>Add a Git repository as a submodule at the specified path
$ git submodule add https://github.com/lib/utils.git vendor/utils
Cloning into 'vendor/utils'...git submodule initInitialize submodule configuration from .gitmodules after cloning
$ git submodule init
Submodule 'vendor/utils' registered for path 'vendor/utils'git submodule updateFetch and checkout the committed submodule versions
$ git submodule update
Cloning into 'vendor/utils'...
Submodule path 'vendor/utils': checked out 'a1b2c3d'git submodule update --remoteUpdate submodules to the latest commit on their tracked remote branch
$ git submodule update --remote
Submodule path 'vendor/utils': checked out 'd4e5f6g'Related Tools
Git Command Builder
Build Git commands visually with an interactive form-based interface
Git Diff Viewer
View diffs with syntax highlighting and side-by-side comparison
Diff Checker
Compare any two text files and see differences highlighted
.gitignore Generator
Generate .gitignore files for any language, framework, or IDE