Git(10)-merge
生活随笔
收集整理的這篇文章主要介紹了
Git(10)-merge
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Merge
- 1. 無沖突合并
- 2. 有沖突合并-手動解決
- 3. git diff in merge
- 4. 廢棄合并
- 5. 合并策略
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 branchalternate * 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 61 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 stuffline 2 stuffline 3 stuffline 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, 61 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 stufline 2 stuffline 3 stuffline 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 命令的結果的組合。
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 -m5. 合并策略
補
總結
以上是生活随笔為你收集整理的Git(10)-merge的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++:50---虚析构函数
- 下一篇: 关于uint32_t uint8_t