📚 Git Cheat Sheet
Complete Git commands reference for developers. Search and copy git commands for commit, push, pull, branch, merge, and more.
💡 Usage Tips: Click the "Copy" button next to the command to quickly copy, click category title to expand/collapse content.
⚙️ Configuration
git config --global user.name "[name]"
Set global username
git config --global user.email "[email]"
Set global email
git config --global core.editor "[editor]"
Set default editor
git config --global color.ui true
Enable colored output
git config --global alias.co checkout
Set command alias (checkout → co)
🚀 Create Repository
git init
Initialize new Git repository
git clone [url]
Clone remote repository to local
git clone -b [branch] [url]
Clone specified branch
💾 Basic Commit
git status
View working directory status
git add [file]
Add file to staging area
git add .
Add all modified files
git commit -m "[message]"
Commit staged content
git commit -am "[message]"
Commit all tracked changes
git commit --amend
Modify last commit
git commit --amend --no-edit
Modify commit without changing message
🌿 Branch Management
git branch
View local branch list
git branch -a
View all branches (including remote)
git branch [branch-name]
Create new branch
git checkout [branch-name]
Switch to specified branch
git checkout -b [branch-name]
Create and switch to new branch
git branch -d [branch-name]
Delete merged branch
git branch -D [branch-name]
Force delete branch
git branch -m [old-name] [new-name]
Rename branch
☁️ Remote Operations
git remote -v
View remote repository info
git remote add origin [url]
Add remote repository
git pull [remote] [branch]
Pull remote updates
git push [remote] [branch]
Push local branch
git push -f [remote] [branch]
Force push (overwrite remote)
git fetch [remote]
Fetch remote updates without merging
git remote remove [remote]
Delete remote repository
🔀 Merge and Rebase
git merge [branch]
Merge specified branch into current branch
git merge --no-ff [branch]
Merge but create merge commit
git rebase [branch]
Rebase to specified branch
git rebase --continue
Continue rebase operation
git rebase --abort
Abort rebase operation
↩️ Undo and Rollback
git reset HEAD~1
Undo last commit (keep changes)
git reset HEAD~[N]
Undo last N commits
git reset --hard HEAD~1
Undo commit and discard changes
git reset --hard [commit-hash]
Rollback to specified commit
git checkout -- [file]
Discard local file changes
git revert [commit-hash]
Create new commit to undo specified commit
git clean -fd
Delete untracked files and directories
🔍 View History
git log
View commit history
git log --oneline
Concise single-line history
git log --graph --oneline
Graphical branch history
git log --stat
View commit statistics
git diff
View working directory differences
git diff --staged
View staged differences
git show [commit-hash]
View specified commit details
🏷️ Tag Management
git tag
View tag list
git tag [tag-name]
Create lightweight tag
git tag -a [tag-name] -m "[message]"
Create annotated tag
git push [remote] [tag-name]
Push tag to remote
git push [remote] --tags
Push all tags
git tag -d [tag-name]
Delete local tag
📦 Stash and Stage
git stash
Stash current changes
git stash save "[message]"
Stash with message
git stash list
View stash list
git stash apply
Restore stash without deleting
git stash pop
Restore stash and delete
git stash drop
Delete latest stash
git stash clear
Clear all stashes
🧩 Submodules
git submodule add [url] [path]
Add submodule
git submodule init
Initialize submodule
git submodule update
Update submodule
Advanced Tips
git cherry-pick [commit-hash]
Pick and apply specified commit
git bisect start [bad] [good]
Binary search to locate issues
git blame [file]
View file modification history
git reflog
View all operation records
git clean -xdf
Thoroughly clean working directory (use with caution)
git gc --aggressive
Compress and optimize repository