如何从远程存储库获取分支的新副本

如何从远程存储库获取分支的新副本

本文介绍了如何从远程存储库获取分支的新副本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我朋友的本地 master 分支显然是一个灾难(通过意外的 merge s和提交 s,我猜)。然而,他的开发分支是好的,但包含他还没有准备推送到远程的更改。



重写他的本地主的最好方法是分支与远程 master 分支并得到一个新的副本(不覆盖他的其他分支)?

  git checkout master 
git reset - - 原产地/主产地

做正确的事情:将主人设置为其原始状态。 (如果你已经在 master 分支上,你可以省略第一条命令。)它也会使分支的引用日志保持原样。






旧的回答:

  git checkout dev 
git branch -D master
git checkout master

这个开关到另一个分支(本例中的dev - 选择任何其他分支),删除本地 master 分支,然后从 remotes / origin / master (根据您的设置和Git版本,这可能不起作用)。最后一条命令通常相当于

  git checkout -b master remotes / origin / master 

与上面的新答案相比,这有一个缺点,即reflog被销毁并重新创建(也就是说,如果需要, ,并且不太清楚这里发生了什么。此外,您还需要有另一个分支,您可以在删除和重新创建过程中切换到其他分支(但在原始问题中就是如此)。


My friend's local master branch is apparently a disaster (through accidental merges and commits, I guess). However, his dev branches are fine but contain changes he's not ready to push to remote.

What's the best way of overriding his local master branch with the remote master branch and get a fresh copy (without overriding his other branches)?

解决方案

As Jefromi commented,

git checkout master
git reset --hard origin/master

does the right thing: setting the master to its origin state. (If you are already on the master branch, you can omit the first command.) It also leaves the branch's reflog intact.


Old inferior answer:

git checkout dev
git branch -D master
git checkout master

This switches to another branch ("dev" in this case – choose any other branch you might have), deletes the local master branch, and then recreates it from remotes/origin/master (which might not work depending on your settings and Git version). The last command is often equivalent to

git checkout -b master remotes/origin/master

Compared to the new answer above this has the disadvantage that the reflog is destroyed and recreated (i.e. you can't as easy undo this if needed), and it is less clear what happens here. Also, you need to have another branch existing to which you can switch during deletion and recreation (but that was the case in the original question).

这篇关于如何从远程存储库获取分支的新副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 05:02