2013-04-11 76 views
26

假設我們有一個從master創建的hotfixes分支。我們添加了提交到hotfixes,但這些提交沒有用處,所以現在我們想要再次從master的新副本開始。如何使用git將分支重置爲其他分支?

澄清更好,這是參考工作流程:http://nvie.com/posts/a-successful-git-branching-model/

,讓我們也說我們推hotfixesorigin遠程因爲我們有一個可怕的設立,這就是測試的時候,唯一的方式,所以我們需要也在遠程服務器上重置分支。

如何將hotfixes重置爲master的副本?

回答

29

你的意思是你想推動你的本地master到遠程hotfixes分支?像這樣:

git push origin +master:hotfixes 

但是,這要求您允許您重寫遠程端的歷史記錄。

+0

這是正確的語法嗎?當我這樣做時,我得到了一個新的遠程分支,名稱中帶有加號(+)。我必須移動像git push origin + master:hotfixes這樣的加號。這是根據git規範:http://git-scm.com/docs/git-push – jwynveen 2014-07-01 21:17:54

+0

@jwynveen你是對的,修復。 – 2014-07-03 09:15:05

+6

fyi,加號是在推送過程中執行'--force'的簡寫:http://stackoverflow.com/questions/1475665/why-git-push-helloworld-mastermaster-instead-of-just-git- push-helloworld – 2016-03-01 15:23:28

6

如果我正確地理解了您的問題,您正在尋找的是將origin/hotfixes的分支指針移至指向origin/master的當前修訂版的方法。

如果是這樣的話,這些set命令應該工作(假設你已經在你的本地混帳回購協議的任何時間過去檢查了hotfixes):

# git branch -f does not allow modifying the currently checked out 
# branch, so checkout any other branch than hotfixes 
git checkout <SOME_OTHER_BRANCH_THAN_HOTFIXES> 

# Move the branch pointer of hotfixes to the commit currently 
# pointed by origin/master 
git branch -f hotfixes origin/master 

# Force push the history rewrite in the hotfixes branch 
# into origin 
git push -f origin hotfixes 
+0

'分支hotifxes設置爲從原點追蹤遠程分支主。「 - 不知道這是所有想要的 – 2016-10-11 12:44:12

36

這是我如何與做的基本的Git命令:

git checkout hotfixes 
git reset --hard master 
git push --force origin hotfixes 

當然它來通知大家工作hotfixes是很重要的。他們很可能不得不刪除他們的本地副本,並從新的副本開始。一種替代方法是創建一個新的分支:

git checkout master 
git branch -tb hotfixes-2 # this creates branch `hotfixes-2` from a copy of `master` 
git push origin HEAD # this creates `hotfixes-2` on the remote server 
+0

我更喜歡這種方式。不太容易出錯。 – 2016-11-15 07:37:15

+0

感謝您的偏好,但我很想知道爲什麼你發現它更不容易出錯 – danza 2016-11-22 16:14:50

+1

我應該說這是個人偏好,並沒有試圖說明一個事實。 對我而言,它需要更多的動作,它們更好地描述你正在做的事情,而不是僅僅推到不同的遙控器,這反過來又使我更加意識到自己在做什麼。 此外,額外的'-f'標誌和'復位--hard'具有警告與GUI工具,如sourcetree例如.. – 2016-11-23 09:13:32

1

這裏的答案是可靠的。將我的暫存分支重置爲主時,我需要進行確切的更改。在這種情況下,我希望將原點重置爲匹配主,並重置我的本地以匹配它。所以這裏有一個git別名,它允許你傳入分支名稱並一次執行兩個命令。 (這是一個有點危險)

reorient = "!f() { git push origin +master:$1 && git reset --hard origin/$1 ; }; f" 

然後使用它像:以上

git reorient hotfixes 

的答案是完全正確的。但這隻會減少按鍵和更快的週轉!希望能幫助到你。

+0

我不會在一個別名中很容易地包裝'git reset --hard' ...我會害怕忘記和丟失重要變化 – danza 2018-01-25 11:39:55

+1

是的。就像我在解釋「這有點危險」中所說的那樣。然而,在某些情況下,你可以發現自己在常規上做這件事,在這種情況下,你可能需要更快一些。 – 2018-01-30 00:47:14