版本控制和协作的核心 Git 命令。
初始设置
# 配置用户
git config --global user.name "Your Name"
git config --global user.email "your@email.com"# 初始化仓库
git init
# 克隆仓库
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git custom-name
基本工作流
暂存和提交
# 查看状态
git status# 添加文件到暂存区
git add file.txt
git add .
git add -A # 所有更改,包括删除
# 提交更改
git commit -m "Commit message"
# 一步完成添加和提交
git commit -am "Message"
# 修改最后一次提交
git commit --amend -m "New message"
git commit --amend --no-edit # 保持消息不变
查看更改
# 显示未暂存的更改
git diff# 显示已暂存的更改
git diff --staged
# 显示特定文件的更改
git diff file.txt
# 显示提交之间的更改
git diff commit1 commit2
分支与合并
分支管理
# 列出分支
git branch
git branch -a # 包含远程分支# 创建分支
git branch feature-name
# 切换分支
git checkout feature-name
git switch feature-name # 现代替代方式
# 创建并切换
git checkout -b feature-name
git switch -c feature-name
# 删除分支
git branch -d branch-name
git branch -D branch-name # 强制删除
# 重命名分支
git branch -m old-name new-name
合并
# 将分支合并到当前分支
git merge feature-name# 禁止快进合并
git merge --no-ff feature-name
# 中止合并
git merge --abort
# 显示合并冲突
git diff --name-only --diff-filter=U
远程操作
管理远程仓库
# 列出远程仓库
git remote -v# 添加远程仓库
git remote add origin https://github.com/user/repo.git
# 更改远程仓库 URL
git remote set-url origin https://github.com/user/new-repo.git
# 删除远程仓库
git remote remove origin
与远程仓库同步
# 从远程仓库获取
git fetch origin# 拉取更改(获取 + 合并)
git pull
# 使用变基拉取
git pull --rebase
# 推送更改
git push
# 推送新分支
git push -u origin branch-name
# 强制推送(小心!)
git push --force-with-lease
历史与日志
查看历史
# 显示提交历史
git log# 每行一个提交
git log --oneline
# 带图形
git log --graph --oneline --all
# 最近 N 个提交
git log -5
# 按作者筛选
git log --author="Name"
# 日期范围内的提交
git log --since="2 weeks ago"
git log --until="2024-01-01"
# 文件历史
git log -- file.txt
搜索历史
# 搜索提交消息
git log --grep="bug fix"# 搜索代码更改
git log -S "function_name"
# 显示每行是谁更改的
git blame file.txt
# 找出引入 bug 的提交
git bisect start
git bisect bad
git bisect good commit-hash
撤销更改
工作目录
# 丢弃文件中的更改
git restore file.txt
git checkout -- file.txt # 旧方式# 丢弃所有更改
git restore .
暂存区
# 取消暂存文件
git restore --staged file.txt
git reset HEAD file.txt # 旧方式# 取消所有暂存
git reset
提交
# 撤销最后一次提交(保留更改)
git reset --soft HEAD~1# 撤销最后一次提交(丢弃更改)
git reset --hard HEAD~1
# 还原提交(创建新提交)
git revert commit-hash
# 重置到特定提交
git reset --hard commit-hash
储藏(Stashing)
# 储藏更改
git stash# 带消息储藏
git stash save "Work in progress"
# 列出储藏
git stash list
# 应用最新储藏
git stash apply
# 应用并删除储藏
git stash pop
# 应用特定储藏
git stash apply stash@{2}
# 删除储藏
git stash drop stash@{0}
# 清除所有储藏
git stash clear
变基(Rebasing)
# 变基当前分支
git rebase main# 交互式变基(最近 3 个提交)
git rebase -i HEAD~3
# 解决冲突后继续变基
git rebase --continue
# 跳过当前提交
git rebase --skip
# 中止变基
git rebase --abort
标签
# 列出标签
git tag# 创建轻量标签
git tag v1.0.0
# 创建附注标签
git tag -a v1.0.0 -m "Version 1.0.0"
# 为特定提交打标签
git tag v1.0.0 commit-hash
# 推送标签
git push origin v1.0.0
# 推送所有标签
git push --tags
# 删除标签
git tag -d v1.0.0
git push origin --delete v1.0.0
高级操作
拣选(Cherry-pick)
# 应用特定提交
git cherry-pick commit-hash# 拣选但不提交
git cherry-pick -n commit-hash
子模块
# 添加子模块
git submodule add https://github.com/user/repo.git path/# 初始化子模块
git submodule init
# 更新子模块
git submodule update
# 带子模块克隆
git clone --recursive https://github.com/user/repo.git
清理
# 预览要删除的文件
git clean -n# 删除未跟踪文件
git clean -f
# 删除未跟踪文件和目录
git clean -fd
# 包含忽略的文件
git clean -fdx
常用工作流
功能分支工作流:
git checkout -b feature/new-feature
# 做出更改
git add .
git commit -m "Add new feature"
git push -u origin feature/new-feature
# 创建 PR,合并后:
git checkout main
git pull
git branch -d feature/new-feature
热修复工作流:
git checkout main
git pull
git checkout -b hotfix/critical-bug
# 修复 bug
git commit -am "Fix critical bug"
git push -u origin hotfix/critical-bug
# 合并后:
git checkout main && git pull
同步分叉:
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
有用的别名
添加到 ~/.gitconfig:
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = log --graph --oneline --all
amend = commit --amend --no-edit
技巧
- 经常提交,稍后完善(交互式变基)
- 写有意义的提交消息
- 使用
.gitignore 排除文件
- 永远不要强制推送到共享分支
- 开始工作前先拉取
- 使用功能分支,而非 main
- 合并前变基功能分支
- 使用
--force-with-lease 而非 --force
常见问题
撤销意外提交:
git reset --soft HEAD~1
恢复已删除的分支:
git reflog
git checkout -b branch-name
修复错误的提交消息:
git commit --amend -m "Correct message"
解决合并冲突:
# 编辑文件解决冲突
git add resolved-files
git commit # 或 git merge --continue
文档
官方文档:https://git-scm.com/doc
Pro Git 书:https://git-scm.com/book
可视化 Git 指南:https://marklodato.github.io/visual-git-guide/
Essential Git commands for version control and collaboration.
Initial Setup
# Configure user
git config --global user.name "Your Name"
git config --global user.email "your@email.com"# Initialize repository
git init
# Clone repository
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git custom-name
Basic Workflow
Staging and committing
# Check status
git status# Add files to staging
git add file.txt
git add .
git add -A # All changes including deletions
# Commit changes
git commit -m "Commit message"
# Add and commit in one step
git commit -am "Message"
# Amend last commit
git commit --amend -m "New message"
git commit --amend --no-edit # Keep message
Viewing changes
# Show unstaged changes
git diff# Show staged changes
git diff --staged
# Show changes in specific file
git diff file.txt
# Show changes between commits
git diff commit1 commit2
Branching & Merging
Branch management
# List branches
git branch
git branch -a # Include remote branches# Create branch
git branch feature-name
# Switch branch
git checkout feature-name
git switch feature-name # Modern alternative
# Create and switch
git checkout -b feature-name
git switch -c feature-name
# Delete branch
git branch -d branch-name
git branch -D branch-name # Force delete
# Rename branch
git branch -m old-name new-name
Merging
# Merge branch into current
git merge feature-name# Merge with no fast-forward
git merge --no-ff feature-name
# Abort merge
git merge --abort
# Show merge conflicts
git diff --name-only --diff-filter=U
Remote Operations
Managing remotes
# List remotes
git remote -v# Add remote
git remote add origin https://github.com/user/repo.git
# Change remote URL
git remote set-url origin https://github.com/user/new-repo.git
# Remove remote
git remote remove origin
Syncing with remote
# Fetch from remote
git fetch origin# Pull changes (fetch + merge)
git pull
# Pull with rebase
git pull --rebase
# Push changes
git push
# Push new branch
git push -u origin branch-name
# Force push (careful!)
git push --force-with-lease
History & Logs
Viewing history
# Show commit history
git log# One line per commit
git log --oneline
# With graph
git log --graph --oneline --all
# Last N commits
git log -5
# Commits by author
git log --author="Name"
# Commits in date range
git log --since="2 weeks ago"
git log --until="2024-01-01"
# File history
git log -- file.txt
Searching history
# Search commit messages
git log --grep="bug fix"# Search code changes
git log -S "function_name"
# Show who changed each line
git blame file.txt
# Find commit that introduced bug
git bisect start
git bisect bad
git bisect good commit-hash
Undoing Changes
Working directory
# Discard changes in file
git restore file.txt
git checkout -- file.txt # Old way# Discard all changes
git restore .
Staging area
# Unstage file
git restore --staged file.txt
git reset HEAD file.txt # Old way# Unstage all
git reset
Commits
# Undo last commit (keep changes)
git reset --soft HEAD~1# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert commit (create new commit)
git revert commit-hash
# Reset to specific commit
git reset --hard commit-hash
Stashing
# Stash changes
git stash# Stash with message
git stash save "Work in progress"
# List stashes
git stash list
# Apply latest stash
git stash apply
# Apply and remove stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Delete stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
Rebasing
# Rebase current branch
git rebase main# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Continue after resolving conflicts
git rebase --continue
# Skip current commit
git rebase --skip
# Abort rebase
git rebase --abort
Tags
# List tags
git tag# Create lightweight tag
git tag v1.0.0
# Create annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Tag specific commit
git tag v1.0.0 commit-hash
# Push tag
git push origin v1.0.0
# Push all tags
git push --tags
# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0
Advanced Operations
Cherry-pick
# Apply specific commit
git cherry-pick commit-hash# Cherry-pick without committing
git cherry-pick -n commit-hash
Submodules
# Add submodule
git submodule add https://github.com/user/repo.git path/# Initialize submodules
git submodule init
# Update submodules
git submodule update
# Clone with submodules
git clone --recursive https://github.com/user/repo.git
Clean
# Preview files to be deleted
git clean -n# Delete untracked files
git clean -f
# Delete untracked files and directories
git clean -fd
# Include ignored files
git clean -fdx
Common Workflows
Feature branch workflow:
git checkout -b feature/new-feature
# Make changes
git add .
git commit -m "Add new feature"
git push -u origin feature/new-feature
# Create PR, then after merge:
git checkout main
git pull
git branch -d feature/new-feature
Hotfix workflow:
git checkout main
git pull
git checkout -b hotfix/critical-bug
# Fix bug
git commit -am "Fix critical bug"
git push -u origin hotfix/critical-bug
# After merge:
git checkout main && git pull
Syncing fork:
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
Useful Aliases
Add to ~/.gitconfig:
[alias]
st = status
co = checkout
br = branch
ci = commit
unstage = reset HEAD --
last = log -1 HEAD
visual = log --graph --oneline --all
amend = commit --amend --no-edit
Tips
- Commit often, perfect later (interactive rebase)
- Write meaningful commit messages
- Use
.gitignore for files to exclude
- Never force push to shared branches
- Pull before starting work
- Use feature branches, not main
- Rebase feature branches before merging
- Use
--force-with-lease instead of --force
Common Issues
Undo accidental commit:
git reset --soft HEAD~1
Recover deleted branch:
git reflog
git checkout -b branch-name
Fix wrong commit message:
git commit --amend -m "Correct message"
Resolve merge conflicts:
# Edit files to resolve conflicts
git add resolved-files
git commit # Or git merge --continue
Documentation
Official docs: https://git-scm.com/doc
Pro Git book: https://git-scm.com/book
Visual Git guide: https://marklodato.github.io/visual-git-guide/