Git: reset other branch to current without a checkout

匿名 (未验证) 提交于 2019-12-03 01:07:01

问题:

I'm writing some scripts for my Git workflow.

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.)

Is this possible?

回答1:

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

What you need is

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


回答2:

Set otherbranch to point at the same commit as currentbranch by running

git branch -f otherbranch currentbranch 

The -f (force) option tells git branch yes, I really mean to overwrite any existing otherbranch reference with the new one.

From the documentation:

-f
--force

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



回答3:

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

$ 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!