2009-10-19 91 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