Git Commands
Searchable Git commands cheat sheet organized by category. Covers basics, branching, remote operations, and undo commands with one-click copy.
Last updated:
How to Use
Expand how to useCollapse how to use
- 1
Search for commands
Enter a command name in the search field, or filter by category (Basic, Branching, etc.) to find the command you need.
- 2
Review options and examples
Check the command description, common options, and practical usage examples.
- 3
Copy commands
Click on an example command to copy it to your clipboard for immediate use in your terminal.
git addAdd files to the staging area
Common Options
- .: Add all changes in current directory
- -A: Add all changes including deletions
- -p: Interactively select changes to add
- -u: Add only tracked file changes
Examples
git add .Stage all changesgit add -pInteractively select changes to stagegit add file.txtStage a specific filegit commitRecord changes to the repository
Common Options
- -m: Specify commit message
- --amend: Modify the last commit
- -a: Auto-stage tracked files and commit
- --no-verify: Skip pre-commit hooks
Examples
git commit -m "feat: add new feature"Commit with a messagegit commit --amendAmend the last commitgit commit -am "fix: bug fix"Auto-stage and commitgit statusShow working tree status
Common Options
- -s: Short format output
- -b: Show branch info
- --porcelain: Machine-readable output
Examples
git statusShow current statusgit status -sbShort format with branch infogit diffShow changes between commits
Common Options
- --staged: Show staged changes
- --cached: Same as --staged
- --stat: Show diffstat
- --name-only: Show only file names
Examples
git diffShow unstaged changesgit diff --stagedShow staged changesgit diff HEAD~1Compare with previous commitgit logShow commit history
Common Options
- --oneline: One line per commit
- --graph: Show branch graph
- -n: Show last n commits
- -p: Show patches
Examples
git log --onelineShow compact historygit log --oneline --graphShow history with graphgit log -5Show last 5 commitsgit pushUpdate remote refs
Common Options
- -u: Set upstream branch
- --force: Force push (caution)
- --force-with-lease: Safer force push
- --tags: Push tags as well
Examples
git push origin mainPush main to origingit push -u origin featurePush and set upstreamgit push --force-with-leaseSafe force pushgit pullFetch and integrate remote changes
Common Options
- --rebase: Rebase instead of merge
- --no-rebase: Merge (default)
- --ff-only: Fast-forward only
Examples
git pullFetch and mergegit pull --rebaseFetch and rebasegit pull origin mainPull from origin maingit fetchDownload objects from remote (no merge)
Common Options
- --all: Fetch from all remotes
- --prune: Remove deleted remote branches
- --tags: Fetch tags
Examples
git fetch originFetch from origingit fetch --allFetch from all remotesgit fetch --pruneClean up deleted branchesgit branchList, create, or delete branches
Common Options
- -a: List all branches
- -d: Delete branch
- -D: Force delete
- -m: Rename branch
Examples
git branchList local branchesgit branch feature/newCreate new branchgit branch -d feature/oldDelete merged branchgit checkoutSwitch branches or restore files (legacy)
Common Options
- -b: Create and switch to new branch
- --: Restore file
- -B: Force create/reset branch
Examples
git checkout mainSwitch to main branchgit checkout -b feature/newCreate and switch branchgit checkout -- file.txtRestore file to last commitgit switchSwitch branches (Git 2.23+)
Common Options
- -c: Create and switch to new branch
- -C: Reset existing branch and switch
- -d: Detached HEAD checkout
Examples
git switch mainSwitch to main branchgit switch -c feature/newCreate and switch branchgit switch -Switch to previous branchgit mergeJoin development histories
Common Options
- --no-ff: Create merge commit
- --squash: Squash commits
- --abort: Abort merge
Examples
git merge feature/newMerge feature/newgit merge --no-ff feature/newMerge with commitgit merge --abortAbort conflicted mergegit rebaseReapply commits on top of another base
Common Options
- -i: Interactive mode
- --onto: Specify new base
- --continue: Continue after conflict
- --abort: Abort rebase
Examples
git rebase mainRebase onto maingit rebase -i HEAD~3Edit last 3 commitsgit rebase --abortAbort and restoregit showShow commit details
Common Options
- --stat: Show file change stats
- --name-only: Show only file names
- --oneline: One line format
Examples
git showShow latest commitgit show HEAD~1Show previous commitgit show abc1234Show specific commitgit blameShow who changed each line
Common Options
- -L: Specify line range
- -w: Ignore whitespace
- -C: Detect copied code
Examples
git blame file.txtShow line-by-line authorsgit blame -L 10,20 file.txtBlame lines 10-20git blame -w file.txtIgnore whitespace changesgit reflogShow HEAD movement history
Common Options
- --all: Show all refs
- -n: Show last n entries
- --date=relative: Relative dates
Examples
git reflogShow HEAD historygit reflog -10Show last 10 entriesgit checkout HEAD@{2}Restore previous stategit shortlogSummarize commits by author
Common Options
- -s: Show commit count only
- -n: Sort by commit count
- -e: Show email addresses
Examples
git shortlog -snShow contributors by countgit shortlog -sneShow with emailsgit shortlog HEAD~100..HEADLast 100 commits contributorsgit resetReset HEAD to specified state
Common Options
- --soft: Keep changes staged
- --mixed: Unstage changes (default)
- --hard: Discard all changes (caution)
Examples
git reset --soft HEAD~1Undo commit, keep changesgit reset HEAD file.txtUnstage a filegit reset --hard HEAD~1Discard last commitgit revertCreate commit that undoes changes
Common Options
- -n: Don't commit, just apply
- --no-edit: Skip editor
- -m: Specify parent for merge
Examples
git revert HEADRevert last commitgit revert abc1234Revert specific commitgit revert -n HEAD~3..HEADRevert multiple commitsgit restoreRestore files (Git 2.23+)
Common Options
- --staged: Unstage file
- --source: Restore from commit
- --worktree: Restore working tree (default)
Examples
git restore file.txtRestore file to last commitgit restore --staged file.txtUnstage filegit restore --source HEAD~1 file.txtRestore from previous commitgit cleanRemove untracked files
Common Options
- -n: Dry run (preview)
- -f: Force deletion
- -d: Remove directories too
- -x: Remove ignored files too
Examples
git clean -nPreview what will be deletedgit clean -fdRemove untracked files and dirsgit clean -fdxRemove including ignored filesgit stashTemporarily save changes
Common Options
- push: Save to stash
- pop: Apply and remove from stash
- list: List stashes
- drop: Remove stash
Examples
git stashSave changes temporarilygit stash popRestore saved changesgit stash listList all stashesgit remoteManage remote repositories
Common Options
- -v: Show URLs
- add: Add remote
- remove: Remove remote
- rename: Rename remote
Examples
git remote -vList remotes with URLsgit remote add upstream https://github.com/user/repo.gitAdd upstream remotegit remote rename origin old-originRename remotegit configGet and set Git options
Common Options
- --global: User-wide settings
- --local: Repository settings
- --list: List all settings
- --unset: Remove setting
Examples
git config --global user.name "Your Name"Set user namegit config --global user.email "you@example.com"Set email addressgit config --listList all settingsgit initCreate empty Git repository
Common Options
- --bare: Create bare repository
- -b: Set initial branch name
- --initial-branch: Set initial branch name
Examples
git initInitialize current directorygit init my-projectCreate new repo in directorygit init -b mainInitialize with main branchgit cloneClone repository into new directory
Common Options
- --depth: Shallow clone
- -b: Clone specific branch
- --single-branch: Clone only one branch
- --recurse-submodules: Include submodules
Examples
git clone https://github.com/user/repo.gitClone a repositorygit clone --depth 1 https://github.com/user/repo.gitShallow clonegit clone -b develop https://github.com/user/repo.gitClone develop branchgit tagCreate, list, or delete tags
Common Options
- -a: Create annotated tag
- -m: Tag message
- -d: Delete tag
- -l: List tags
Examples
git tag v1.0.0Create lightweight taggit tag -a v1.0.0 -m "Release 1.0.0"Create annotated taggit tag -d v1.0.0Delete taggit cherry-pickApply specific commits to current branch
Common Options
- -n: Apply without committing
- --no-commit: Same as -n
- -x: Append commit ID to message
- --abort: Abort cherry-pick
Examples
git cherry-pick abc1234Apply specific commitgit cherry-pick abc1234 def5678Apply multiple commitsgit cherry-pick --abortAbort cherry-pickgit bisectBinary search to find bug-introducing commit
Common Options
- start: Start bisect
- good: Mark commit as good
- bad: Mark commit as bad
- reset: End bisect
Examples
git bisect startStart binary searchgit bisect bad HEADMark HEAD as badgit bisect good v1.0.0Mark v1.0.0 as goodgit grepSearch text in repository
Common Options
- -n: Show line numbers
- -i: Case insensitive
- -l: Show only file names
- -c: Count matches
Examples
git grep "TODO"Search for TODOgit grep -n "function"Search with line numbersgit grep -l "import"Show matching file namesAbout Git Commands
Git Commands is a cheat sheet compiling frequently used Git commands. It comprehensively covers commands needed for development, from basic operations to branching, history viewing, and undo operations. Each command includes descriptions, common options, and practical examples that can be copied with one click.
Key Features
- Collection of 30+ essential Git commands
- Options and usage examples for each command
- Category filtering (basic, branching, history, etc.)
- Real-time search functionality
- One-click copy of examples
Use Cases
- Looking up the correct syntax for git rebase, cherry-pick, or bisect
- Quickly checking git branch, merge, and stash options during terminal work
- Learning Git as a beginner moving from GitHub Desktop to the command line
- Referencing undo commands (reset, revert, restore) when you need to roll back a change
- Using as a cheat sheet during code review, pair programming, or a GitHub PR workflow
- Onboarding new team members to your team's Git workflow and branching conventions
FAQ
What's the difference between git pull and git fetch?
git fetch only downloads remote changes to local without affecting your working branch. git pull performs a fetch and then automatically merges or rebases to incorporate changes into your working branch.
What's the difference between git reset and git revert?
git reset rewrites commit history to return to a previous state. git revert creates a new commit that undoes changes, preserving history. revert is safer for shared branches.
What's the difference between checkout and switch?
switch was added in Git 2.23 specifically for branch switching. checkout handles both branch switching and file restoration, mixing responsibilities. Using the newer commands (switch/restore) is recommended.
What's the difference between --force and --force-with-lease?
--force pushes unconditionally, while --force-with-lease only pushes if the remote is in the expected state. This reduces the risk of accidentally overwriting others' changes.
What if I accidentally deleted a commit?
Use git reflog to view the history of HEAD movements. You can find the hash of the deleted commit and potentially recover it using git checkout or git cherry-pick.
