Git Command Reference

全局设置

配置用户信息

git config --global user.name "Your Name"

# 设置全局用户名,用于标识提交作者

git config --global user.email "your.email@example.com"

# 设置全局邮箱地址,与用户名一起标识提交作者

git config --list

# 查看所有Git配置信息

git config --global init.defaultBranch main

# 设置默认分支名为main(替代传统的master)

git config --global core.editor "code --wait"

# 设置VS Code为默认编辑器(--wait参数会等待编辑器关闭)

git config --global color.ui auto

# 启用Git输出的颜色显示,提高可读性

配置别名

git config --global alias.st status

# 创建status命令的简短别名st

git config --global alias.co checkout

# 创建checkout命令的简短别名co

git config --global alias.br branch

# 创建branch命令的简短别名br

git config --global alias.ci commit

# 创建commit命令的简短别名ci

git config --global alias.unstage 'reset HEAD --'

# 创建取消暂存文件的别名unstage

常用命令

仓库初始化

git init

# 在当前目录初始化一个新的Git仓库

git clone <repository-url>

# 克隆远程仓库到本地(默认克隆所有分支)

git clone -b <branch-name> <repository-url>

# 克隆远程仓库的指定分支到本地

文件操作

git status

# 查看工作区和暂存区的状态(最常用命令)

git add .

# 添加所有文件到暂存区(包括新文件和修改的文件)

git add <filename>

# 添加指定文件到暂存区

git add -u

# 添加所有已跟踪文件的修改到暂存区(不包括新文件)

git add -i

# 进入交互式添加模式,可以选择性地暂存文件的部分更改

提交操作

git commit -m "commit message"

# 提交暂存区的更改到本地仓库,并添加提交信息

git commit -am "commit message"

# 添加所有已跟踪文件的修改并提交(相当于git add -u + git commit)

git commit --amend -m "new commit message"

# 修改最近一次提交的信息(不会创建新的提交)

git commit --allow-empty -m "empty commit"

# 创建一个空提交(常用于触发CI/CD流程)

分支管理

查看分支

git branch

# 查看本地分支列表(当前分支前有*标记)

git branch -r

# 查看远程分支列表

git branch -a

# 查看所有分支(包括本地和远程)

git branch -v

# 查看分支详细信息(包括最新提交)

创建分支

git branch <branch-name>

# 基于当前分支创建新分支(不切换)

git branch <branch-name> <commit-hash>

# 基于特定提交创建新分支

git checkout -b <branch-name>

# 创建并立即切换到新分支(传统方式)

git switch -c <branch-name>

# 创建并切换到新分支(Git 2.23+推荐方式)

切换分支

git checkout <branch-name>

# 切换到指定分支(传统方式)

git switch <branch-name>

# 切换到指定分支(Git 2.23+推荐方式)

git checkout -

# 切换到上一个分支(类似cd -)

git checkout main

# 切换到主分支(假设主分支名为main)

合并分支

git merge <branch-name>

# 将指定分支合并到当前分支(默认使用快进合并)

git merge --ff-only <branch-name>

# 仅允许快进合并(如果不能快进则失败)

git merge --no-ff <branch-name>

# 强制创建合并提交(即使可以快进)

git merge --squash <branch-name>

# 将分支的所有提交压缩为一个提交再合并

删除分支

git branch -d <branch-name>

# 安全删除已合并的分支

git branch -D <branch-name>

# 强制删除分支(即使未合并)

git push origin --delete <branch-name>

# 删除远程分支

远程仓库操作

远程仓库管理

git remote -v

# 查看所有远程仓库及其URL

git remote add origin <repository-url>

# 添加远程仓库并命名为origin

git remote set-url origin <new-repository-url>

# 修改远程仓库URL

git remote rename origin upstream

# 将远程仓库origin重命名为upstream

git remote remove origin

# 删除名为origin的远程仓库

推送操作

git push origin <branch-name>

# 将本地分支推送到远程仓库

git push -u origin main

# 首次推送主分支并设置上游跟踪

git push --all origin

# 推送所有本地分支到远程仓库

git push --tags origin

# 推送所有标签到远程仓库

git push --force origin <branch-name>

# 强制推送(会覆盖远程历史,谨慎使用)

拉取操作

git pull

# 拉取并合并当前分支的远程更改

git pull origin <branch-name>

# 拉取指定远程分支并合并到当前分支

git fetch

# 获取远程仓库最新状态但不合并

git fetch --all

# 获取所有远程仓库的最新状态

git pull --rebase

# 拉取远程更改并使用变基而非合并

撤销与回退

撤销暂存区

git reset HEAD <filename>

# 将文件从暂存区移回工作区(不丢失修改)

git reset HEAD

# 将所有文件从暂存区移回工作区

git restore --staged <filename>

# 新版本Git中撤销暂存的方式

撤销工作区

git checkout -- <filename>

# 丢弃工作区中对文件的修改(不可恢复)

git checkout -- .

# 丢弃工作区中所有修改(谨慎使用)

git restore <filename>

# 新版本Git中撤销工作区修改的方式

回退提交

git reset --soft HEAD~1

# 撤销最近提交,修改保留在暂存区

git reset --mixed HEAD~1

# 撤销最近提交,修改保留在工作区(默认行为)

git reset --hard HEAD~1

# 彻底撤销最近提交,丢弃所有修改(谨慎使用)

git reset --hard <commit-hash>

# 回退到指定提交,丢弃之后的所有修改

创建反向提交

git revert <commit-hash>

# 创建新提交来撤销指定提交的更改

git revert -m 1 <merge-commit-hash>

# 撤销合并提交(-m 1表示保留主分支线)

git revert <commit-hash1>..<commit-hash2>

# 撤销一个范围内的多个提交

暂存修改

git stash

# 暂存当前工作目录和暂存区的修改

git stash -u

# 暂存包括未跟踪文件在内的所有修改

git stash save "work in progress"

# 暂存修改并添加描述信息

git stash list

# 查看所有暂存的修改列表

git stash pop

# 恢复最近一次暂存的修改并从列表中删除

git stash apply stash@{0}

# 恢复指定的暂存修改(不删除)

git stash drop stash@{0}

# 删除指定的暂存修改

git stash clear

# 清空所有暂存的修改

日志与对比

查看提交历史

git log

# 查看当前分支的提交历史

git log --oneline

# 简洁模式查看提交历史(只显示提交哈希和消息)

git log --graph --oneline

# 图形化显示分支合并历史

git log --graph --all --oneline

# 图形化显示所有分支的提交历史

git log -- <filename>

# 查看指定文件的提交历史

git log --author="Author Name"

# 查看指定作者的提交

git log --since="2024-01-01" --until="2024-12-31"

# 查看指定时间范围内的提交

查看提交详情

git show <commit-hash>

# 查看指定提交的详细信息和更改内容

git show HEAD

# 查看最近一次提交的详细信息

git show --stat <commit-hash>

# 只查看提交的统计信息,不显示具体差异

对比差异

git diff

# 查看工作区与暂存区的差异

git diff --cached

# 查看暂存区与上次提交的差异

git diff HEAD

# 查看工作区与上次提交的差异

git diff <commit-hash1> <commit-hash2>

# 对比两个提交之间的差异

git diff <branch1> <branch2>

# 对比两个分支之间的差异

git diff <filename>

# 查看指定文件的差异

git diff --word-diff

# 以单词为单位显示差异(而非行)

查找和定位

git log --grep="keyword"

# 查找提交信息中包含关键字的提交

git grep "search-term"

# 在代码中搜索指定内容

git blame <filename>

# 查看文件的每一行是谁修改的

git bisect start
git bisect bad <bad-commit>
git bisect good <good-commit>

# 使用二分查找定位引入问题的提交

完整工作流程

1. 项目初始化

mkdir my-project
cd my-project

# 创建项目目录并进入

git init

# 初始化Git仓库

git config user.name "Your Name"
git config user.email "your.email@example.com"

# 配置用户信息(如果未全局配置)

echo "node_modules/" > .gitignore
echo "*.log" >> .gitignore
echo ".env" >> .gitignore

# 创建.gitignore文件排除不需要版本控制的文件

2. 首次提交

echo "# My Project" > README.md
echo "console.log('Hello World');" > index.js

# 创建初始项目文件

git add .

# 添加所有文件到暂存区

git commit -m "Initial commit: Add README and basic setup"

# 进行首次提交

git status

# 检查仓库状态

3. 连接远程仓库

git remote add origin https://github.com/username/my-project.git

# 添加远程仓库(先在GitHub/GitLab等平台创建)

git push -u origin main

# 推送代码到远程仓库并设置上游跟踪

git remote -v

# 验证远程连接

4. 功能开发流程

git checkout -b feature/user-authentication

# 基于当前分支创建功能分支

# 开发功能...编辑文件,添加新功能

git status

# 查看修改状态

git add src/auth.js src/login.js

# 添加特定文件到暂存区

git commit -m "Add user authentication system\n\n- Implement login functionality\n- Add password validation\n- Create user session management"

# 提交修改(多行提交信息)

git push origin feature/user-authentication

# 推送功能分支到远程

5. 更新和同步

git checkout main

# 切换回主分支

git pull origin main

# 拉取主分支最新代码

git checkout feature/user-authentication

# 切换回功能分支

git rebase main

# 将主分支的最新修改变基到功能分支

# 如果有冲突,解决冲突后继续

git add <conflicted-files>
git rebase --continue
git push --force-with-lease origin feature/user-authentication

# 安全强制推送更新后的功能分支

6. 代码审查和合并准备

git rebase -i HEAD~3

# 交互式变基,压缩或修改最近3个提交

# 在交互界面中选择squash或fixup来合并提交

git push --force-with-lease origin feature/user-authentication

# 推送整理后的提交历史

# 在GitHub/GitLab等平台创建Pull Request/Merge Request

7. 合并与冲突处理

# 方式一:通过命令行合并

git checkout main
git pull origin main
git merge feature/user-authentication

# 如果有合并冲突

git status

# 1. 查看冲突文件

# 2. 手动编辑冲突文件,解决冲突标记

git add <resolved-files>
git commit -m "Resolve merge conflicts in user authentication"

# 方式二:使用变基合并(保持线性历史)

git checkout main
git pull origin main
git checkout feature/user-authentication
git rebase main
git checkout main
git merge feature/user-authentication

8. 推送与清理

git push origin main

# 推送合并后的主分支到远程

git branch -d feature/user-authentication

# 删除本地功能分支

git push origin --delete feature/user-authentication

# 删除远程功能分支

git remote prune origin

# 清理本地已不存在的远程分支引用

git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0

# 创建并推送版本标签(可选)

9. 持续开发循环

git checkout main
git pull origin main
git checkout -b feature/payment-system

# 开始新功能开发,重复步骤4-8

10. 紧急修复流程

git checkout main
git pull origin main
git checkout -b hotfix/security-patch

# 基于main分支创建热修复分支

# 进行紧急修复...编辑代码

git add .
git commit -m "Security patch: Fix authentication vulnerability"
git push origin hotfix/security-patch

# 快速提交和推送热修复

git checkout main
git merge hotfix/security-patch
git push origin main

# 快速合并到main(跳过正常审查流程)

git checkout develop
git merge hotfix/security-patch
git push origin develop

# 同时合并到开发分支(如果存在)

git branch -d hotfix/security-patch
git push origin --delete hotfix/security-patch

# 清理热修复分支

git tag -a v1.0.1 -m "Emergency security patch"
git push origin v1.0.1

# 创建紧急版本标签