git 如何从远程存储库获取分支的新副本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5657451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a fresh copy of a branch from the remote repository?
提问by Brian
My friend's local master
branch is apparently a disaster (through accidental merge
s and commit
s, I guess). However, his dev branches are fine but contain changes he's not ready to push to remote.
我朋友的当地master
分支机构显然是一场灾难(我猜是意外的merge
s 和commit
s)。然而,他的开发分支很好,但包含他还没有准备好推送到远程的更改。
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)?
master
用远程master
分支覆盖他的本地分支并获得新副本(不覆盖他的其他分支)的最佳方法是什么?
回答by Pa?lo Ebermann
As Jefromi commented,
正如 Jefromi 评论的那样,
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.
做正确的事:将 master 设置为其原始状态。(如果您已经在master
分支上,则可以省略第一个命令。)它还使分支的 reflog 保持不变。
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
这会切换到另一个分支(在本例中为“dev” – 选择您可能拥有的任何其他分支),删除本地master
分支,然后从中重新创建它remotes/origin/master
(根据您的设置和 Git 版本,这可能不起作用)。最后一个命令通常相当于
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).
与上面的新答案相比,它的缺点是 reflog 被破坏和重新创建(即,如果需要,您不能轻易撤消此操作),并且不太清楚这里发生了什么。此外,您需要存在另一个分支,您可以在删除和重新创建期间切换到该分支(但在原始问题中就是这种情况)。
回答by Travis D
Pa?lo Ebermann's answer is correct:
Pa?lo Ebermann 的回答是正确的:
git checkout master
git reset --hard origin/master
And add that if you also wish to remove untrackedfiles and ignoredfiles:
如果您还希望删除未跟踪的文件和忽略的文件,请添加:
git clean -xfn # dry run with -n
Source with further details: How to remove local (untracked) files from the current Git working tree?
带有更多详细信息的来源:如何从当前 Git 工作树中删除本地(未跟踪)文件?