Git笔记:分支管理2之合并
多人协作工作模式:
1、首先,git push origin <branch_name> 推送自己的修改
2、若推送失败,因远程分支比你本地更新,需要先用git pull 试图合并
3、如果合并有冲突,则解决冲突,并在本地提交
4、没有冲突或冲突已解决,再用git push origin <branch_name>推送就能成功
若git pull 提示no tracking information,则说明本地分支和远程分支的链接没有创建,用命令git branch --set-upstream-to=origin/<branch_name> <branch_name>
29447@GW64 /d/myProject (dev)
$ git branch --set-upstream-to=origin/dev dev
branch 'dev' set up to track 'origin/dev'. # 本地分支与远程分支关联
29447@GW64 /d/myProject (dev)
$ git push --set-upstream origin dev
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 317 bytes | 317.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:bai-cao/python_clone.git
406c7bd..81edaad dev -> dev
branch 'dev' set up to track 'origin/dev'. # 本地分支与远程分支关联
1、 解决合并冲突
在合并过程中出现冲突时,Git会标记冲突文件,需要手动解决冲突
打开冲突文件,按照标记解决冲突
29447@GW64 /d/myProject (main)
$ git merge dev -m "merge dev"
Auto-merging dev.txt # 冲突文件
CONFLICT (content): Merge conflict in dev.txt # 冲突内容标注在该文件
Automatic merge failed; fix conflicts and then commit the result.
29447@GW64 /d/myProject (main|MERGING)$ # 进入对应merge模式
29447@GW64 /d/myProject (main|MERGING)
$ git status -s
UU dev.txt
29447@GW64 /d/myProject (main|MERGING)
$ git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: dev.txt
no changes added to commit (use "git add" and/or "git commit -a")
29447@GW64 /d/myProject (main|MERGING)
$
冲突文件
Git 用
<<<<<<,======,>>>>>>标注出不同分支的内容
标记冲突解决完成后:
git add <conflict-file>
git commit -m "conflict-file"
29447@GW64 /d/myProject (main|MERGING)
$ git commit -am "Merge conflict in dev.txt "
[main c4b8dec] Merge conflict in dev.txt
29447@GW64 /d/myProject (main)
$ git merge dev -m "merge dev"
Already up to date.
29447@GW64 /d/myProject (main)
$ git log --graph --oneline --since=3.hours.ago
* c4b8dec (HEAD -> main) Merge conflict in dev.txt
|\
| * aad601a (dev) update dev.txt
* | 7d7d4db update test.txt&dev.txt
* | cef3c4d (origin/main, origin/HEAD) merge test branch
|\ \
| |/
|/|
| * 629c3fc branch test add test.txt
* dc79c57 branch dev add dev.txt
2、pull时冲突
29447@GW64 /d/myProject (main)
$ touch main.txt
29447@GW64 /d/myProject (main)
$ git add .
29447@GW64 /d/myProject (main)
$ git commit -m "main:add main.txt"
[main 9449a11] main:add main.txt
1 file changed, 3 insertions(+)
create mode 100644 main.txt
29447@GW64 /d/myProject (main)
$ git push origin main
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 356 bytes | 356.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To github.com:bai-cao/python_clone.git
8b58b4c..9449a11 main -> main
29447@GW64 /d/myProject (main)
$ git checkout test_branch
Switched to branch 'test_branch'
Your branch is up to date with 'origin/test_branch'.
29447@GW64 /d/myProject (test_branch)
$ touch main.txt
29447@GW64 /d/myProject (test_branch)
$ git add .
29447@GW64 /d/myProject (test_branch)
$ git commit -m "test_branch:add main.txt"
[test_branch 47a9da0] test_branch:add main.txt
1 file changed, 3 insertions(+)
create mode 100644 main.txt
29447@GW64 /d/myProject (test_branch)
$ git pull origin main
From github.com:bai-cao/python_clone
* branch main -> FETCH_HEAD
Auto-merging main.txt
CONFLICT (add/add): Merge conflict in main.txt
Automatic merge failed; fix conflicts and then commit the result.
image.png
1、避免频繁更新:可能导致代码冲突和混乱,建议在有需要的时候再更新版本
2、注意冲突处理:如果在更新版本的过程中出现冲突,需要及时处理冲突,避免影响其他开发人员的工作
3、提交有意义的commit:更新版本之前,建议先提交有意义的commit,方便其他开发人员了解代码变更的目的。
3、 git merge 参数
git merge --fast-forward <branch> # 默认
dev多次commit,再合并到master.若master没有任何提交,则直接把master之后指向dev最后一提交节点上,分支提交记录为一条直线。最后master指针和dev指针都指向原来dev的最后一次提交
通常用于本地自己开发,协作开发用的很少
git merge --no-ff <branch> # 禁用fast-forward模式。生成一个新的提交节点,来指示这个合并操作。分支提交记录也能看到master和dev各自的线,以及合并过程。推荐使用(便于追溯到完整的分支提交历史)
多人协作时常用,一般都要禁用,这样提交记录更清晰
git merge --squash # 拉取的分支提交记录过多,合并到master时不想太乱希望把这些冗杂记录汇总成一条记录
git merge
29447@GW64 /d/myProject (main)
$ git merge -m "【main branch】merge dev with fast-forwad" dev
Updating 9bfc88b..f8f9251
Fast-forward (no commit created; -m option ignored)
readme.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -7
* f8f9251 (HEAD -> main, dev) 【dev branch】v9 修改readme.md
* 9bfc88b [main branch]merge dev with no-ff
|\
| * 5d34cb0 【dev branch】修改readme.txt v8
|/
* 627b11a 【main|merging】confilct fixed
|\
| * 3e38705 【feature1 branch】修改readme.md
* | cde7ee2 【main branch】修改readme.md
|/
* 2f1909d [dev branch]:v6修改readme.md
29447@GW64 /d/myProject (main)
$ git merge dev --no-ff -m "【main branch】merge dev with no-ff"
Merge made by the 'ort' strategy.
readme.md | 1 +
1 file changed, 1 insertion(+)
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -7
* 2912197 (HEAD -> main) 【main branch】merge dev with no-ff # 多一条merge的提交记录
|\
| * 4fbfa48 (dev) 【dev branch】v10 修改readme 第10次
|/
* f8f9251 【dev branch】v9 修改readme.md
* 9bfc88b [main branch]merge dev with no-ff
|\
| * 5d34cb0 【dev branch】修改readme.txt v8
|/
* 627b11a 【main|merging】confilct fixed
|\
| * 3e38705 【feature1 branch】修改readme.md
29447@GW64 /d/myProject (main)
$ git merge dev --squash
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested
29447@GW64 /d/myProject (main)
$ cat readme.md # main分支中文件以及与dev分支一致,证明已成功合并
v12:2025/6/21 21:05 删除上述所有
v13:How are you? I'm fine,thank you.And you?
29447@GW64 /d/myProject (main)
$ git log --graph --oneline -9 # log中无dev分支2次提交记录
* 2912197 (HEAD -> main) 【main branch】merge dev with no-ff
|\
| * 4fbfa48 【dev branch】v10 修改readme 第10次
|/
* f8f9251 【dev branch】v9 修改readme.md
* 9bfc88b [main branch]merge dev with no-ff
|\
| * 5d34cb0 【dev branch】修改readme.txt v8
|/
* 627b11a 【main|merging】confilct fixed
|\
| * 3e38705 【feature1 branch】修改readme.md
* | cde7ee2 【main branch】修改readme.md
|/
* 2f1909d [dev branch]:v6修改readme.md
29447@GW64 /d/myProject (main)
$ git reflog -5 # HEAD@{1}、HEAD@{2}是dev分支上的两次提交;log未展示
2912197 (HEAD -> main) HEAD@{0}: checkout: moving from dev to main
8d32ebd (dev) HEAD@{1}: commit: 【dev branch】v12 修改readme 第12次
e0ab264 HEAD@{2}: commit: 【dev branch】v11 修改readme 第11次
4fbfa48 HEAD@{3}: checkout: moving from main to dev
2912197 (HEAD -> main) HEAD@{4}: merge dev: Merge made by the 'ort' strategy.