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

我朋友的本地 master分支显然是一场灾难(通过意外的 mergecommit,我猜)。但是,他的开发分支很好,但是包含了他还没有准备好推送到远程的更改。

用远程 master分支覆盖他的本地 master分支并获得一个新的副本(而不覆盖他的其他分支)的最佳方法是什么?

119778 次浏览

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).

Paŭlo Ebermann's answer is correct:

git checkout master
git reset --hard origin/master

And add that if you also wish to remove untracked files and ignored files:

git clean -xfn # dry run with -n

Source with further details: How to remove local (untracked) files from the current Git working tree?