1. 基础配置
1.1 查看 Git 版本
bash
git --version
作用:查看当前 Git 客户端版本,用于排查兼容性问题。
1.2 配置用户名和邮箱
bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
作用:设置提交记录中的作者信息。
说明:
--global表示当前用户所有仓库生效。- 不加
--global表示只对当前仓库生效。
查看配置:
bash
git config --list
git config user.name
git config user.email
1.3 配置默认分支名
bash
git config --global init.defaultBranch main
作用:执行 git init 时默认创建 main 分支,而不是旧式 master。
1.4 配置换行符
Windows 常用:
bash
git config --global core.autocrlf true
Linux/macOS 常用:
bash
git config --global core.autocrlf input
作用:减少跨系统协作时的 CRLF/LF 换行差异。
1.5 配置命令别名
bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
作用:简化常用命令。
使用示例:
bash
git st
git br
2. 仓库初始化与克隆
2.1 初始化本地仓库
bash
git init
作用:把当前目录初始化为 Git 仓库,会生成 .git 目录。
适用场景:本地已有项目,需要开始用 Git 管理。
2.2 克隆远程仓库
bash
git clone <仓库地址>
示例:
bash
git clone https://example.com/company/project.git
作用:从远程仓库下载完整代码和历史记录。
2.3 克隆到指定目录
bash
git clone <仓库地址> <目录名>
示例:
bash
git clone https://example.com/company/project.git my-project
作用:自定义本地目录名。
2.4 克隆指定分支
bash
git clone -b develop <仓库地址>
作用:克隆后直接切到指定分支。
3. 查看状态与历史
3.1 查看工作区状态
bash
git status
作用:查看当前分支、暂存区、工作区改动、未跟踪文件等。
常用简洁形式:
bash
git status -sb
3.2 查看提交历史
bash
git log
作用:查看当前分支提交记录。
常用简洁图形形式:
bash
git log --oneline --graph --decorate --all
说明:
--oneline:每个 commit 一行。--graph:显示分支图。--decorate:显示分支名、tag。--all:显示所有分支。
3.3 查看某次提交详情
bash
git show <commitId>
作用:查看某次提交的作者、时间、说明和代码差异。
3.4 查看某个文件历史
bash
git log -- <文件路径>
示例:
bash
git log -- src/main/java/App.java
查看文件每次修改差异:
bash
git log -p -- <文件路径>
3.5 查看每行代码最后修改人
bash
git blame <文件路径>
作用:定位某行代码是谁、在哪次提交修改的。
4. 查看差异
4.1 查看工作区未暂存差异
bash
git diff
作用:查看工作区相对暂存区的改动。
4.2 查看已暂存差异
bash
git diff --cached
或:
bash
git diff --staged
作用:查看已经 git add 但还没提交的内容。
4.3 查看两个分支差异
bash
git diff branchA..branchB
示例:
bash
git diff develop..feature/login
作用:查看两个分支代码内容差异。
4.4 查看两个提交差异
bash
git diff <commit1> <commit2>
作用:比较两个提交点之间的代码差异。
4.5 只看变更文件列表
bash
git diff --name-only
git diff --cached --name-only
作用:快速确认哪些文件有变更。
5. 暂存与提交
5.1 添加指定文件到暂存区
bash
git add <文件路径>
示例:
bash
git add src/app.ts
作用:把文件改动加入暂存区,准备提交。
5.2 添加当前目录所有改动
bash
git add .
作用:把当前目录及子目录下所有新增、修改、删除加入暂存区。
注意:执行前建议先 git status,避免误提交无关文件。
5.3 交互式暂存
bash
git add -p
作用:按代码块选择是否加入暂存区,适合一个文件中既有相关改动又有无关改动的情况。
5.4 提交代码
bash
git commit -m "提交说明"
示例:
bash
git commit -m "feat: add login page"
作用:把暂存区内容生成一次提交。
5.5 修改最近一次提交说明
bash
git commit --amend
作用:修改最近一次提交的信息,或把当前暂存区内容追加进最近一次提交。
注意:如果该提交已经推送到共享远程分支,谨慎使用,因为会改写历史。
5.6 空提交
bash
git commit --allow-empty -m "trigger ci"
作用:不改变文件,也创建一次提交,常用于触发 CI/CD。
6. 分支操作
6.1 查看本地分支
bash
git branch
6.2 查看远程分支
bash
git branch -r
6.3 查看所有分支
bash
git branch -a
6.4 查看分支关联关系
bash
git branch -vv
作用:查看本地分支关联的远程分支,以及领先/落后情况。
6.5 创建分支
bash
git branch <分支名>
作用:创建分支但不切换。
6.6 创建并切换分支
推荐新命令:
bash
git switch -c <分支名>
旧命令:
bash
git checkout -b <分支名>
示例:
bash
git switch -c feature/login
6.7 切换分支
bash
git switch <分支名>
旧命令:
bash
git checkout <分支名>
6.8 从远程分支创建本地分支
bash
git switch -c develop origin/develop
或:
bash
git switch --track origin/develop
作用:基于远程跟踪分支创建本地分支并建立 upstream 关联。
6.9 重命名当前分支
bash
git branch -m <新分支名>
重命名指定分支:
bash
git branch -m <旧分支名> <新分支名>
6.10 删除本地分支
安全删除,要求分支已合并:
bash
git branch -d <分支名>
强制删除:
bash
git branch -D <分支名>
注意:-D 可能丢失未合并提交,使用前应确认。
7. 远程仓库操作
7.1 查看远程仓库
bash
git remote -v
7.2 添加远程仓库
bash
git remote add origin <仓库地址>
作用:给当前仓库添加远程地址。
7.3 修改远程仓库地址
bash
git remote set-url origin <新仓库地址>
7.4 删除远程仓库配置
bash
git remote remove origin
7.5 获取远程更新但不合并
bash
git fetch origin
作用:更新本地的 origin/* 远程跟踪分支,但不改变当前工作分支代码。
7.6 获取所有远程更新
bash
git fetch --all
7.7 清理不存在的远程分支引用
bash
git fetch --prune
或:
bash
git remote prune origin
作用:远程分支已删除时,清理本地残留的 origin/xxx 引用。
8. 拉取与推送
8.1 拉取代码
bash
git pull
作用:从当前分支关联的远程分支拉取并合并。
等价于:
bash
git fetch
git merge
8.2 使用 rebase 方式拉取
bash
git pull --rebase
作用:把本地提交移动到远程最新提交之后,历史更线性。
适合:个人 feature 分支同步远程代码。
谨慎:多人共享分支上不要随意 rebase 已推送历史。
8.3 推送当前分支
bash
git push
前提:当前分支已设置 upstream。
8.4 第一次推送并设置 upstream
bash
git push -u origin <分支名>
示例:
bash
git push -u origin feature/login
作用:推送本地分支到远程,并建立默认关联。
8.5 推送到指定远程分支
bash
git push origin 本地分支名:远程分支名
示例:
bash
git push origin feature/login:feature/login
8.6 删除远程分支
bash
git push origin --delete <分支名>
示例:
bash
git push origin --delete feature/login
9. 合并、变基与挑选提交
9.1 合并分支
bash
git merge <分支名>
示例:
bash
git switch develop
git merge feature/login
作用:把指定分支合并到当前分支。
9.2 禁止快进合并
bash
git merge --no-ff <分支名>
作用:保留一次合并提交,方便看出功能分支合入历史。
9.3 变基
bash
git rebase <目标分支>
示例:
bash
git switch feature/login
git rebase develop
作用:把当前分支的提交"挪到"目标分支最新提交之后,使历史更线性。
注意:不要对已被多人基于开发的共享提交随意 rebase。
9.4 继续 rebase
bash
git rebase --continue
作用:解决冲突并 git add 后继续变基。
9.5 取消 rebase
bash
git rebase --abort
作用:放弃本次 rebase,回到 rebase 前状态。
9.6 挑选某次提交
bash
git cherry-pick <commitId>
作用:把某个 commit 的改动复制到当前分支。
适用场景:
- hotfix 修复同步到 develop。
- 只想拿某个功能分支中的部分提交。
9.7 继续 cherry-pick
bash
git cherry-pick --continue
9.8 取消 cherry-pick
bash
git cherry-pick --abort
10. 撤销与恢复
10.1 取消暂存但保留工作区改动
bash
git restore --staged <文件路径>
旧命令:
bash
git reset HEAD <文件路径>
作用:把文件从暂存区移出,但不丢失文件修改。
10.2 丢弃某个文件的工作区改动
bash
git restore <文件路径>
旧命令:
bash
git checkout -- <文件路径>
作用:恢复文件到最近一次提交或暂存状态。
风险:会丢失该文件未提交改动。
10.3 恢复某个文件到指定提交
bash
git restore --source=<commitId> -- <文件路径>
作用:把指定文件恢复到某个提交时的内容。
10.4 撤销某次提交,生成反向提交
bash
git revert <commitId>
作用:不改写历史,新增一个反向提交来撤销旧提交。
适合:已经推送到共享分支的提交。
10.5 回退当前分支到指定提交
bash
git reset --soft <commitId>
git reset --mixed <commitId>
git reset --hard <commitId>
区别:
| 命令 | 提交记录 | 暂存区 | 工作区 |
|---|---|---|---|
--soft |
回退 | 保留 | 保留 |
--mixed |
回退 | 取消暂存 | 保留 |
--hard |
回退 | 丢弃 | 丢弃 |
注意:git reset --hard 会丢弃未提交改动,极具风险。
11. stash 临时保存
11.1 保存当前改动
bash
git stash
作用:临时保存工作区和暂存区改动,让工作区变干净。
11.2 保存时带说明
bash
git stash push -m "说明"
11.3 包含未跟踪文件
bash
git stash push -u -m "include untracked"
作用:同时保存未被 Git 跟踪的新文件。
11.4 查看 stash 列表
bash
git stash list
11.5 应用最近一次 stash
bash
git stash apply
作用:恢复改动,但 stash 记录仍保留。
11.6 应用并删除最近一次 stash
bash
git stash pop
作用:恢复改动,并从 stash 列表删除该记录。
11.7 应用指定 stash
bash
git stash apply stash@{0}
11.8 删除 stash
bash
git stash drop stash@{0}
清空所有 stash:
bash
git stash clear
注意:clear 不可轻易恢复,谨慎使用。
12. 标签 tag
12.1 查看 tag
bash
git tag
12.2 创建轻量 tag
bash
git tag v1.0.0
12.3 创建附注 tag
bash
git tag -a v1.0.0 -m "release v1.0.0"
推荐正式发布使用附注 tag。
12.4 给指定提交打 tag
bash
git tag -a v1.0.0 <commitId> -m "release v1.0.0"
12.5 推送 tag
bash
git push origin v1.0.0
推送所有 tag:
bash
git push origin --tags
12.6 删除本地 tag
bash
git tag -d v1.0.0
12.7 删除远程 tag
bash
git push origin --delete v1.0.0
13. 清理命令
13.1 查看将被清理的未跟踪文件
bash
git clean -n
作用:预览会删除哪些未跟踪文件。
13.2 删除未跟踪文件
bash
git clean -f
13.3 删除未跟踪目录
bash
git clean -fd
13.4 删除被忽略文件
bash
git clean -fdx
风险:会删除 .gitignore 忽略的文件,例如构建产物、依赖目录、环境文件等,慎用。
14. 子模块 submodule
14.1 克隆带子模块的项目
bash
git clone --recurse-submodules <仓库地址>
14.2 初始化并更新子模块
bash
git submodule update --init --recursive
14.3 更新子模块到远程最新
bash
git submodule update --remote --recursive
作用:处理项目中依赖其他 Git 仓库的情况。
15. 工作区排查命令
15.1 查看当前 HEAD
bash
git rev-parse --abbrev-ref HEAD
作用:查看当前所在分支。
15.2 查看最近引用变动
bash
git reflog
作用:查看本地 HEAD 和分支指针历史,非常适合误删分支、误 reset 后找回提交。
15.3 查找包含某个提交的分支
bash
git branch --contains <commitId>
远程也一起查:
bash
git branch -a --contains <commitId>
15.4 查看哪些分支已合并
bash
git branch --merged
查看未合并分支:
bash
git branch --no-merged
15.5 二分查找问题提交
bash
git bisect start
git bisect bad
git bisect good <正常的commitId>
之后每次测试当前版本:
bash
git bisect good
# 或
git bisect bad
结束:
bash
git bisect reset
作用:在大量提交中定位引入 bug 的提交。
16. 常见日常流程
16.1 新功能开发流程
bash
git switch develop
git pull --rebase
git switch -c feature/xxx
# 修改代码
git status
git add .
git commit -m "feat: xxx"
git push -u origin feature/xxx
之后提交 MR/PR 合并到 develop。
16.2 同步 develop 到当前功能分支
方式一:merge,保留合并记录。
bash
git switch feature/xxx
git fetch origin
git merge origin/develop
方式二:rebase,历史更线性。
bash
git switch feature/xxx
git fetch origin
git rebase origin/develop
16.3 发布打 tag
bash
git switch main
git pull
git tag -a v1.0.0 -m "release v1.0.0"
git push origin v1.0.0
16.4 热修复流程
bash
git switch main
git pull
git switch -c hotfix/xxx
# 修复问题
git add .
git commit -m "fix: xxx"
git push -u origin hotfix/xxx
# 合并到 main 后,再同步到 develop
17. 高风险命令提醒
| 命令 | 风险 | 建议 |
|---|---|---|
git reset --hard |
丢弃未提交改动 | 执行前 git status,必要时先 git stash 或建临时分支 |
git clean -fdx |
删除未跟踪和忽略文件 | 先执行 git clean -n 预览 |
git push --force |
覆盖远程历史 | 优先使用 --force-with-lease,并确认没人基于旧历史开发 |
git rebase |
改写提交历史 | 不要随意 rebase 公共分支 |
git branch -D |
删除未合并分支 | 先确认提交已合并或已备份 |
git stash clear |
清空所有 stash | 确认 stash 不再需要 |
18. 命令选择建议
| 目标 | 推荐命令 |
|---|---|
| 查看当前改动 | git status -sb、git diff |
| 提交代码 | git add、git commit |
| 新建分支 | git switch -c |
| 切换分支 | git switch |
| 更新远程信息 | git fetch |
| 拉取当前分支 | git pull --rebase 或 git pull |
| 推送新分支 | git push -u origin 分支名 |
| 撤销已推送提交 | git revert |
| 临时切分支 | git stash push -u -m "说明" |
| 找回误操作 | git reflog |
| 标记版本 | git tag -a |