Git(10)-merge

merge相关的操作的命令

git checkout master
git merge alternate
# 解决冲突 .....
git add file_1
git commit -m "Add slternate line 5, 6"
git reset --hard HEAD     # before merge commit将工作目录和索引还原到git merge命令前状态
git reset --hard ORIG_HEAD # after merge commit 将工作目录和索引还原到git merge命令前状态
git checkout -m            # 把冲突解决方案搞砸了,想要返回到冲突的原始状态

合并操作:检出目标分支->确保工作区干净->执行合并操作。执行合并操作后,另一个分支不受影响。

1. 无冲突合并

合并不涉及相同文件的相同部分

chenyingying01@cyy merge % git checkout master
Switched to branch 'master'
chenyingying01@cyy merge % git status
On branch master
nothing to commit, working tree clean
chenyingying01@cyy merge % git merge alternate
Merge made by the 'recursive' strategy.
 file | 1 +
 1 file changed, 1 insertion(+)
chenyingying01@cyy merge % git log --graph --pretty=oneline --abbrev-commit
*   50fc402 (HEAD -> master) Merge branch 'alternate'
|\
| * 4fb8628 (alternate) Add alternate's line 4
* | 608c235 Another file
|/
* 61570a2 Initial 3 line file
chenyingying01@cyy merge %

2. 有冲突合并-手动解决

合并涉及相同文件的相同部分, merge时会产生冲突。需要手动的去修改冲突,然后在add, commmit。 代码分支合并时必须保证合入后不会影响原有的功能。

chenyingying01@cyy merge % git branch
  alternate
* master
chenyingying01@cyy merge % cat >> file
Line 5 stuff
Line 6 stuff
chenyingying01@cyy merge % git add file
chenyingying01@cyy merge % git commit -m "Add line 5 and line 6"
[master f0adb70] Add line 5 and line 6
 1 file changed, 2 insertions(+)
 
chenyingying01@cyy merge % git checkout alternate
Switched to branch 'alternate'
chenyingying01@cyy merge % cat >> file
Line 5 alternate stuff
Line 6 alternate stuff
chenyingying01@cyy merge % git diff
diff --git a/file b/file
index 3754bb3..260d21c 100644
--- a/file
+++ b/file
@@ -2,3 +2,5 @@ line 1 stuff
 line 2 stuff
 line 3 stuff
 line 4 alternate stuff
+Line 5 alternate stuff
+Line 6 alternate stuff
chenyingying01@cyy merge % git add file
chenyingying01@cyy merge % git commit -m "Add slternate line 5, 6"
line 1 stuff
[alternate 8de3d40] Add slternate line 5, 6
 1 file changed, 2 insertions(+)
 
chenyingying01@cyy merge % git checkout master
Switched to branch 'master'
chenyingying01@cyy merge % git merge alternate
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
chenyingying01@cyy merge % git diff
diff --cc file
index f1209a9,260d21c..0000000
--- a/file
+++ b/file
@@@ -2,5 -2,5 +2,10 @@@ line 1 stuf
  line 2 stuff
  line 3 stuff
  line 4 alternate stuff
++<<<<<<< HEAD
 +Line 5 stuff
 +Line 6 stuff
++=======
+ Line 5 alternate stuff
+ Line 6 alternate stuff
++>>>>>>> alternate
chenyingying01@cyy merge % vim file              # 手动解决冲突
chenyingying01@cyy merge % git add file
chenyingying01@cyy merge % git commit
[master b80e372] Create commit 7015896: Merge branch "alternate"
chenyingying01@cyy merge % git log --graph --pretty=oneline --abbrev-commit
*   b80e372 (HEAD -> master) Create commit 7015896: Merge branch "alternate"
|\
| * 8de3d40 (alternate) Add slternate line 5, 6
* | f0adb70 Add line 5 and line 6
* | 50fc402 Merge branch 'alternate'
|\|
| * 4fb8628 Add alternate's line 4
* | 608c235 Another file
|/
* 61570a2 Initial 3 line file
chenyingying01@cyy merge %

3. git diff in merge

在编程的过程中,提倡使用几个单独概念的,定义良好的提交进行merge,而不是等到最后一个大的庞大提交时再merge.
针对有冲突文件,git diff 命令的输出是 git diff HEAD 和git diff MERGE_HEAD 命令的结果的组合。

chenyingying01@cyy conflict % git diff
diff --cc hello
index e63164d,562080a..0000000
--- a/hello
+++ b/hello
@@@ -1,3 -1,3 +1,7 @@@
  hello
++<<<<<<< HEAD
 +worlds
++=======
+ world
++>>>>>>> alt
  Yay!
chenyingying01@cyy conflict % git diff HEAD
diff --git a/hello b/hello
index e63164d..1f2f61c 100644
--- a/hello
+++ b/hello
@@ -1,3 +1,7 @@
 hello
+<<<<<<< HEAD
 worlds
+=======
+world
+>>>>>>> alt
 Yay!
chenyingying01@cyy conflict % git diff MERGE_HEAD
diff --git a/hello b/hello
index 562080a..1f2f61c 100644
--- a/hello
+++ b/hello
@@ -1,3 +1,7 @@
 hello
+<<<<<<< HEAD
+worlds
+=======
 world
+>>>>>>> alt
 Yay!

4. 废弃合并

# 1.before merge commit 将工作目录和索引还原到git merge命令前状态
git reset --hard HEAD
# 2.after merge commit 将工作目录和索引还原到git merge命令前状态
# ORIG_HEAD git 把原始分支的HEAD放在ORIG_HEAD中
git reset --hard ORIG_HEAD  
# 3.把冲突解决方案搞砸了,想要返回到冲突的原始状态
git checkout -m

5. 合并策略

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值