需要将git分支重置为原始版本 [英] Need to reset git branch to origin version

查看:94
本文介绍了需要将git分支重置为原始版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然在一家分店工作了一段时间,所以我分手了,给它一个合适的名字。现在我想覆盖我原本不应该从源(github)版本的分支。是否有捷径可寻?我尝试删除分支,然后重新设置跟踪分支,但它只是给了我以后再次处理的版本。 解决方案

如果您尚未推送至原点,则可以将分支重置为上游分支:

  git checkout mybranch 
git reset --hard origin / mybranch

(请确保您在单独的分支中引用您的最新提交,就像您在问题中提到的一样) p>

请注意,复位后, mybranch @ {1} 在重置之前引用旧的提交。 p>

但是,如果您已经推送过,请参阅创建git分支,并将原始格式恢复为上游状态。其他选项。






正如 Brad Herman reset --hard 删除任何新文件或将修改后的文件重置为HEAD 即可。



实际上,为了确保您从清理板块开始,在 git clean -f -d 之后开始重置将确保一个工作树正好与您刚刚重置的分支一致。






这个博文建议这些别名(仅适用于 master 分支,但您可以调整/扩展这些分支):


  [别名] 
resetorigin =!git fetch origin&& git reset --hard origin / master&& git clean -f -d
resetupstream =!git fetch upstream&& git reset --hard upstream / master&& git clean -f -d

然后你可以输入:

  git resetupstream 

  git resetorigin 



I was accidentally working on a branch I shouldn't have been for a while, so I branched off of it giving it the appropriate name. Now I want to overwrite the branch I shouldn't have been on to the version from origin (github). Is there an easy way to do this? I tried deleting the branch and then resetting up the tracking branch, but it just gives me the version I was working on again.

解决方案

If you don't have pushed yet to origin, you can reset your branch to the upstream branch with:

git checkout mybranch
git reset --hard origin/mybranch

(Make sure that you reference your latest commit in a separate branch, like you mention in your question)

Note that, just after the reset, mybranch@{1} refers to the old commit, before reset.

But if you had already pushed, see "Create git branch, and revert original to upstream state" for other options.


As commented by Brad Herman, a reset --hard would remove any new file or reset modified file to HEAD.

Actually, to be sure you start from a "clean slate", a git clean -f -d after the reset would ensure a working tree exactly identical to the branch you just reset to.


This blog post suggests those aliases (for master branch only, but you can adapt/extend those):

[alias]
   resetorigin = !git fetch origin && git reset --hard origin/master && git clean -f -d
   resetupstream = !git fetch upstream && git reset --hard upstream/master && git clean -f -d

Then you can type:

git resetupstream

or

git resetorigin

这篇关于需要将git分支重置为原始版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆