在没有结账的情况下将其他分支重置为当前

时间:2009-10-19 20:56:16

标签: git

我正在为我的Git工作流编写一些脚本。

我需要将其他(现有)分支重置为当前分支,而无需结帐。

在:

 CurrentBranch: commit A
 OtherBranch: commit B

后:

 CurrentBranch: commit A
 OtherBranch: commit A

相当于

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

(注意--soft:我不想影响工作树。)

这可能吗?

4 个答案:

答案 0 :(得分:183)

通过运行

otherbranch设置为与currentbranch相同的提交
git branch -f otherbranch currentbranch

-f(强制)选项告诉git branch 是的,我的意思是用新的覆盖任何现有的otherbranch引用。

来自documentation

  

-f
  --force

     

如果已存在则重置为。没有-f git branch拒绝更改现有分支。

答案 1 :(得分:75)

您描述的工作流程并不等同:当您执行reset --hard时,您将丢失工作树中的所有更改(您可能希望将其设为reset --soft)。

您需要的是

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

答案 2 :(得分:20)

您可以随时使用此命令与您的分支同步

$ git push . CurrentBranch:OtherBranch -f

同样没有-f它会替换这组命令

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

当您不需要在CurrentBranch中提交所有文件时,它会很有用,因此您无法切换到另一个分支。

答案 3 :(得分:0)

其他答案很好,但有点吓人。我只是提供了另一种选择,可以通过每天使用的各种命令来简单地实现所需的功能。

简而言之,这些选项都不会与您当前所在的分支或当前头层相混淆。

git branch -C other_branch(从当前HEAD强制创建other_branch)

要将other_branch重置为您不在的分支...

git branch -C old_branch other_branch(从old_branch强制创建other_branch)

要将remote的other_branch重置为some_branch,这有点不同...

git pull -f origin old_branch:other_branch(将其原点/ old_branch强制拉入(忽略已更新的内容)

相关问题