merge相关的操作的命令
git checkout master
git merge alternate
git add file_1
git commit -m "Add slternate line 5, 6"
git reset --hard HEAD
git reset --hard ORIG_HEAD
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. 废弃合并
git reset --hard HEAD
git reset --hard ORIG_HEAD
git checkout -m
5. 合并策略
补
|