代码之家  ›  专栏  ›  技术社区  ›  Alexander Gladysh

在不签出的情况下将其他分支重置为当前分支

git
  •  87
  • Alexander Gladysh  · 技术社区  · 15 年前

    我正在为我的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 回复  |  直到 6 年前
        1
  •  71
  •   P Shved    15 年前

    您描述的工作流不是等效的:当您执行 reset --hard 您将丢失工作树中的所有更改(您可能希望 reset --soft )

    你需要的是

    git update-ref refs/heads/OtherBranch refs/heads/CurrentBranch
    
        2
  •  176
  •   Colin D Bennett    9 年前

    集合 otherbranch 指向与 currentbranch 通过运行

    git branch -f otherbranch currentbranch
    

    这个 -f (强制)选项告诉 git branch 是的,我真的想覆盖现有的 其他分支 与新的参考 .

    documentation :

    -F
    --力

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

        3
  •  18
  •   Denis Kuznetsov    12 年前

    您可以随时与这个命令同步您的分支

    $ git push . CurrentBranch:OtherBranch -f
    

    另外,如果没有-f,它将替换这组命令

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

    当您不需要提交当前分支中的所有文件,因而无法切换到其他分支时,它会很有用。

        4
  •  -5
  •   Kyle s    7 年前

    我来这里是为了寻找一个更糟糕的解决方案来设置分支状态。

    这是一种进行硬重置的方法。请确定你知道你在做什么。这可能很危险。

    git checkout <branchToReset>
    git reset --hard <branchThatHasCorrectState>
    git push --force
    

    小枝 现在完全一样了 具有正确状态的分支 小枝 已在远程回购上更新为新状态。所有在里面的东西 小枝 不存在于 具有正确状态的分支 不再存在。你在破坏历史,因此才是危险的部分。