Git Commands Cheat Sheet
Complete Git command reference manual, organized by category for quick lookup
Basic Commands(13)
Create a new Git repository in current directory
Clone a remote repository to local
Shallow clone, fetch only the latest commit
Add file to staging area
Add all changes to staging area
Commit staged changes
Modify the last commit
Show current repository status
Show unstaged changes
Show staged changes
Show all configuration
Set global username
Set global email
Branch Management(14)
List all local branches
List all branches (including remote)
Create a new branch
Delete a branch
Rename a branch
Switch to a branch
Create and switch to new branch
Switch to a branch (Git 2.23+)
Create and switch to new branch (Git 2.23+)
Merge specified branch into current branch
Merge with a merge commit
Rebase current branch onto specified branch
Continue rebase after resolving conflicts
Apply specific commit to current branch
Remote Operations(10)
Show remote repository details
Add a remote repository
Fetch latest content from remote
Fetch updates from all remotes
Fetch and merge remote branch
Fetch and rebase
Push to remote repository
Force push (use with caution)
Push and set upstream branch
Delete remote branch
Undo Changes(8)
Unstage a file
Undo last commit, keep changes
Undo commit and staging, keep working directory
Undo commit and discard all changes
Undo a commit (creates new commit)
Restore working directory file (Git 2.23+)
Unstage file (Git 2.23+)
Remove untracked files and directories
Tag Management(6)
List all tags
Create a lightweight tag
Create an annotated tag
Delete local tag
Push tag to remote
Push all tags to remote
View History(7)
Show commit history
Show compact commit history
Show all branches history as graph
Show commit details
Show modification history for each line
Show all operation history
Start binary search for problem commit
Stash(7)
Stash current changes
Stash changes with message
List all stashes
Apply and drop the latest stash
Apply stash without dropping
Drop the latest stash
Remove all stashes
What is Git?
Git is a distributed version control system created by Linus Torvalds for managing project code history. It supports collaborative development, tracks every code change, and provides branch management and version rollback.
Mastering Git commands is essential for every developer. This cheat sheet organizes the most commonly used Git commands by category for quick reference and learning.
How to Use
Click any command card to copy the command. Use the search box to find specific commands. Click category tabs to filter commands by type.
Common Tips
FAQ
Q: What's the difference between git pull and git fetch?
A: git fetch only downloads remote updates without merging; git pull equals git fetch + git merge. It's recommended to use fetch first to review changes before deciding how to merge.
Q: How to undo the last commit?
A: Use git reset --soft HEAD~1 to undo commit but keep changes; use git reset --hard HEAD~1 to completely undo and discard changes. If already pushed, use git revert for safety.
Q: How to resolve merge conflicts?
A: Manually edit conflict files to choose which content to keep, then git add to mark as resolved, finally git commit to complete merge. Use git mergetool for visual conflict resolution.
Q: What's the difference between git merge and git rebase?
A: merge creates a merge commit preserving branch history; rebase moves commits to target branch tip making history more linear. Use merge for shared branches, rebase for local branches.
Q: How to remove a large file from history?
A: Use git filter-branch or BFG Repo-Cleaner. Note this rewrites history requiring force push.