Git Commands Cheat Sheet

Complete Git command reference manual, organized by category for quick lookup

All: 65 个命令

Basic Commands(13)

git init

Create a new Git repository in current directory

git clone <url>

Clone a remote repository to local

git clone --depth=1 <url>

Shallow clone, fetch only the latest commit

git add <file>

Add file to staging area

git add .

Add all changes to staging area

git commit -m "message"

Commit staged changes

git commit --amend

Modify the last commit

git status

Show current repository status

git diff

Show unstaged changes

git diff --staged

Show staged changes

git config --list

Show all configuration

git config --global user.name "name"

Set global username

git config --global user.email "email"

Set global email

Branch Management(14)

git branch

List all local branches

git branch -a

List all branches (including remote)

git branch <name>

Create a new branch

git branch -d <name>

Delete a branch

git branch -m <old> <new>

Rename a branch

git checkout <branch>

Switch to a branch

git checkout -b <branch>

Create and switch to new branch

git switch <branch>

Switch to a branch (Git 2.23+)

git switch -c <branch>

Create and switch to new branch (Git 2.23+)

git merge <branch>

Merge specified branch into current branch

git merge --no-ff <branch>

Merge with a merge commit

git rebase <branch>

Rebase current branch onto specified branch

git rebase --continue

Continue rebase after resolving conflicts

git cherry-pick <commit>

Apply specific commit to current branch

Remote Operations(10)

git remote -v

Show remote repository details

git remote add <name> <url>

Add a remote repository

git fetch <remote>

Fetch latest content from remote

git fetch --all

Fetch updates from all remotes

git pull <remote> <branch>

Fetch and merge remote branch

git pull --rebase

Fetch and rebase

git push <remote> <branch>

Push to remote repository

git push -f

Force push (use with caution)

git push -u origin <branch>

Push and set upstream branch

git push origin --delete <branch>

Delete remote branch

Undo Changes(8)

git reset <file>

Unstage a file

git reset --soft HEAD~1

Undo last commit, keep changes

git reset --mixed HEAD~1

Undo commit and staging, keep working directory

git reset --hard HEAD~1

Undo commit and discard all changes

git revert <commit>

Undo a commit (creates new commit)

git restore <file>

Restore working directory file (Git 2.23+)

git restore --staged <file>

Unstage file (Git 2.23+)

git clean -fd

Remove untracked files and directories

Tag Management(6)

git tag

List all tags

git tag <name>

Create a lightweight tag

git tag -a <name> -m "msg"

Create an annotated tag

git tag -d <name>

Delete local tag

git push origin <tag>

Push tag to remote

git push --tags

Push all tags to remote

View History(7)

git log

Show commit history

git log --oneline

Show compact commit history

git log --oneline --graph --all

Show all branches history as graph

git show <commit>

Show commit details

git blame <file>

Show modification history for each line

git reflog

Show all operation history

git bisect start

Start binary search for problem commit

Stash(7)

git stash

Stash current changes

git stash save "message"

Stash changes with message

git stash list

List all stashes

git stash pop

Apply and drop the latest stash

git stash apply

Apply stash without dropping

git stash drop

Drop the latest stash

git stash clear

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

Undo git add
Use git restore --staged or git reset to unstage
Modify last commit
Use git commit --amend to modify the message or content of last commit
Save work progress
Use git stash to temporarily save work and switch to other branches
Undo pushed commit
Use git revert instead of git reset to avoid breaking remote history
View file history
Use git log -p to see complete modification history of a file

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.