Git Cheat Sheet
A complete reference of Git commands with examples and usage instructions. Find the command you need using the search bar or browse by category.
git init
SetupInitialize a new Git repository
Syntax:
git init [directory]
Examples:
git init
Initialize repository in current directorygit init my-project
Initialize repository in my-project directoryNotes:
Creates a .git subdirectory with repository files
git clone
SetupClone a repository into a new directory
Syntax:
git clone [url] [directory]
Examples:
git clone https://github.com/user/repo.git
Clone remote repositorygit clone https://github.com/user/repo.git my-folder
Clone into specific foldergit clone --depth=1 https://github.com/user/repo.git
Clone only the latest revisionNotes:
Creates a copy of a repository with full history
git config
SetupGet and set repository or global options
Syntax:
git config [--global] [name] [value]
Examples:
git config --global user.name "Your Name"
Set global usernamegit config --global user.email "email@example.com"
Set global emailgit config --list
List all configuration settingsNotes:
Use --global to apply settings for all repositories for the user
git help
SetupDisplay help information about Git commands
Syntax:
git help [command]
Examples:
git help
Show general helpgit help commit
Show help for commit commandgit help -a
List all available commandsgit help -g
List concept guidesNotes:
Opens help documentation in browser or terminal
git version
SetupDisplay the version of Git
Syntax:
git version
Examples:
git version
Show Git version informationgit --version
Alternative way to show versionNotes:
Useful for checking which Git version is installed
git add
BasicAdd file contents to the index (staging area)
Syntax:
git add [file|pattern]
Examples:
git add file.txt
Add a specific filegit add .
Add all new and modified filesgit add -p
Interactive add (choose chunks to stage)Notes:
Prepares content for the next commit
git commit
BasicRecord changes to the repository
Syntax:
git commit -m "[message]"
Examples:
git commit -m "Add feature X"
Commit with messagegit commit -a -m "Fix bugs"
Add all tracked files and commitgit commit --amend
Modify the last commitNotes:
Creates a snapshot of staged changes
git status
BasicShow the working tree status
Syntax:
git status [options]
Examples:
git status
Display full statusgit status -s
Display short statusNotes:
Shows which files are modified, staged, or untracked
git log
BasicShow commit logs
Syntax:
git log [options]
Examples:
git log
Show commit historygit log --oneline
Compact summary viewgit log --graph
Show ASCII graph of branch and merge historygit log -p
Show changes introduced by each commitNotes:
View the commit history
git rm
BasicRemove files from the working tree and index
Syntax:
git rm [file]
Examples:
git rm file.txt
Remove file from working tree and indexgit rm --cached file.txt
Remove from index only (keep in working tree)git rm -r directory/
Remove directory recursivelygit rm '*.log'
Remove all .log filesNotes:
Different from regular file deletion - stages the removal
git mv
BasicMove or rename files, directories, or symlinks
Syntax:
git mv [source] [destination]
Examples:
git mv old-name.txt new-name.txt
Rename a filegit mv file.txt directory/
Move file to directorygit mv old-dir/ new-dir/
Rename a directoryNotes:
Equivalent to mv + git rm + git add, but preserves history better
git branch
BranchingList, create, or delete branches
Syntax:
git branch [name] | [-d name]
Examples:
git branch
List local branchesgit branch -a
List all branches (local and remote)git branch feature
Create branch named 'feature'git branch -d feature
Delete branch 'feature' (if merged)git branch -D feature
Force delete branch 'feature'Notes:
Branches are lightweight pointers to commits
git checkout
BranchingSwitch branches or restore working tree files
Syntax:
git checkout [branch|commit] | [--] [file]
Examples:
git checkout feature
Switch to branch 'feature'git checkout -b feature
Create and switch to branch 'feature'git checkout -- file.txt
Discard changes to file.txtNotes:
Updates working directory to match selected branch or paths
git switch
BranchingSwitch branches (newer alternative to checkout)
Syntax:
git switch [branch]
Examples:
git switch feature
Switch to branch 'feature'git switch -c feature
Create and switch to branch 'feature'git switch -
Switch to previous branchNotes:
Modern replacement for git checkout for switching branches
git merge
BranchingJoin two or more development histories together
Syntax:
git merge [branch]
Examples:
git merge feature
Merge 'feature' branch into current branchgit merge --no-ff feature
Create a merge commit even if fast-forward is possiblegit merge --abort
Abort a merge in progressNotes:
Incorporates changes from the named branch into the current branch
git rebase
BranchingReapply commits on top of another base tip
Syntax:
git rebase [base]
Examples:
git rebase main
Rebase current branch onto 'main'git rebase -i HEAD~3
Interactive rebase for last 3 commitsgit rebase --abort
Abort a rebase in progressNotes:
Rewrites commit history to create a linear history
git remote
RemoteManage set of tracked repositories
Syntax:
git remote [add|remove|show|rename]
Examples:
git remote -v
List remote connections with URLsgit remote add origin https://github.com/user/repo.git
Add remote named 'origin'git remote show origin
Show information about remote 'origin'git remote rename old new
Rename a remoteNotes:
Defines connections to other repositories
git fetch
RemoteDownload objects and refs from another repository
Syntax:
git fetch [remote]
Examples:
git fetch origin
Fetch from remote 'origin'git fetch --all
Fetch from all remotesgit fetch origin branch
Fetch specific branch from originNotes:
Fetches branches and tags without merging
git pull
RemoteFetch from and integrate with another repository or branch
Syntax:
git pull [remote] [branch]
Examples:
git pull
Pull from tracked upstream branchgit pull origin main
Pull from 'main' branch of 'origin'git pull --rebase
Pull and rebase instead of mergeNotes:
Equivalent to git fetch followed by git merge
git push
RemoteUpdate remote refs along with associated objects
Syntax:
git push [remote] [branch]
Examples:
git push origin main
Push 'main' branch to remote 'origin'git push -u origin feature
Push and set upstream for 'feature' branchgit push --tags
Push all tags to remotegit push --force
Force push (use with caution)Notes:
Publishes local changes to remote repository
git diff
InspectionShow changes between commits, commit and working tree, etc
Syntax:
git diff [options] [commit] [--] [path]
Examples:
git diff
Show unstaged changesgit diff --staged
Show staged changesgit diff HEAD
Show all changes (staged and unstaged)git diff branch1..branch2
Show differences between two branchesNotes:
Displays file differences
git show
InspectionShow various types of objects
Syntax:
git show [object]
Examples:
git show
Show the last commitgit show abc123
Show commit with hash abc123git show HEAD~3
Show the third-last commitgit show master:file.txt
Show file.txt from master branchNotes:
Shows commit details and changes
git blame
InspectionShow what revision and author last modified each line of a file
Syntax:
git blame [file]
Examples:
git blame file.txt
Show who changed each line in file.txtgit blame -L 10,20 file.txt
Show blame for lines 10-20git blame -w file.txt
Ignore whitespace changesNotes:
Helps find who introduced changes to specific lines
git grep
InspectionSearch for patterns in tracked files
Syntax:
git grep [pattern] [commit]
Examples:
git grep "function"
Search for 'function' in all tracked filesgit grep -n "TODO"
Search with line numbersgit grep "pattern" HEAD~5
Search in files from 5 commits agogit grep -i "error"
Case-insensitive searchNotes:
Faster than regular grep as it only searches tracked files
git shortlog
InspectionSummarize git log output by author
Syntax:
git shortlog [options]
Examples:
git shortlog
Show commits grouped by authorgit shortlog -s
Show only commit counts per authorgit shortlog -n
Sort by number of commitsgit shortlog --since="1 month ago"
Show contributions from last monthNotes:
Useful for generating changelogs and contribution summaries
git ls-files
InspectionShow information about files in the index and working tree
Syntax:
git ls-files [options]
Examples:
git ls-files
List all tracked filesgit ls-files --others
List untracked filesgit ls-files --ignored
List ignored filesgit ls-files --stage
Show staged files with metadataNotes:
Useful for debugging what Git is tracking
git ls-tree
InspectionList the contents of a tree object
Syntax:
git ls-tree [tree-ish]
Examples:
git ls-tree HEAD
List files in the current commitgit ls-tree -r HEAD
Recursively list all filesgit ls-tree master:directory/
List files in specific directorygit ls-tree --name-only HEAD
Show only file namesNotes:
Shows the tree structure as Git sees it
git rev-parse
InspectionParse revision (or other objects) identifiers
Syntax:
git rev-parse [object]
Examples:
git rev-parse HEAD
Get the full SHA of HEADgit rev-parse --short HEAD
Get short SHA of HEADgit rev-parse --abbrev-ref HEAD
Get current branch namegit rev-parse HEAD~3
Get SHA of 3 commits agoNotes:
Useful in scripts for getting commit hashes and branch names
git describe
InspectionGive an object a human readable name based on an available ref
Syntax:
git describe [commit]
Examples:
git describe
Describe current commitgit describe --tags
Use any tag, not just annotated tagsgit describe --always
Show commit hash if no tag foundgit describe --abbrev=0
Show only the tag nameNotes:
Useful for generating version numbers from Git tags
git stash
AdvancedStash the changes in a dirty working directory away
Syntax:
git stash [push|pop|apply|list]
Examples:
git stash
Stash changesgit stash pop
Apply and remove the latest stashgit stash apply
Apply the latest stash without removing itgit stash list
List all stashesgit stash drop
Remove the latest stashNotes:
Temporarily stores modified, tracked files for later use
git tag
AdvancedCreate, list, delete or verify a tag object signed with GPG
Syntax:
git tag [name] [commit] | -d [name]
Examples:
git tag v1.0.0
Create a lightweight taggit tag -a v1.0.0 -m "Version 1.0.0"
Create an annotated taggit tag
List all tagsgit tag -d v1.0.0
Delete tag v1.0.0Notes:
Tags are used to mark specific points in history, usually for releases
git reset
AdvancedReset current HEAD to the specified state
Syntax:
git reset [--soft|--mixed|--hard] [commit]
Examples:
git reset HEAD~1
Undo the last commit, keep changes stagedgit reset --soft HEAD~1
Undo the last commit, keep changes stagedgit reset --mixed HEAD~1
Undo the last commit, unstage changesgit reset --hard HEAD~1
Undo the last commit, discard all changesNotes:
Changes where HEAD is pointing, with different effects on working directory and index
git revert
AdvancedCreate a new commit that undoes changes from a previous commit
Syntax:
git revert [commit]
Examples:
git revert HEAD
Revert the last commitgit revert abc123
Revert the commit with hash abc123git revert --no-commit HEAD
Revert but do not automatically commitNotes:
Creates a new commit that undoes changes, safer than reset for public branches
git cherry-pick
AdvancedApply the changes introduced by some existing commits
Syntax:
git cherry-pick [commit]
Examples:
git cherry-pick abc123
Apply commit abc123 to current branchgit cherry-pick abc123..def456
Apply a range of commitsgit cherry-pick --no-commit abc123
Apply changes without committingNotes:
Useful for moving specific commits between branches
git clean
AdvancedRemove untracked files from the working tree
Syntax:
git clean [-d] [-f]
Examples:
git clean -n
Show what would be removed (dry run)git clean -f
Remove untracked filesgit clean -df
Remove untracked files and directoriesgit clean -xf
Remove untracked and ignored filesNotes:
Permanently deletes untracked files, use with caution
git bisect
AdvancedUse binary search to find the commit that introduced a bug
Syntax:
git bisect [start|bad|good|reset]
Examples:
git bisect start
Start bisect sessiongit bisect bad
Mark current commit as badgit bisect good v1.0
Mark v1.0 as goodgit bisect reset
End bisect sessionNotes:
Helps find which commit introduced a bug by binary search
git archive
AdvancedCreate an archive of files from a named tree
Syntax:
git archive [format] [tree-ish]
Examples:
git archive --format=zip HEAD > archive.zip
Create zip archive of current commitgit archive --format=tar.gz --prefix=project/ HEAD
Create tar.gz with prefixgit archive v1.0.0 > release.tar
Archive specific taggit archive HEAD:src/ > src.tar
Archive specific directoryNotes:
Useful for creating release packages without .git directory
git submodule
AdvancedInitialize, update or inspect submodules
Syntax:
git submodule [command] [options]
Examples:
git submodule add https://github.com/user/repo.git path/to/submodule
Add a new submodulegit submodule init
Initialize submodule configurationgit submodule update
Update submodules to recorded commitsgit submodule status
Show status of submodulesgit submodule foreach git pull origin main
Run command in each submoduleNotes:
Allows including other Git repositories as subdirectories
Pro Git Tips
Everyday Git Workflow
git pull
- Update your local repositorygit checkout -b feature
- Create a new branch- Make your changes
git add .
- Stage your changesgit commit -m "Description"
- Commit changesgit push -u origin feature
- Push to remote
Git Best Practices
- Commit early and often
- Write meaningful commit messages
- Use branches for new features or bug fixes
- Pull before pushing to avoid conflicts
- Use
git diff
to review changes before committing - Set up
.gitignore
to exclude unwanted files