简体   繁体   English

Git:如何将我的本地分支恢复到远程分支状态?

[英]Git: How to revert my local branch to the remote branch state?

I messed my local branch of a project, and i want to revert my local copy to the remote state. 我弄乱了项目的本地分支,我想将本地副本还原到远程状态。 How i acomplish that simply goal? 我如何完成那个简单的目标?

If you are not afraid of losing any local history, you can switch to another branch then delete your local branch, then check the remote version out. 如果您不怕丢失任何本地历史记录,可以切换到另一个分支,然后删除本地分支,然后检查远程版本。 For example, if you wanted to revert a branch called "test_feature," you could do this: 例如,如果要还原名为“test_feature”的分支,则可以执行以下操作:

$ git checkout master
$ git branch -D test_feature # see note about -D below
$ git checkout test_feature  # should track origin/test_feature

NOTE: -D will force delete the branch, and will suppress warnings about unmerged changes. 注意:-D将强制删除分支,并将禁止有关未合并更改的警告。

This is useful if you have merged a branch you didn't intend to, as the HEAD pointer can change depending on the type of merge. 如果您合并了一个您不想要的分支,这很有用,因为HEAD指针可以根据合并类型进行更改。

EDIT : Another method for doing the same thing is to simply type: 编辑 :另一种做同样事情的方法是简单地输入:

git reset --hard origin/test_feature

This will reset the branch you are currently on to the state of the remote (origin in this case) branch test_feature . 这会将您当前所在的分支重置为远程状态(本例中为原点)分支test_feature

@hvgotcodes has a variation of this in his example (he's targeting the HEAD commit) @hvgotcodes在他的例子中有一个变体(他的目标是HEAD提交)

you can reset with 你可以重置

git reset --hard HEAD

you will lose all your changes if you do this. 如果你这样做,你将失去所有的改变。 This might be helpful . 这可能会有所帮助

If you want to revert to remote LAST version : 如果要还原为远程LAST版本

git fetch --all
git reset --hard origin/master

If you want to revert to a specific version : 如果要还原为特定版本

First, get the string that identify the commit in some date, doing: 首先,获取在某个日期标识提交的字符串,执行:

git rev-list -n 1 --before="2009-07-27 13:37" origin/master

it prints the commit identifier, take the string (for instance XXXX) and do: 它打印提交标识符,获取字符串(例如XXXX)并执行:

git checkout XXXX

There's simple one liners to do what you're asking, but usually when I feel like I've "messed up a branch" vs. what's on the remote, I use interactive rebasing, which gives me a bit more control. 有一个简单的单线可以做你想要的,但通常当我觉得我“搞乱了一个分支”而不是遥控器上的内容时,我使用交互式变基,这让我有了更多的控制权。 That way I can edit commits if need be, or remove lines if I just don't want the commit. 这样我可以根据需要编辑提交,或者如果我不想提交则删除行。 See here: http://book.git-scm.com/4_interactive_rebasing.html 见这里: http//book.git-scm.com/4_interactive_rebasing.html

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

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