2009-10-19 47 views
79

我正在爲我的Git工作流寫一些腳本。Git:重置其他分支到當前沒有結帳

我需要將其他(現有)分支重置爲當前分支,而無需結帳。

前:

CurrentBranch: commit A 
OtherBranch: commit B 

後:

等效的

$ git checkout otherbranch 
$ git reset --soft currentbranch 
$ git checkout currentbranch 

CurrentBranch: commit A 
OtherBranch: commit A 

(注--soft:我不想影響工作樹。)

這可能嗎?

+0

有誰知道這是不是也有可能在例如:It? – zedoo 2018-01-24 17:26:51

回答

59

您描述的工作流程並不相同:當您執行reset --hard時,將失去工作樹中的所有更改(您可能希望將其更改爲reset --soft)。

你需要的是

git update-ref refs/heads/OtherBranch refs/heads/CurrentBranch 
+0

對,謝謝。我不想影響工作樹。 – 2009-10-19 21:40:48

+2

男人,我希望這個工作沒有多餘的'refs/heads /'... – ELLIOTTCABLE 2013-04-15 23:09:53

+32

做這個更好的方法是'git push。當前:other'。這可以在沒有'refs/heads'(/ cc @elliottcable)的情況下工作,並且它也會阻止你更新簽出的分支。請注意,如果更新不是快進,您可能需要傳遞-f(或使用'+ current:other')。 – 2013-06-06 07:41:42

15

您可以隨時

$ git push . CurrentBranch:OtherBranch -f 

而且使用此命令同步您的分支沒有-f它代替這組命令

$ git checkout OtherBranch 
$ git merge CurrentBranch 
$ git checkout CurrentBranch 

當您不需要在CurrentBranch中提交所有文件並且不能切換到另一個b時,它會很有用牧場。

+0

可能會導致「remote:error:denying non-fast-forward」 – Basilevs 2014-06-04 15:13:18

+0

除非絕對不可避免,否則我不會包含'-f'選項。在我的情況下沒有它,它運行良好。 – Melebius 2015-03-31 07:43:30

140

設置otherbranch在同一個點作爲提交由currentbranch運行

git branch -f otherbranch currentbranch 

-f(力)選項告訴git branch是的,我真的要覆蓋所有現有otherbranch參考用新

documentation

-f
--force

Reset to if exists already. Without -f git branch refuses to change an existing branch.

+2

這是最簡單的答案。謝謝! – 2014-05-26 17:40:25

+0

我得到'致命的:無法強制更新當前分支。「當試圖這樣做。 – 2014-06-25 15:42:20

+3

@FuadSaud這是因爲你已經有'otherbranch'檢出。這個SO問題具體是關於將另一個分支重置爲不同的提交(即不重置檢出分支)。你想要做的就是用'git reset targetbranch'來重置當前分支,以強制當前分支指向'targetbranch'。加上'--hard'來強制工作樹到那個內容。或''soft'離開索引,只改變分支本身。 – 2014-06-25 19:28:06