Skip to main content
Documents
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Git FAQ

一、Git 基础

1. Git 和 SVN 的区别是什么?

对比维度 Git SVN
版本库模型 分布式,每人都有完整仓库 集中式,依赖中央服务器
分支 轻量,几乎免费 复制目录,重
提交速度 快,本地提交 慢,需要网络
离线工作 完全支持 不能
存储结构 内容寻址(对象) 文件差异
合并冲突处理 强大 一般

一句话总结:

Git 是分布式的、以内容为核心、分支极快、合并强的现代工具。

2. Git 工作区、暂存区、版本库分别是什么?

  • 工作区(working directory):你的代码所在目录。
  • 暂存区(stage/index):git add 后进入的区域。
  • 本地仓库(repository):.git 目录,存储 commit、tree 对象。
  • 远程仓库(origin):GitLab/GitHub 上的仓库。

流程图:

工作区 -> git add -> 暂存区 -> git commit -> 本地库 -> git push -> 远程库

3. HEAD、working tree、index 的关系是什么?

  • HEAD:当前在哪个 commit 或分支上
  • Index:暂存区快照
  • Working Tree:工作区文件

图示:

HEAD -> Index -> Working Tree

4. Git reset、checkout、revert 区别?

命令 作用 是否修改历史 是否危险 场景
reset 回滚到旧版本 危险 你想重写历史
revert 生成一个新提交用于“撤销”某次提交 安全 已 push 的代码
checkout 切换分支/恢复文件 安全 查看旧代码

5. git fetch 和 git pull 的区别?

  • fetch:只拉取,不合并(安全)
  • pull = fetch + merge
  • 也可 pull –rebase

建议:

永远不要直接用 git pull,用:

git pull --rebase

二、分支 / 合并 / Rebase

6. merge 和 rebase 的区别?

merge

  • 会生成一个新的 merge commit
  • 保留分支历史
  • 历史变复杂

rebase

  • 把你的 commit“移动”到最新分支上
  • 历史更干净
  • 会重写 commit(危险)

原则:

  • 已 push 的保持 merge
  • 未 push 的可以 rebase

7. 什么是 fast-forward merge?

当目标分支没有额外 commit 时:

  • master: A -> B
  • feature: A -> B -> C

合并只需要移动指针

不会生成 merge commit。

8. 什么是冲突?如何解决?

冲突发生于两人同时修改同一代码区域。解决方式:

  1. 编辑冲突文件
  2. 删除标记行(«««<, =======, »»»>)
  3. git add
  4. git commit

9. 如何删除本地分支?

git branch -d feature/a

强制删除(未合并):

git branch -D feature/a

10. 如何删除远程分支?

git push origin --delete feature/a

三、高级

11. 什么是 Git 对象?commit 内部是什么?

Git 有 4 类对象:

  1. blob:文件内容
  2. tree:目录
  3. commit:包含 tree、作者、时间、父 commit
  4. tag:指向 commit

commit 是指向 tree 的指针。

12. Git 如何判断两个文件内容是否相同?

  • Git 不看修改时间
  • Git 看内容 SHA-1 哈希

内容一致 -> 哈希一致 -> 文件视为相同对象 -> 不重复存储

13. Git stash 用法?

保存当前未提交的修改:

git stash

恢复:

git stash pop

查看:

git stash list

14. 如何查看某个文件的修改历史?

git log -- filename

15. 如何查看某一行是谁改的?

git blame filename

16. 如何比较两个分支?

git diff branch1..branch2

17. 如何只 clone 某个分支?

git clone -b develop --single-branch URL

18. Git shallow clone(浅克隆)是什么?

git clone --depth 1 URL

只克隆最新 commit。

用途:CI 节省时间。

四、CI/CD、真实环境

19. GitLab CI 执行 merge 报错 MERGE_HEAD missing?

说明:

  • 当前目录没有 git merge
  • 需要 git fetch 和 git merge 的正确顺序

修复:

git fetch origin target-branch
git merge origin/target-branch

20. Git 凭证缓存有哪些方式?

优先级从高到低:

  1. 命令行 -c 设置
  2. 环境变量 GIT_*
  3. credential.helper=store(文件)
  4. 系统级 ~/.git-credentials

21. 如何让 git push 使用指定 credentials 文件?

git -c credential.helper="store --file=/tmp/cred" push

并且删除默认读取:

git -c credential.useHttpPath=true \
   -c credential.helper="store --file=/tmp/cred" \
   push

22. Git 如何强制不读取 ~/.git-credentials?

git -c credential.helper= push

或:

git -c credential.helper="store --file=/tmp/cred" \
   -c credential.helper= \
   push

五、场景

23. 如果你误删了一个分支,如何找回?

使用 reflog:

git reflog
git checkout <commit>
git branch recovery

24. 如何回滚一个已经 push 的 commit?

使用 revert:

git revert <commit>

不要用 reset!

25. 如何查看某个 Tag 是基于哪个 commit?

git show tagname

26. 一个文件被删了,如何找回?

git checkout HEAD -- path/file.txt

27. CI 中 git pull 总是要输入密码怎么办?

使用:

git -c credential.helper= -c credential.helper="store --file=/cred" pull

六、实战命令

常用查看命令

git status
git log --oneline --graph --decorate
git branch -a
git remote -v

常用合并命令

git merge xxx
git rebase xxx

常用撤销命令

git reset --hard
git revert
git checkout -- file