如何将本地 git repo 与 origin/master 同步以消除所有更改

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

How to sync local git repo with origin/master eliminating all changes

gitsynclocalpullmaster

提问by Ben Weaver

I want to sync my local repository so as to make the local an exact copy of the master. Since checking it out I have added several files to my local, not present in master, that I do not wish to commit. Nevertheless, in this procedure, I want to erase all differences of my local from master: when I am done, the additional files in local will have been deleted.

我想同步我的本地存储库,以便使本地成为 master 的精确副本。自从检查出来后,我已将几个文件添加到我的本地,而不是在 master 中,我不想提交。尽管如此,在这个过程中,我想清除我本地与 master 的所有差异:完成后,本地中的其他文件将被删除。

An earlier question offers a strategy to make local like master: Reset local repository branch to be just like remote repository HEAD

较早的一个问题提供了一种使本地像 master 一样的策略:将 本地存储库分支重置为就像远程存储库 HEAD

But this does not work for me. The recommended commands, git fetch origin git reset --hard origin/master

但这对我不起作用。推荐的命令, git fetch origin git reset --hard origin/master

do not erase the additional files from my local, even though git status indicates local and master are identical. And also, in one case the master's version of a file did not replace my local version.

不要从我的本地删除额外的文件,即使 git status 表明本地和主是相同的。而且,在一种情况下,文件的主版本没有替换我的本地版本。

Any ideas on how to do this?

关于如何做到这一点的任何想法?

回答by functionpointer

Actually, the commands you tried will reset all tracked files to the state of origin/master. However, git doesn't touch untracked files (usually). In fact, the whole purpose of the "untracked file"-feature is, to be able to have file completely independent of git inside the repository.

实际上,您尝试的命令会将所有跟踪的文件重置为origin/master. 但是,git 不会触及未跟踪的文件(通常)。实际上,“未跟踪文件”功能的全部目的是使文件完全独立于存储库中的 git。

However, you can still make git delete untracked files if you want to:

但是,如果您想,您仍然可以使用 git 删除未跟踪的文件:

To delete all untracked files from your repository, type:

要从存储库中删除所有未跟踪的文件,请键入:

git clean -f

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

(来源:如何从当前 Git 工作树中删除本地(未跟踪)文件?

Be aware, as the files you delete are untracked, they will be lost forever.

请注意,由于您删除的文件未被跟踪,它们将永远丢失

回答by Jan Warcho?

I think that you're mixing two things up:

我认为你混淆了两件事:

  • updating your repository to be in sync with remote repository
  • resetting your working directory to be identical to a particular commit.
  • 更新您的存储库以与远程存储库同步
  • 将您的工作目录重置为与特定提交相同。

To update your repository (i.e. the database in which git stores all information about your project and its history), use git fetch remote_repository_name(remote_repository_nameis usually origin). After running this command, all commits and objects that are present in the remote repository originwill be present in your repository.

要更新您的存储库(即 git 存储有关您的项目及其历史记录的所有信息的数据库),请使用git fetch remote_repository_name(remote_repository_name通常是origin)。运行此命令后,远程存储库中存在的所有提交和对象都origin将出现在您的存储库中。

Now, the working directory- i.e. the files that you see in the folder containing the project - is something different from the repository database itself. Notice that you cannot make the working directory to be the same as the the remote repository, because these are two different kinds of things (a directory with files vs. a git database). You can reset your working directory to reflect a particular commit (for example tip of masterbranch from origin) - here @oznerol256's answergives you what you need.

现在,工作目录- 即您在包含项目的文件夹中看到的文件 - 与存储库数据库本身不同。请注意,您不能使工作目录与远程存储库相同,因为这是两种不同的事物(包含文件的目录与 git 数据库)。您可以重置您的工作目录以反映特定提交(例如master来自的分支提示origin) - 此处@oznerol256 的答案为您提供了您需要的内容。

回答by wei2912

As the files are not yet committed, you can use:

由于文件尚未提交,您可以使用:

rm -rf ./*
git reset --hard HEAD

This will wipe out all files, then revert the directory back to its original state.

这将清除所有文件,然后将目录恢复到其原始状态。