简体   繁体   English

用远程跟踪分支覆盖本地分支(分支已分支)

[英]Overwrite local branch with remote tracking branch (branches have diverged)

I have create a new branch named my-4.3.y using the following command (note: my-4.3.y is set up to track remote branch 4.3.y from origin): 我使用以下命令创建了一个名为my-4.3.y的新分支(注意:将my-4.3.y设置为从源头跟踪远程分支4.3.y ):

git checkout -b my-4.3.y origin/4.3.y

I haven't worked on the my-4.3.y branch after checking it out. 签出后我还没有在my-4.3.y分支上工作。 Now, several days later, when I run: 现在,几天后,当我运行时:

git status

It tells me that my-4.3.y and origin/4.3.y have diverged . 它告诉我my-4.3.yorigin / 4.3.y已经发散了

I don't care where and why the branches have diverged, I don't want to merge the remote branch into my. 我不在乎分支在哪里以及为什么分支,我不想将远程分支合并到我的分支中。 I just want my branch to be equal to the remote branch again. 我只希望我的分支再次等于远程分支。

So, what I am doing is: (1) checkout some other branch (2) delete my-4.3.y and (3) check it out again: 因此,我正在做的是:(1)签出一些其他分支(2)删除my-4.3.y和(3)再次签出:

git checkout some_other_branch
git branch -D my-4.3.y
git checkout -b my-4.3.y origin/4.3.y

Is there an easier way to that? 有更简单的方法吗?

You can use git reset --hard to force your currently checked out branch to any arbitrary point you would like. 您可以使用git reset --hard强制将当前签出的分支强制到您想要的任意点。

Note: using --hard is not working directory safe, and will throw out any changes you have. 注意:使用--hard不能保证目录的安全,并且会丢弃您所做的任何更改。

For the specific case of updating to your tracking branch you can use @{u} to specify your upstream 对于更新到跟踪分支的特定情况,您可以使用@{u}指定上游

git reset --hard @{u}

Generally, you can pass in any branch reference or anything else Git resolves to a commit eg 通常,您可以传入任何分支引用或Git解析为提交的任何其他内容,例如

git reset --hard origin/4.3.y

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM