git merge 举例

介绍

git 主分支main, 创建子分支 feature_1

main 分支创建新文件 main.c, 同时子分支创建新文件 feature_1.c,现在想把 main 的修改记录同不到 feature_1

操作步骤

bash 复制代码
$ git branch
* main

$ git log
commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit


$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature_1
  remotes/origin/main

$ git checkout -b feature_1 remotes/origin/feature_1
Branch 'feature_1' set up to track remote branch 'feature_1' from 'origin'.
Switched to a new branch 'feature_1'

$ git status
On branch feature_1
Your branch is up to date with 'origin/feature_1'.

nothing to commit, working tree clean

$ git branch
* feature_1
  main
$ git branch -a
* feature_1
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature_1
  remotes/origin/main

$ git log
commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit

$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

$ touch main.c

$ echo main > main.c

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	main.c

nothing added to commit but untracked files present (use "git add" to track)

$ git add .

$ git commit -m "Add main.c"
[main a079cfa] Add main.c
 1 file changed, 1 insertion(+)
 create mode 100644 main.c

$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To ssh://gitlab.powertest.com.cn:12022/powertest/test-git-feature-1.git
   33063bf..a079cfa  main -> main

$ git log
commit a079cfa408b94e871796efc799a58b7f6ad011f1 (HEAD -> main, origin/main, origin/HEAD)
Author: powertest
Date:   Tue Feb 3 17:09:22 2026 +0800

    Add main.c

commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit

$ git checkout feature_1
Switched to branch 'feature_1'
Your branch is up to date with 'origin/feature_1'.

$ git log
commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit

$ git status
On branch feature_1
Your branch is up to date with 'origin/feature_1'.

nothing to commit, working tree clean

$ touch feature_1.c

$ echo feature_1 > feature_1.c 

$ git add .

$ git commit -m "Add feature_1.c"
[feature_1 e3b7643] Add feature_1.c
 1 file changed, 1 insertion(+)
 create mode 100644 feature_1.c

$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 294 bytes | 294.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: To create a merge request for feature_1, visit:
remote:   http://gitlab.powertest.com.cn/powertest/test-git-feature-1/-/merge_requests/new?merge_request%5Bsource_branch%5D=feature_1
remote: 
To ssh://gitlab.powertest.com.cn:12022/powertest/test-git-feature-1.git
   33063bf..e3b7643  feature_1 -> feature_1

1$ git log
commit e3b764366eb37080de262c344bebb8a41f0f0318 (origin/feature_1)
Author: powertest
Date:   Tue Feb 3 17:10:04 2026 +0800

    Add feature_1.c

commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit


$ git log --oneline --graph
* e3b7643 (HEAD -> feature_1, origin/feature_1) Add feature_1.c
* 33063bf Initial commit

$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

$ git log --oneline --graph
* a079cfa (HEAD -> main, origin/main, origin/HEAD) Add main.c
* 33063bf Initial commit

$ git checkout feature_1
Switched to branch 'feature_1'
Your branch is up to date with 'origin/feature_1'.

$ git merge main
Merge made by the 'ort' strategy.
 main.c | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 main.c

$ git log
commit 04841ece50fc5c453c89a78b1b337a46ffde5841 (HEAD -> feature_1)
Merge: e3b7643 a079cfa
Author: powertest
Date:   Tue Feb 3 17:11:48 2026 +0800

    Merge branch 'main' into feature_1

commit e3b764366eb37080de262c344bebb8a41f0f0318 (origin/feature_1)
Author: powertest
Date:   Tue Feb 3 17:10:04 2026 +0800

    Add feature_1.c

commit a079cfa408b94e871796efc799a58b7f6ad011f1 (origin/main, origin/HEAD, main)
Author: powertest
Date:   Tue Feb 3 17:09:22 2026 +0800

    Add main.c

commit 33063bff55e8d7142a2210a6973455abc98ec431
Author: powertest
Date:   Tue Feb 3 08:40:22 2026 +0000

    Initial commit

$ git log --oneline --graph
*   04841ec (HEAD -> feature_1) Merge branch 'main' into feature_1	# 最新合并提交
|\  
| * a079cfa (origin/main, origin/HEAD, main) Add main.c			# main分支最新业务提交
* | e3b7643 (origin/feature_1) Add feature_1.c
|/  
* 33063bf Initial commit						# 仓库初始提交(共同祖先)

详细解释

行 1:* 04841ec (HEAD -> feature_1) Merge branch 'main' into feature_1

空格结尾的*是合并提交专属标记(区别于普通提交的单个*),表示该提交有两个父指针,是分支合入的核心记录;

04841ec:该提交的7 位简短哈希值(唯一标识,完整哈希可通过git show 04841ec查看);

(HEAD -> feature_1):HEAD(Git 当前工作区的 "头指针")指向本地 feature_1 分支,表示当前正处于 feature_1 分支;

Merge branch 'main' into feature_1:标准的合并提交说明,含义是将本地 main 分支的最新代码,合入到了本地 feature_1 分支,这是本次合入操作的直接记录;

核心属性:该提交无任何业务代码,仅通过双父指针记录合入关系(父 1 = 行 3 的 e3b7643,父 2 = 行 2 的 a079cfa)。

|\(行 1 下方):表示分支分叉------ 从共同祖先(33063bf)开始,main 和 feature_1 分支并行开发,各自产生新的提交(a079cfa 和 e3b7643);

行 2:| * a079cfa (origin/main, origin/HEAD, main) Add main.c

| *:|表示分支线性延续,*是普通业务提交标记(单父指针),整体表示该提交属于main 分支的独立演进(与 feature_1 分支并行);

a079cfa:main 分支最新提交的简短哈希;

(origin/main, origin/HEAD, main):多分支指向标记,含义是:

main:本地 main 分支指向该提交;

origin/main:远程仓库(origin)的 main 分支指向该提交(本地 main 已与远程 main 同步);

origin/HEAD:远程仓库的 "头指针",默认指向远程主分支(origin/main),表示远程仓库最新状态就是该提交;

Add main.c:提交说明,明确该提交的业务操作------ 在 main 分支中新增了 main.c 文件(有实际代码变更)。

行 3:* | e3b7643 (origin/feature_1) Add feature_1.c

* |:*是普通业务提交标记,|表示与 main 分支并行开发,整体表示该提交属于feature_1 分支的独立演进;

e3b7643:feature_1 分支的业务提交哈希;

(origin/feature_1):远程仓库的 feature_1 分支指向该提交(本地该提交已推送到远程);

Add feature_1.c:提交说明,业务操作是 ------ 在 feature_1 分支中新增了 feature_1.c 文件(有实际代码变更)。

|/(行 3 下方):表示分支合入------ 两个并行分支的提交,最终通过合并提交(04841ec)完成融合,合入后分支历史重新汇聚。

行 4:* 33063bf Initial commit

*:普通提交标记,仓库的初始提交(无父指针);

33063bf:初始提交的哈希;

Initial commit:Git 初始化仓库时的默认提交说明,表示这是仓库的第一个提交,是 main 和 feature_1 分支的最新共同祖先(分支分叉点)。

相关推荐
AI_56783 小时前
Git冲突治理白皮书:智能标记与可视化协同的下一代解决方案
大数据·人工智能·git·机器学习
念丶小宇3 小时前
Git常用指令
大数据·git·elasticsearch
CSDN_RTKLIB4 小时前
Git Clone
git
中二病码农不会遇见C++学姐4 小时前
.env 文件是干啥的?为什么不能提交到 Git?
git·学习
CoderJia程序员甲5 小时前
GitHub 热榜项目 - 日榜(2026-02-03)
git·ai·开源·llm·github
宴之敖者、5 小时前
Linux——git和gdb
linux·运维·git
辰风沐阳6 小时前
git 忽略大小写(重命名文件)
大数据·git·elasticsearch
天麓6 小时前
git rebase 举例
git
艺杯羹6 小时前
Git文件状态管理:从基础到进阶的完整指南
大数据·git·elasticsearch·版本控制·git教程·代码管理·git基础