2017-08-09 160 views
0

我創建了一個用於開發的分支,它有一些無用的提交。現在我想將提交合併到mainline分支。我想在原始/主分支中提交的唯一提交是最新的提交和另一個將第一次提交壓縮爲第二次提交的提交。如何合併到只有兩個提交和一個提交被壓扁的另一個分支?

編輯: 對不起,如果這不明確,讓我試試更清楚。假設我在開發分支中有5個提交。我想將dev分支中完成的所有更改合併到origin/master分支。問題是我想壓縮提交#1-4到一個提交併合併提交和提交#5到原始/主分支。

回答

2

根據該分支上提交的數目,你想擺脫,尤其是那些「無用」的人,你可以做一個互動變基擺脫Ø ˚F那些你不想要的,或者你可以簡單地挑選您真的想保持兩個:

# check out master and update it first 
git checkout master 
git merge origin/master 

# cherry-pick the latest commit from the development branch 
git cherry-pick dev-branch 

# cherry-pick the other commit (use the hash from the log) 
git cherry-pick theothercommitshash 

之後,你有這兩個提交乾淨就master頂部,您可以使用軟復位壓制它們的或互動式底座。

問題是我想壓縮提交#1-4到一個提交併合併提交和提交#5到原始/主分支。

這實際上是交互式重新裝訂的完美用例。

從開發分支開始並運行git rebase -i dev~5以啓動編輯器,併爲最後5次提交提供交互式重新佈局計劃。它應該是這樣的:

pick <hash1> Commit 1 
pick <hash2> Commit 2 
pick <hash3> Commit 3 
pick <hash4> Commit 4 
pick <hash5> Commit 5 

更改內3 picksquash,所以它看起來是這樣的:

pick <hash1> Commit 1 
squash <hash2> Commit 2 
squash <hash3> Commit 3 
squash <hash4> Commit 4 
pick <hash5> Commit 5 

squash基本意思是「採取承諾,但它融合成以前的」。因此,在這種情況下,提交1被按原樣進行挑選,然後提交2,3和4被一個接一個地壓縮。所以你最終得到一個包含所有4個提交更改的提交。

保存該計劃並退出編輯器以啓動交互式重新綁定,並在Git提示您執行時調整已壓扁的提交的提交消息。最終,你最終只會在分支上進行兩次提交:壓扁的提交,並在最後提交5個提交。

最後,你可以將分支合併到主,然後就完成了。

+0

在我查看master並更新它之後,我能否壓縮前4個提交(在我添加的示例中提到過)?如果是這樣,你能告訴我怎麼做嗎?對不起,我對git相當陌生。 – lezzago

+0

@lezzago更新了有關更新的答案。 – poke

0

如果你不希望合併在一個臨時黨支部到主的所有更改,你可以使用git cherry-pick

git checkout master 
# Create a new commit in branch master which is equal to the latest 
# commit in my_dev_branch 
git cherry-pick my_dev_branch 

如果你想從my_dev_branch櫻桃採摘兩次提交,然後捏碎他們到單個提交在master,然後--no-commit就是你需要:

git checkout master 
git cherry-pick --no-commit <some-commit> 
git cherry-pick --no-commit <another-commit> 
# Now that all changes are in index, create a new commit 
git commit 
相關問題