git 丢弃所有更改并从上游拉取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13781388/
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
git discard all changes and pull from upstream
提问by NinjaCowgirl
How do I fetch upstream repo and make it replace master? I only have one branch on my repo, which is master, and I completely messed it up, so I basically need to start over from the upstream. I think init will do the job, but is there an easier way?
如何获取上游 repo 并使其替换 master?我的repo上只有一个分支,它是master,我完全搞砸了,所以我基本上需要从上游重新开始。我认为 init 可以完成这项工作,但有没有更简单的方法?
回答by Eric Walker
There are (at least) two things you can do here–you can reclone the remote repo, or you can reset --hard
to the common ancestor and then do a pull, which will fast-forward to the latest commit on the remote master.
在这里(至少)有两件事你可以做——你可以重新克隆远程仓库,或者你可以reset --hard
到共同的祖先然后做一个拉取,这将快进到远程主服务器上的最新提交。
To be concrete, here's a simple extension of Nevik Rehnel's original answer:
具体来说,这是 Nevik Rehnel 原始答案的简单扩展:
git reset --hard origin/master
git pull origin master
NOTE: using git reset --hard
will discard any uncommitted changes, and it can be easy to confuse yourself with this command if you're new to git, so make sure you have a sense of what it is going to do before proceeding.
注意: usinggit reset --hard
将丢弃任何未提交的更改,如果您是 git 新手,很容易将自己与此命令混淆,因此请确保在继续之前了解它将要执行的操作。
回答by Nevik Rehnel
while on branch master:
git reset --hard origin/master
在分支 master 上:
git reset --hard origin/master
then do some clean up with git gc
(more about this in the man pages)
然后做一些清理git gc
(更多关于这个在手册页)
Update: You will also probably need to do a git fetch origin
(or git fetch origin master
if you onlywant that branch); it should not matter if you do this before or after the reset. (Thanks @eric-walker)
更新:你可能还需要做一个git fetch origin
(或者git fetch origin master
如果你只想要那个分支);在重置之前或之后执行此操作应该无关紧要。(感谢@eric-walker)
回答by Luca C.
You can do it in a single command:
您可以在单个命令中执行此操作:
git fetch --all && git reset --hard origin/master
Or in a pair of commands:
或者在一对命令中:
git fetch --all
git reset --hard origin/master
Note than you will lose ALL your local changes
请注意,您将丢失所有本地更改
回答by resultsway
git reset <hash> # you need to know the last good hash, so you can remove all your local commits
git fetch upstream
git checkout master
git merge upstream/master
git push origin master -f
voila, now your fork is back to same as upstream.
瞧,现在你的叉子又回到了上游。