最短路径:冲突了不用慌,永远是同一个循环
bashgit status # 1. 看哪些文件冲突(UU 开头的) # 2. 打开文件改内容,删掉 <<<<<<< ======= >>>>>>> 这些标记 git add <文件> # 3. 标记"这个文件我解决了" git merge --continue # rebase 时换成 git rebase --continue想反悔就一句:
git merge --abort/git rebase --abort,一切回到动手之前。全部输出在 git 2.56.0.windows.2 / 2026-10 实机验证。
merge 还是 rebase:先看历史长什么样
两者的差别不在功能,在历史记录的形状。
同一个场景(主分支有了新提交,你的分支上也有两个提交):
$ git log --oneline --graph -6
* 9cda259 Merge branch 'feature/rebase' ← 新增的合并提交
|\
| * b0df011 r2: 分支提交二
| * 90c6721 r1: 分支提交一
* | 69dc0e3 m: 主分支另一条线
|/
* 910e896 master2: 改 a.txt$ git rebase master
Successfully rebased and updated refs/heads/feature/rebase.
$ git log --oneline --graph -6
* b0cd13c r2: 分支提交二 ← 提交还在,但 SHA 变了
* e0af8c6 r1: 分支提交一
* 69dc0e3 m: 主分支另一条线
* 910e896 master2: 改 a.txt选择标准:
| 场景 | 选哪个 | 原因 |
|---|---|---|
| 个人分支,合并进主干前 | rebase | 历史干净,一条直线,好 review |
| 主干、多人共享的分支 | merge | 不改写别人已经拉走的历史 |
| 已经推送过、别人可能基于它工作 | merge | 同上,rebase 会让别人的本地历史错乱 |
| 只想把功能合进来,不在乎历史形状 | 都行 | merge 更省事,不用处理一串冲突 |
一句话记法:没推出去的分支随便 rebase,推出去的分支只 merge。
冲突:是怎么来的,怎么解决
冲突不是错误,是 Git 在说:"同一个位置有两个版本,我判断不了留哪个,你来定。"
现场
$ git merge topic
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.打开冲突文件,Git 把两个版本都留在里面:
$ cat a.txt
<<<<<<< HEAD
master version ← 当前分支(HEAD)的版本
=======
topic version ← 要合并进来的版本的
>>>>>>> topic解决三步
- 编辑文件,留下你想要的内容,并删掉
<<<<<<</=======/>>>>>>>这三行标记 git add <文件>—— 这一步的意思不是"暂存改动",而是**"这个文件的冲突我处理完了"**git commit(merge 场景可以git commit --no-edit直接用默认信息)
$ git add a.txt
$ git commit --no-edit
[master 19c1b43] Merge branch 'topic'
$ git log --oneline --graph -4
* 19c1b43 Merge branch 'topic'
|\
| * 65e0c9f topic: 改 a.txt
* | 7141189 master: 也改 a.txt
|/
* f380152 Revert "feat: 一个有问题的提交"中途想放弃
$ git status --short
UU a.txt ← UU = 双方都改了,还没解决
$ git merge --abort
$ git status --short ← 干净了,回到 merge 之前冲突状态怎么看
git status --short 前两位是"暂存区/工作区"状态:
| 标记 | 含义 |
|---|---|
UU | 两个分支都改了,冲突未解决 |
AA | 双方都新增了同名文件 |
DU / UD | 一方删除、一方修改 |
M / M | 已暂存 / 未暂存(正常状态) |
rebase 遇到冲突
git rebase 会逐个提交重放,所以可能冲突多次。每次冲突的处理循环和 merge 一样(改文件 → add),但接下来不是 commit,而是告诉 rebase 怎么继续:
$ git rebase master
CONFLICT (content): Merge conflict in a.txt
error: could not apply 44584f3... topic: 改 a.txt
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".三个出口,按你的意图选:
| 命令 | 什么时候用 | 真实结果 |
|---|---|---|
git rebase --continue | 冲突已解决,继续重放剩下的提交 | Successfully rebased and updated refs/heads/topic. |
git rebase --skip | 这个提交的内容已经不需要了(常见于它已被别人合过) | 跳过该提交,后续继续 |
git rebase --abort | 不想干了,整体回退 | 回到 rebase 之前的状态 |
# 解决完冲突后
$ git add a.txt
$ git rebase --continue
[detached HEAD d855c37] topic: 改 a.txt
Successfully rebased and updated refs/heads/topic.
$ git log --oneline --graph --all
* d855c37 topic: 改 a.txt
* d8d5de6 master: 也改 a.txt
* efd35a7 init: 初始提交坑:脚本/IDE 里 rebase --continue 会报 "Terminal is dumb"
$ git rebase --continue
error: Terminal is dumb, but EDITOR unset
Please supply the message using either -m or -F option.
error: could not commit staged changes.rebase 完成时要打开编辑器确认提交信息,而非交互环境(脚本、CI、部分 IDE 终端)没有编辑器,于是卡住。
解决:临时指定一个"直接接受"的编辑器。
GIT_EDITOR=true git rebase --continueWindows PowerShell 下写 $env:GIT_EDITOR="true"; git rebase --continue。
注意报错后仓库停在 rebase 中途,此时切分支会失败(fatal: cannot switch branch while rebasing)。别乱试,先 --continue 或 --abort 收尾。
cherry-pick:只摘一个提交过来
适用场景:同一个修复要在多个分支上生效,或者只想从别人的分支里拿一个提交。
$ git cherry-pick e1e076cd347891ad90b4cc48f5b417b067eef169
[master e1e076c] feat: 只想要这一个提交
1 file changed, 1 insertion(+)
create mode 100644 cherry.txt
$ git log --oneline -3
e1e076c feat: 只想要这一个提交 ← 内容一样,SHA 不同
8641456 init: 初始提交也可以一次摘多个提交(git cherry-pick A..B,左开右闭,不含 A)。
cherry-pick 的代价
它复制提交内容、生成新 SHA,所以:同一个改动在历史里出现了两次。长期反复 cherry-pick 会导致同一 bug 修了又出现、git log -S 查出来一堆重复。能用 merge/rebase 解决就别用它。
stash:临时把现场收起来
写一半需要切分支去救火,又不想提交半成品 —— 这就是 stash 的场景。
$ git status --short
M a.txt
?? w.txt
$ git stash push -u -m "写一半的改动" # -u 连未跟踪文件一起收,-m 给个能认出来的名字
Saved working directory and index state On master: 写一半的改动
$ git status --short # 干净了,可以放心切分支
$ git stash list
stash@{0}: On master: 写一半的改动
$ git stash show -p # 看看到底存了什么
diff --git a/a.txt b/a.txt
@@ -1 +1 @@
-hello
+hello modified
$ git stash pop # 取出来并从栈里删除| 命令 | 作用 |
|---|---|
git stash push -u -m "说明" | 存起来(-u 含未跟踪文件,强烈建议加) |
git stash list | 看有几份 |
git stash show -p stash@{0} | 看某一份的内容 |
git stash pop | 取最新一份并删除记录 |
git stash apply stash@{0} | 取出来但保留记录(想在多处用同一份) |
git stash drop stash@{0} | 删掉某一份 |
不加 -u 是经典事故
默认只暂存已被 Git 跟踪的文件。新写的、从没 git add 过的文件会被留在工作区,一 stash 一切分支就跟着过去了,或者以为存好了其实没存。养成习惯:git stash push -u。
推送被拒绝:non-fast-forward
这是最常遇到的报错,很容易被误当成权限问题。
$ git push origin master
To ~/remote.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '~/remote.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.意思:远端有你本地没有的提交,直接覆盖会把别人的工作弄丢,所以 Git 拒绝。
正确姿势(保持历史直线):
$ git pull --rebase origin master
From ~/remote.git
* branch master -> FETCH_HEAD
Rebasing (1/1)Successfully rebased and updated refs/heads/master.
$ git push origin master
To ~/remote.git
b871bef..27f839d master -> master
$ git log --oneline --graph
* 27f839d mine: 我本地的提交
* b871bef teammate: 别人的提交
* 4979213 init: 初始提交不要用 git push -f 解决这个报错
看到 rejected 就 -f,等于把别人的提交直接从远端抹掉。要强推也只用 --force-with-lease,而且只在自己独占的分支上。详见 撤销与找回。
分支操作速查
# 查看
git branch # 本地分支
git branch -a # 含远程分支
git branch -v # 带最后一次提交
# 新建 / 切换(git 2.23+ 推荐写法)
git switch -c feature/x # 新建并切过去(老写法 git checkout -b)
git switch -c feature/x origin/feature/x # 基于远程分支新建
# 删除
git branch -d feature/x # 已合并才让删
git branch -D feature/x # 强制删(慎用;删错了用 reflog 救)
git push origin -d feature/x # 删远程分支
# 合并
git merge feature/x # 普通合并
git merge --no-ff feature/x # 强制生成合并提交(保留"这里合并过"的痕迹)
git merge --squash feature/x # 把整个分支压成一个改动,不提交,等你手动 commit
# 同步
git fetch --prune # 拉取远端状态,并清掉已删除的远程分支引用switch / restore 取代 checkout
git checkout 一个命令管三件事(切分支、切提交、还原文件),是 Git 公认的设计失误。新写法把它拆开了:
| 老写法 | 新写法 |
|---|---|
git checkout -b x | git switch -c x |
git checkout x | git switch x |
git checkout -- <文件> | git restore <文件> |
git checkout <commit> -- <文件> | git restore --source=<commit> <文件> |
