git 无需结帐即可将其他分支重置为当前分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1591107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:54:08  来源:igfitidea点击:

Reset other branch to current without a checkout

git

提问by Alexander Gladysh

I'm writing some scripts for my Git workflow.

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

I need to reset other (existing) branch to the current one, without checkout.

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

Before:

前:

 CurrentBranch: commit A
 OtherBranch: commit B

After:

后:

 CurrentBranch: commit A
 OtherBranch: commit A

Equivalent of

相当于

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

(Note --soft: I do not want to affect working tree.)

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

Is this possible?

这可能吗?

采纳答案by P Shved

The workflows you describe are not equivalent: when you perform reset --hardyou lose all the changes in the working tree (you might want to make it reset --soft).

您描述的工作流程并不等效:当您执行时,reset --hard您会丢失工作树中的所有更改(您可能想要更改reset --soft)。

What you need is

你需要的是

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

回答by Colin D Bennett

Set otherbranchto point at the same commit as currentbranchby running

设置otherbranch为指向与currentbranch运行相同的提交

git branch -f otherbranch currentbranch

The -f(force) option tells git branchyes, I really mean to overwrite any existing otherbranchreference with the new one.

-f(强制)选项告诉git branch是的,我真的要覆盖任何现有的otherbranch与新的一个参考

From the documentation:

文档

-f
--force

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

-f
--force

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

回答by Denis Kuznetsov

You can sync with this command your branches at any time

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

$ git push . CurrentBranch:OtherBranch -f

Also without -f it replace this set of commands

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

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

It can be useful when you don't need commit all your files in CurrentBranch and so you can't switch to another branches.

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