正在加载,请稍候…

Git Command Cheat Sheet: The Most Useful Commands Explained

A comprehensive Git reference covering everyday commands, branching, undo operations, and advanced workflows.

Essential Git Commands Reference

Git is the most widely used distributed version control system. This reference covers the most important commands with practical examples for daily development workflows.

Configuration

# Set user identity (required before first commit)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default editor
git config --global core.editor "code --wait"

# Set default branch name
git config --global init.defaultBranch main

# View all configuration
git config --list

# View specific setting
git config user.email

Repository Setup

# Initialize a new repository
git init
git init my-project

# Clone a remote repository
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git custom-folder

# Clone only specific branch
git clone -b develop https://github.com/user/repo.git

Daily Workflow Commands

# Check status of working directory
git status
git status -s    # Short format

# Stage changes
git add file.txt           # Stage specific file
git add .                  # Stage all changes
git add -p                 # Stage changes interactively (patch mode)
git add src/               # Stage entire directory

# Commit
git commit -m "feat: add user authentication"
git commit -am "fix: correct typo"  # Stage tracked files and commit
git commit --amend          # Amend last commit message or add staged changes

# View history
git log
git log --oneline           # One line per commit
git log --oneline --graph   # ASCII graph of branches
git log -n 10               # Last 10 commits
git log --since="2 weeks ago"
git log --author="Alice"
git log --grep="login"      # Search commit messages

Branching

# Create and switch to new branch
git checkout -b feature/login  # Traditional
git switch -c feature/login    # Modern (Git 2.23+)

# Switch between branches
git checkout main
git switch main

# List branches
git branch           # Local branches
git branch -a        # All branches (local + remote)
git branch -v        # Show last commit on each branch

# Delete branch
git branch -d feature/done  # Safe delete (merged only)
git branch -D feature/old   # Force delete

# Rename current branch
git branch -m new-name

Remote Operations

# Add remote
git remote add origin https://github.com/user/repo.git

# View remotes
git remote -v

# Fetch (download changes, don't merge)
git fetch origin
git fetch --all

# Pull (fetch + merge)
git pull origin main
git pull --rebase origin main  # Pull with rebase instead of merge

# Push
git push origin feature/login
git push -u origin feature/login  # Set upstream (-u) for future git push
git push --force-with-lease       # Safer alternative to --force

Merging and Rebasing

# Merge branch into current branch
git merge feature/login
git merge --no-ff feature/login  # Always create merge commit

# Rebase current branch onto main
git rebase main
git rebase -i HEAD~3  # Interactive rebase of last 3 commits

# Abort rebase if conflicts are too complex
git rebase --abort

Stashing

# Save work in progress temporarily
git stash
git stash push -m "WIP: half-done feature"

# List stashes
git stash list

# Apply most recent stash
git stash pop          # Apply and remove from stash
git stash apply        # Apply but keep in stash

# Apply specific stash
git stash apply stash@{2}

# Drop stash
git stash drop stash@{0}
git stash clear        # Remove all stashes

Undoing Changes

# Discard working directory changes (unstaged)
git restore file.txt           # Modern
git checkout -- file.txt       # Traditional

# Unstage a file (keep changes in working directory)
git restore --staged file.txt

# Undo last commit (keep changes staged)
git reset --soft HEAD~1

# Undo last commit (keep changes unstaged)
git reset HEAD~1

# Undo last commit (discard changes entirely)
git reset --hard HEAD~1

# Revert a commit (creates a new commit that undoes changes)
git revert abc1234    # Safe for shared history

-> Try the Git Memo Reference