官方帮助文档

$ git help merge
GIT-MERGE(1)                         Git Manual                         GIT-MERGE(1)
NAMEgit-merge - Join two or more development histories togetherSYNOPSISgit merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit][--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]][--[no-]allow-unrelated-histories][--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>...]git merge (--continue | --abort | --quit)DESCRIPTIONIncorporates changes from the named commits (since the time their historiesdiverged from the current branch) into the current branch. This command isused by git pull to incorporate changes from another repository and can beused by hand to merge changes from one branch into another.Assume the following history exists and the current branch is "master":A---B---C topic/D---E---F---G masterThen "git merge topic" will replay the changes made on the topic branch sinceit diverged from master (i.e., E) until its current commit (C) on top ofmaster, and record the result in a new commit along with the names of the twoparent commits and a log message from the user describing the changes.A---B---C topic/         \D---E---F---G---H masterThe second syntax ("git merge --abort") can only be run after the merge hasresulted in conflicts. git merge --abort will abort the merge process and tryto reconstruct the pre-merge state. However, if there were uncommittedchanges when the merge started (and especially if those changes were furthermodified after the merge was started), git merge --abort will in some casesbe unable to reconstruct the original (pre-merge) changes. Therefore:Warning: Running git merge with non-trivial uncommitted changes isdiscouraged: while possible, it may leave you in a state that is hard to backout of in the case of a conflict.The third syntax ("git merge --continue") can only be run after the merge hasresulted in conflicts.OPTIONS--commit, --no-commit

Git Merge —— Fast Forward ( 快进到某个分支)

  • master不变,新创建一个分支bugfix,在bugfix分支上修改代码提交commit,完后在master分支上,把master分支上指向bugfix上最新的commit对象,
$ cat .git/HEAD # 我们现在head指向master分支
ref: refs/heads/master
$ git log
commit ef1ab1732d63429bee8a8e8d50adf315eca3a74b (HEAD -> master) # HEAD 指向master
...
$ git checkout -b fix-xxx
切换到一个新分支 'fix-xxx'
$ git branch
* fix-xxxmaster
$ git log
commit ef1ab1732d63429bee8a8e8d50adf315eca3a74b (HEAD -> fix-xxx, master)
$ vim hello.txt
$ git add hello.txt
$ git commit -m 'fix'
[fix-xxx 351ed28] fix1 file changed, 1 insertion(+), 1 deletion(-)
$  git log
commit 351ed2832ff8d44d3a9bcd459d2d3c750606637a (HEAD -> fix-xxx)
Author: xxx <xxx.com>
Date:   xxxfix
commit ef1ab1732d63429bee8a8e8d50adf315eca3a74b (master)
...
$  git checkout master
切换到分支 'master'
$ git log
commit ef1ab1732d63429bee8a8e8d50adf315eca3a74b (HEAD -> master)
...
$  git merge fix-xxx
Updating ef1ab17..351ed28 # 指针从我们commit的ef1ab17——>351ed28
Fast-forward # 场景hello.txt | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
$ git log
commit 351ed2832ff8d44d3a9bcd459d2d3c750606637a (HEAD -> master, fix-xxx)
Author: xxx <xxx.com>
Date:   xxxfix
commit ef1ab1732d63429bee8a8e8d50adf315eca3a74b (HEAD -> master)
...
$  tree .git
.git
├── COMMIT_EDITMSG
├── HEAD
├── ORIG_HEAD # merge 之后多出来的,
├── config
├── description
├── hooks
├── index
├── info
│   ├── exclude
│   └── refs
├── logs
│   ├── HEAD
│   └── refs
│       └── heads
│           ├── fix-xxx
│           └── master
├── objects
│   ├── 35
│   │   └── 1ed2832ff8d44d3a9bcd459d2d3c750606637a
│   ├── 56
│   │   └── 00391ce473902576cb284ecb48410ea7c9f308
│   ├── 91
│   │   └── b84d9a6b5b007824972f6f8478e2a2eb662b54
│   ├── info
│   │   ├── commit-graph
│   │   └── packs
│   └── pack
│       ├── pack-0c8c5e08719b5d74e9c03714a71555a9bc1c9632.idx
│       └── pack-0c8c5e08719b5d74e9c03714a71555a9bc1c9632.pack
├── packed-refs
└── refs├── heads│   ├── fix-xxx│   └── master└── tags
$ cat .git/HEAD
ref: refs/heads/master
$ cat .git/ORIG_HEAD
ef1ab1732d63429bee8a8e8d50adf315eca3a74b # merge 之前master指向最新的commit对象
$ cat .git/refs/heads/master
351ed2832ff8d44d3a9bcd459d2d3c750606637a # merge 之后master指向的commit对象
我们可以在这时进行回滚操作
  • Git认为Fast-forward 这种场景很危险,我们需要有回滚操作,也就是撤回当上一个版本

$ git reset ORIG_HEA
重置后取消暂存的变更:
M   hello.txt
$ git log# 重新指向 merge 之前指向的commit对象
commit ef1ab1732d63429bee8a8e8d50adf315eca3a74b (HEAD -> master)
$  git status
位于分支 master
尚未暂存以备提交的变更:(使用 "git add <文件>..." 更新要提交的内容)(使用 "git restore <文件>..." 丢弃工作区的改动)修改:     hello.txt修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
# 我们commit回滚到merge之前的master状态,更改会放到工作区中# 扩展 https://www.cnblogs.com/lfxiao/p/9378763.html

Git Merge —— 3 Way merge

在我们merge之前,已经有人merge过其他分支

修改一样的文件,产生冲突
 git checkout -b fix-xxx2
切换到一个新分支 'fix-xxx2'
$ vim hello.txt
$ git add hello.txt
$ git commit -m 'fix 2'
[fix-xxx2 79c4f6a] fix 21 file changed, 1 insertion(+)
$ git log
commit 79c4f6aede40a7042add2f89fcd80d260041c0a5 (HEAD -> fix-xxx2)
Author: hh <hh@123.com>
Date:   Sun Mar 28 14:25:38 2021 +0800fix 2$ tree .git/objects
.git/objects
├── 17
│   └── 8724d997ef9fc1141e797171d682553136edfc
├── 41
│   └── 1b486eb6afe280fb7e26ed1b00b39c11af4462
├── 61
│   └── 5640b3bdc533cc5180a00ebfe8675115aa11ac
├── 79
│   └── c4f6aede40a7042add2f89fcd80d260041c0a5
├── b4
│   └── bfc685e634816f6bad0b51bac882173bc8e54a
├── e4
│   └── 582fa0fc21613231e5bb8bf284a7db9df0937e
├── info
│   ├── commit-graph
│   └── packs
└── pack├── pack-3c257c1a1c5cddbd7adcbbcd0b2eae95178d8e6b.idx└── pack-3c257c1a1c5cddbd7adcbbcd0b2eae95178d8e6b.pack8 directories, 10 files
$ git checkout master
切换到分支 'master'
$ git merge fix-xxx2
更新 ef1ab17..79c4f6a
Fast-forwardhello.txt | 1 +1 file changed, 1 insertion(+)
$ git merge fix-xxx
自动合并 hello.txt
冲突(内容):合并冲突于 hello.txt
自动合并失败,修正冲突然后提交修正的结果。
$ vim hello.txt #处理我们产生的冲突代码
hello git
<<<<<<< HEAD
fix bug
fix bug
=======
fix
>>>>>>> fix-xxx
$ git status
位于分支 master
您有尚未合并的路径。(解决冲突并运行 "git commit")(使用 "git merge --abort" 终止合并)未合并的路径:(使用 "git add <文件>..." 标记解决方案)双方修改:   hello.txt修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
$ git add hello.txt
$ git status
位于分支 master
所有冲突已解决但您仍处于合并中。(使用 "git commit" 结束合并)要提交的变更:修改:     hello.txt$ git commit -m 'fix merge bug-fix-xxx'
[master c21d1eb] fix merge bug-fix-xxx
$ git log
commit c21d1eb5cba29552302382495fb9dbf9bae66f47 (HEAD -> master) # 处理完冲突后的产生的分支
Merge: 79c4f6a 351ed28
Author: XXX
Date:   XXXfix merge bug-fix-xxxcommit 79c4f6aede40a7042add2f89fcd80d260041c0a5 (fix-xxx2) # fix-xxx2没有变化
Author: XXX
Date:   XXXfix 2commit 351ed2832ff8d44d3a9bcd459d2d3c750606637a (fix-xxx) # fix-xxx
Author: XXX
Date:   XXXfixcommit c21d1eb5cba29552302382495fb9dbf9bae66f47 # merge 之前master最新的commit
...
$ tree .git/objects
.git/objects
├── 17
│   └── 8724d997ef9fc1141e797171d682553136edfc
├── 41
│   └── 1b486eb6afe280fb7e26ed1b00b39c11af4462
├── 61
│   └── 5640b3bdc533cc5180a00ebfe8675115aa11ac
├── 79
│   └── c4f6aede40a7042add2f89fcd80d260041c0a5
├── b4
│   └── bfc685e634816f6bad0b51bac882173bc8e54a
├── c2 # 处理merge冲突后的产生的对象
│   ├── 1d1eb5cba29552302382495fb9dbf9bae66f47 # commit对象
│   └── 7e40a839d857c095dd5e3bda0aaf2a61224cb5 # tree对象
├── c7
│   └── 037c0fda18887223fc27048d5491bd4627471a
├── e4
│   └── 582fa0fc21613231e5bb8bf284a7db9df0937e
├── f9
│   └── 9c1c17a0c67bd6df61ed7fae3e7842b8723105
├── info
│   ├── commit-graph
│   └── packs
└── pack├── pack-3c257c1a1c5cddbd7adcbbcd0b2eae95178d8e6b.idx└── pack-3c257c1a1c5cddbd7adcbbcd0b2eae95178d8e6b.pack11 directories, 14 files
$  git cat-file -t  c21d1e
commit
git cat-file -p c21d1e
tree c27e40a839d857c095dd5e3bda0aaf2a61224cb5
parent 79c4f6aede40a7042add2f89fcd80d260041c0a5 # merge 之前master最新的commit对象
parent 351ed2832ff8d44d3a9bcd459d2d3c750606637a # 所merge的commit对象
author hh <hh@123.com> 1616913093 +0800
committer hh <hh@123.com> 1616913093 +0800fix merge bug-fix-xxx
$  git cat-file -t  c27e40
tree
$ git cat-file -p c27e40
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    empty_file.txt
040000 tree c2ed92794640553084dfab1d35c0f468342e02a6    folder1
100644 blob c7037c0fda18887223fc27048d5491bd4627471a    hello.txt
100644 blob 0f7b21a50d13eb924a712a1cb763dca8ef69f6f3    hello_copy.txt
修改不一样的文件,不会产生冲突
#
git checkout fix-xxx
切换到分支 'fix-xxx'
$ vim fix-xxx.txt
$ git add fix-xxx.txt
$ git commit -m 'add fix-xxx'
[fix-xxx 8814570] add fix-xxx1 file changed, 1 insertion(+)create mode 100644 fix-xxx.txt
$ git checkout master
切换到分支 'master'
$ git checkout fix-xxx2
切换到分支 'fix-xxx2'
$ vim fix-xxx2.txt
$ git add fix-xxx2.txt
$ git status
位于分支 fix-xxx2
要提交的变更:(使用 "git restore --staged <文件>..." 以取消暂存)新文件:   fix-xxx2.txt$ git commit -m 'add fix-xxx2'
[fix-xxx2 6048e4f] add fix-xxx21 file changed, 1 insertion(+)create mode 100644 fix-xxx2.txt
$ git checkout master
切换到分支 'master'
$ git merge fix-xxx2
# 跳出一个merge commit 的vim界面,我们填写我们的commit message,保存
Merge made by the 'recursive' strategy.fix-xxx2.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 fix-xxx2.txt
$ git merge fix-xxx
Merge made by the 'recursive' strategy.fix-xxx.txt | 1 +1 file changed, 1 insertion(+)create mode 100644 fix-xxx.txt
$ git log
commit daeef85f655ca2a2f240839edfbbc858b32bce0b (HEAD -> master) # merge commit
Merge: ac68fc7 8814570
Author: hh <hh@123.com>
Date:   Sun Mar 28 14:49:00 2021 +0800Merge branch 'fix-xxx' into masterfix bug fix xxxcommit ac68fc7a6f26ec18585249989a5023a940bc9974 # merge commit
Merge: c21d1eb 6048e4f
Author: hh <hh@123.com>
Date:   Sun Mar 28 14:48:42 2021 +0800Merge branch 'fix-xxx2' into mastercommit 6048e4fc794e7df0192939309d8be0d47ca3f71a (fix-xxx2)
Author: hh <hh@123.com>
Date:   Sun Mar 28 14:48:13 2021 +0800add fix-xxx2commit 88145707b4619e8ada9308178bec3cd54b18f290 (fix-xxx)
Author: hh <hh@123.com>
Date:   Sun Mar 28 14:46:46 2021 +0800add fix-xxxcommit c21d1eb5cba29552302382495fb9dbf9bae66f47
Merge: 79c4f6a 351ed28
Author: hh <hh@123.com>
Date:   Sun Mar 28 14:31:33 2021 +0800fix merge bug-fix-xxx
#
tree .git/objects
.git/objects
├── 17
│   └── 8724d997ef9fc1141e797171d682553136edfc
├── 38
│   └── da773d597dd7a0c0f829ebc9435913347bb4ba
├── 41
│   └── 1b486eb6afe280fb7e26ed1b00b39c11af4462
├── 60
│   └── 48e4fc794e7df0192939309d8be0d47ca3f71a
├── 61
│   └── 5640b3bdc533cc5180a00ebfe8675115aa11ac
├── 68
│   └── 0ee446d66eab3822ad022908fc03a08f3979de
├── 79
│   └── c4f6aede40a7042add2f89fcd80d260041c0a5
├── 80
│   └── 2ad127fc8baeb11b1a78fd939b5c1fa20636a1
├── 88
│   └── 145707b4619e8ada9308178bec3cd54b18f290
├── ac
│   ├── 1fb501cc308e056a74e3ede5e30c48ac97d2b6
│   └── 68fc7a6f26ec18585249989a5023a940bc9974
├── b4
│   └── bfc685e634816f6bad0b51bac882173bc8e54a
├── c0
│   └── 9cc07facbad3ad88b6384e0f9529ac2a429f30
├── c2
│   ├── 1d1eb5cba29552302382495fb9dbf9bae66f47
│   └── 7e40a839d857c095dd5e3bda0aaf2a61224cb5
├── c7
│   └── 037c0fda18887223fc27048d5491bd4627471a
├── c8
│   └── 1e07cb60fa336b16995dadcd13e50f9c7f9dd4
├── da
│   └── eef85f655ca2a2f240839edfbbc858b32bce0b
├── e4
│   └── 582fa0fc21613231e5bb8bf284a7db9df0937e
├── f9
│   └── 9c1c17a0c67bd6df61ed7fae3e7842b8723105
├── info
│   ├── commit-graph
│   └── packs
└── pack├── pack-3c257c1a1c5cddbd7adcbbcd0b2eae95178d8e6b.idx└── pack-3c257c1a1c5cddbd7adcbbcd0b2eae95178d8e6b.pack20 directories, 24 files

Git基本操作- 命令 merge - 分支合并、处理冲突相关推荐

  1. svn多分支合并主干冲突 svn Working copy and merge source not ready for reintegration

    svn多分支合并主干方法 分支a合并到本地主干后,本地主干提交代码到svn主干,这时主干版本改变,分支b再提交代码到主干会报冲突,版本不统一,这时的解决办法是: 方法1.将主干代码合并到分支b,提交分 ...

  2. Git分支合并请求冲突处理规则

    1. Git分支合并请求冲突处理规则 1.1. 问题描述 提交分支合并请求时,出现冲突,提示为 此合并请求有变更与目标分支冲突. 示例如图: 1.2. 原因 合并的目标分支,已经有了更改,比如其他分支 ...

  3. 【Git】如何进行分支合并

    [Git]如何进行分支合并 在平时的开发中,我们通常会在 Git上建立多个分支,以方便代码的管理与维护,比如[master-dev]开发模型,这种开发模型就是 master存放已完成的代码,而 dev ...

  4. git can‘t merge 的处理 代码冲突问题的解决

    git can't merge 的处理 代码冲突问题的解决 参考文章: (1)git can't merge 的处理 代码冲突问题的解决 (2)https://www.cnblogs.com/jint ...

  5. GIT分支合并与冲突解决

    git中的分支十分轻量,因此我们在使用git的时候会频繁的用到分支,不可避免的需要进行分支合并. 在git中合并分支有两种选择:merge 和 rebase.但是,无论哪一种,都有可能产生冲突. 一. ...

  6. git的分支合并与冲突

    一.分支合并 1.切换到主分支 git checkout master 2.合并分支 git merge 分支名 git push 注: 当main分支上如果已经新增或修改了,则会跳出如下: 你可以选 ...

  7. git命令进行分支合并

    方法1.使用git命令合并分支 1.使用命令查看当前所属分支 git branch 2.拉取项目最新的代码 git pull 3.将A分支的代码合并至B分支上 //首先切换分支到想合并的分支上 git ...

  8. git命令创建分支|合并分支|删除分支

    一. 创建test分支提交步骤 1.列出所有分支: # git branch -a 2.创建test分支: # git branch test 3.切换到test分支: # git checkout ...

  9. 【工具篇】git常用命令--关于分支切换、解决冲突覆盖

    目录 关于分支 解决冲突覆盖代码 提交记录码获取的地方: 关于分支 git branch 查看当前分支 git checkout -b dev-wangxiaobo 新建本地分支 git branch ...

最新文章

  1. Scrum Master的职业发展路线
  2. xdebug 远程调试
  3. 汇编语言-012(扩展加法指令ADC、带借位减法指令SBB、执行加法后进行ASCII调整指令、AAS 、AAM、AAD 、DAA指令将和数转成压缩十进制格式)
  4. 2018.09.22 atcoder Integers on a Tree(构造)
  5. python清除实例化类_在Python中,如何尝试(和排除)类的实例化?
  6. 关于PHP页面显示乱码问题的解决
  7. TF-IDF算法-golang实现
  8. neo4j 如何删除所以的节点和关系
  9. c++ 创建文件夹_C/C++编程笔记:vscode搭建C/C++环境,初学编程可收藏
  10. 何不给你单调的鼠标指针换一个好看的样式?(Windows系统下)
  11. 计算机编程php网页源码水果网上销售系统mysql数据库web结构html布局
  12. swiper5 点击图换下一张_软网推荐:骗倒社交好友 制作另类隐藏图
  13. java中文繁体简体转换
  14. IIS环境网站http服务器内部500错误的解决方法
  15. java okhhtp下载学信网学籍信息
  16. 七天时间从电脑小白变成编程高手
  17. iOS双重验证设备无法接收验证码,更新手机号。
  18. 湍流 Spectrum 与 Cascade 的理解
  19. c语言16进制转换成文本显示,文本如何转换成16进制字节集数据
  20. 爬虫——把jason格式用pandas化成dataframe

热门文章

  1. MLOps领域人工智能创业公司一览
  2. 机器学习-白板推导系列笔记(三十四)-MDP
  3. 拿不到 offer 免费学 | 廖雪峰的“大数据分析全栈工程师”课程第11期开始招生...
  4. 上海大火,据说事出有因--上海公寓大火原因:无证电焊工违章操作引发火灾
  5. Python菱形继承的初始化问题和继承顺序
  6. C++使用VARIANT实现二维数组的操作、怎么使用COleSafeArray实现二维数组将字符串写入excel
  7. [Rockyxia Studio 2010] 新版首页2011-03-22上线
  8. linux php pdo mssql,Linux下PHP连接MSSQL2005/2008
  9. 笔试 -- SQL语句之横竖表转换
  10. 3 Genymotion的安装与使用