2009-06-03 103 views
9

我們正在使用一個我已經克隆的中央git存儲庫,並且正在本地分支上工作。如何有效地重建和推送本地git分支?

當我想在中央存儲庫中可用我的變化,我不得不發出以下命令(起始於mybranch):

#Stash local changes not yet ready for checkin 
git stash 

#Make sure we have all changes from the central repository 
git checkout master 
git pull 

#Rebase local changes 
git checkout mybranch 
git rebase 

#Push changes 
git checkout master 
git merge mybranch 
git push 

#Back to my branch and continue work 
git checkout mybranch 
git stash apply 

我想知道是否可以使用更少git命令來完成相同的目標。 mastermybranch之間的幾個開關特別惱人,因爲我們的存儲庫相當龐大,所以需要一些時間。

回答

13

推動有沒有必要如果你不需要更新,你可以觸摸你的本地主分支,這似乎是導致很多不必要的分支切換。

這是一個更簡單的工作流程。

git fetch 

# ensure that everything is committed 
# perhaps git commit -a is required... 

git rebase origin/master 


# If you don't want to push the very latest commits you might 
# want to checkout a parent or ancestor of the current commit 
# to test that the proposed commit passes tests, etc. 
# e.g. git checkout HEAD~n 

# push to the remote master 
git push origin HEAD:master 

# if you checked out a parent, go back to the original branch 
git checkout mybranch 

如果你是超級自信父提交,你可以跳過結帳步驟,只是做到以下幾點,但我強烈建議反對。發佈未經測試的提交不是「最佳實踐」。

git push origin HEAD^:master 
1

您可以結合拉底墊和成一個:

混帳拉--rebase主

但總體而言,是的,從我的經驗涉及到所有這些命令。

爲了保持您的存儲庫清潔,它經常運行「git gc」,它將刪除未使用的對象。這應該減少分支交換時間。

0

我平時做。

git co master 
git pull 
git rebase master mywrk # fix conflicts if any 
git rebase mywrk master 
git push 

如果您喜歡這種方式,您可以定義別名以保存輸入。

6

沒有必要對master和mybranch分支進行拉動。既然你是這樣一個很好的公民,做快進更新它非常直截了當:

# Save local mods not ready for commit 
git stash 
# Do the pull & rebase local work assuming this is a remote tracking branch 
git pull --rebase 
git checkout master 
git merge mybranch 
git push 

當然,你也可以從你的mybranch分支

# Save local mods not ready for commit 
git stash 
# Do the pull & rebase local work assuming this is a remote tracking branch 
git pull --rebase 
git push origin mybranch:master 
+0

你是什麼意思「假設這是一個遠程跟蹤分支」? mybranch是一個本地分支,只存在於我自己的倉庫中。你的解決方案也能在這種情況下工作嗎? – siebert 2009-06-04 08:24:51

相關問題