Git Cheatsheet
Searchable reference of 106 essential git commands organized by workflow. Find what you need fast.
106 commands found
Setup & Config
Set global username
git config --global user.name "Your Name"Set global email
git config --global user.email "you@example.com"List all config settings
git config --listSet default branch name
git config --global init.defaultBranch mainSet editor for commit messages
git config --global core.editor "code --wait"Enable colored output
git config --global color.ui autoSet credential caching
git config --global credential.helper cacheCreate a global alias
git config --global alias.co checkoutCreating & Cloning
Initialize a new repository
git initInitialize a bare repository
git init --bareClone a repository
git clone <url>Clone into a specific folder
git clone <url> <directory>Shallow clone (latest commit only)
git clone --depth 1 <url>Clone a specific branch
git clone -b <branch> <url>Staging & Committing
Stage all changes
git add .Stage all tracked and untracked files
git add -AInteractively stage hunks
git add -pStage a specific file
git add <file>Commit with a message
git commit -m "message"Amend last commit message
git commit --amendAmend commit without changing message
git commit --amend --no-editShow working tree status
git statusShow short status
git status -sShow unstaged changes
git diffShow staged changes
git diff --stagedDiff against last commit
git diff HEAD~1Branching
List local branches
git branchList all branches (local + remote)
git branch -aCreate a new branch
git branch <branch-name>Delete a branch (safe)
git branch -d <branch-name>Force delete a branch
git branch -D <branch-name>Rename current branch
git branch -m <new-name>Switch to a branch
git checkout <branch-name>Create and switch to new branch
git checkout -b <branch-name>Switch to previous branch
git checkout -Switch branch (modern)
git switch <branch-name>Create and switch branch (modern)
git switch -c <branch-name>Merging & Rebasing
Merge a branch into current
git merge <branch>Merge with no fast-forward
git merge --no-ff <branch>Abort a merge in progress
git merge --abortSquash merge (combine commits)
git merge --squash <branch>Rebase current branch onto another
git rebase <branch>Interactive rebase (last N commits)
git rebase -i HEAD~<n>Rebase onto a specific base
git rebase --onto <new-base> <old-base> <branch>Abort a rebase in progress
git rebase --abortContinue rebase after resolving conflicts
git rebase --continueCherry-pick a commit
git cherry-pick <commit-hash>Undoing Changes
Soft reset (keep changes staged)
git reset --soft HEAD~1Mixed reset (unstage changes)
git reset --mixed HEAD~1Hard reset (discard all changes)
git reset --hard HEAD~1Reset to specific commit
git reset --hard <commit-hash>Revert a commit (creates new commit)
git revert <commit-hash>Restore a file to last commit
git restore <file>Unstage a file
git restore --staged <file>Discard all unstaged changes
git checkout -- .Remote & Collaboration
Add a remote repository
git remote add origin <url>List remotes with URLs
git remote -vRemove a remote
git remote remove <name>Change remote URL
git remote set-url origin <new-url>Fetch from all remotes
git fetch --allFetch and prune deleted branches
git fetch --prunePull latest changes
git pullPull with rebase
git pull --rebasePush and set upstream
git push -u origin <branch>Force push safely
git push --force-with-leaseDelete a remote branch
git push --delete origin <branch>Push all branches
git push --all originStashing
Stash current changes
git stashStash with a message
git stash push -m "description"Apply latest stash
git stash popList all stashes
git stash listShow stash contents
git stash show -pApply specific stash without removing
git stash apply stash@{0}Drop a specific stash
git stash drop stash@{0}Clear all stashes
git stash clearViewing History & Diffs
View commit log
git logCompact one-line log
git log --onelineLog with graph visualization
git log --oneline --graph --allLog with diff patches
git log -pCommits since a date
git log --since="2024-01-01"Commits by a specific author
git log --author="name"Show a specific commit
git show <commit-hash>Show who changed each line
git blame <file>Summary of contributors
git shortlog -snSearch commit messages
git log --grep="keyword"Tagging
List all tags
git tagCreate an annotated tag
git tag -a v1.0.0 -m "Release v1.0.0"Create a lightweight tag
git tag v1.0.0Delete a local tag
git tag -d v1.0.0Push all tags to remote
git push --tagsPush a specific tag
git push origin v1.0.0Delete a remote tag
git push --delete origin v1.0.0Describe current commit with tags
git describe --tagsAdvanced & Cleanup
Remove untracked files and dirs
git clean -fdDry-run clean (preview)
git clean -nGarbage collect loose objects
git gcView reflog (history of HEAD)
git reflogStart binary search for a bug
git bisect startMark current commit as bad
git bisect badMark current commit as good
git bisect good <commit>End bisect session
git bisect resetAdd a submodule
git submodule add <url> <path>Initialize submodules after clone
git submodule update --init --recursiveAdd a worktree
git worktree add <path> <branch>List worktrees
git worktree listRemove a worktree
git worktree remove <path>Why Use a Git Cheatsheet?
Git is the foundation of modern software development, used by virtually every engineering team for version control, collaboration, and code deployment. While Git's distributed architecture is powerful, its command-line interface has hundreds of commands and flags that even experienced developers need to reference regularly. A well-organized cheatsheet helps you quickly find the right command for branching, merging, rebasing, stashing, and resolving conflicts.
This git cheatsheet is organized by workflow rather than alphabetically, making it faster to find commands when you know what you want to do but not the exact syntax. DevOps engineers use git commands throughout CI/CD pipelines, infrastructure-as-code workflows, and GitOps deployment patterns. Whether you are resolving a merge conflict during code review, cherry-picking a hotfix to a release branch, or bisecting to find a regression, having these commands at your fingertips accelerates your entire development workflow.
Frequently Asked Questions
How to undo the last git commit?
Use git reset --soft HEAD~1 to undo the commit but keep your changes staged, or git reset --mixed HEAD~1 to undo the commit and unstage changes (files remain modified). Use git reset --hard HEAD~1 to completely discard the commit and all changes (destructive). If the commit is already pushed, use git revert HEAD to create a new commit that undoes the changes safely.
What is the difference between git merge and rebase?
git merge creates a new merge commit that combines two branch histories, preserving the complete history. git rebase rewrites commits on top of another branch, creating a linear history without merge commits. Rebase produces a cleaner log but rewrites history (never rebase shared/pushed branches). Many teams use rebase for local feature branches and merge for integrating into main.
How to delete a remote branch?
Use git push origin --delete branch-name to delete a remote branch. To clean up your local references to deleted remote branches, run git fetch --prune. To delete a local branch, use git branch -d branch-name (safe, only if merged) or git branch -D branch-name (force delete regardless of merge status). Always verify you are not deleting a branch that others depend on.