如何从远程 Git 存储库中提取并覆盖本地存储库中的更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6284809/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:08:30  来源:igfitidea点击:

How can I pull from remote Git repository and override the changes in my local repository?

git

提问by Barka

I need to throw away all the changes in my local repository and pull all the code from the remote repository. What is the Git command to do this?

我需要扔掉本地存储库中的所有更改并从远程存储库中提取所有代码。执行此操作的 Git 命令是什么?

回答by CharlesB

Provided that the remote repository is origin, and that you're interested in master:

假设远程存储库是 origin,并且您对 master 感兴趣:

git fetch origin
git reset --hard origin/master

This tells it to fetch the commits from the remote repository, and position your working copy to the tip of its master branch.

这告诉它从远程存储库获取提交,并将您的工作副本定位到其主分支的顶端。

All your local commits not common to the remote will be gone.

您对远程不常见的所有本地提交都将消失。

回答by sebastian-c

As an addendum, if you want to reapply your changes on top of the remote, you can also try:

作为附录,如果您想在遥控器上重新应用您的更改,您还可以尝试:

git pull --rebase origin master

If you then want to undo some of your changes (but perhaps not all of them) you can use:

如果您想撤消某些更改(但可能不是全部),您可以使用:

git reset SHA_HASH

Then do some adjustment and recommit.

然后做一些调整并重新提交。