2012-02-11 55 views
3

假如我是master分支並創建一個新的分支:Git:什麼是將工作分支合併到主的最快方法?

git checkout -b feature_branch 

我開始對feature_branch工作,並在某些時候,我想用變基到我的變化合並master。所以,我這樣做:

# Get the latest code on master 
git checkout master 
git pull 

# Rebase on master and push the most updated 'feature_branch' 
git checkout feature_branch 
git rebase master 
git push 

# Merge 'feature_branch' to 'master' and push the updated 'master' 
git checkout master 
git merge feature_branch 
git push 

# Back to work on 'feature_branch' 
git checkout feature_branch 

有沒有辦法減少步驟數量並達到相同?

在過程結束時,我希望master,origin/master,feature_branchorigin/feature_branch指向相同的提交。

回答

2

您可以刪除一些命令。這確實是相同的:

# you have to have the branch checked out to pull, since pulling means merging 
# into master, and you need a work tree to merge in 
git checkout master 
git pull 

# no need to check out first; rebase does the right thing with two arguments 
git rebase master feature_branch 

git checkout master 
git merge feature_branch 

# git push by default pushes all branches that exist here and on the remote 
git push 

git checkout feature_branch 

嚴格地說,合併成主保證是快進(一個簡單的合併),所以它實際上並不需要工作的樹,但真正有沒有內置在途中跳過結帳。有一些解決方法,例如推進到同一個存儲庫:git push . feature_branch:master(安全,但很奇怪)或直接更新ref:git update-ref master feature_branch(不安全 - 不檢查它是否是快進),但通常你可能只是快速切換分支。

另請注意,如果您不想重新綁定功能分支,那麼您可以跳過該功能,而不是重寫feature_branch,並最終在主設備中進行合併提交,而不是重新綁定feature_branch和快進合併。

+0

好的答案!風格的東西,但也許「git pull。feature_branch」,而不是「git merge feature_branch」。加入git push也可能很酷--tags – 2012-02-11 03:34:13

+1

@AdrianCornish:'git pull。 feature_branch'會和'git merge feature_branch'完全一樣,只會更容易混淆。我在這裏沒有看到任何東西來表明標籤需要處理。 – Cascabel 2012-02-11 03:37:57

+0

同意 - 標籤可能是一個額外的功能:-)。 同意這兩個命令是相同的,我之所以建議這樣做,是因爲拉請求工作流被用於許多OpenSource的東西 - 因此實際上您正在響應您寫的拉請求。純粹是一種精神狀態 - 你將Repo Y中作者X的改變拉到你的分支Z. – 2012-02-11 03:53:43

相關問題