git 如何在不覆盖本地文件的情况下从远程拉取文件?

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

How do I pull files from remote without overwriting local files?

gitgithub

提问by Joe Isaacson

I am trying to set up a new git repo to a pre-existing remote repo.

我正在尝试将新的 git 存储库设置为预先存在的远程存储库。

I want my local files to overwrite the remote repo, but git says that I first have to pull those remote files in and merge them.

我希望我的本地文件覆盖远程存储库,但是 git 说我首先必须将这些远程文件拉入并合并它们。

Is there any way to pull but make sure that the local files are not overwritten by the remote?

有没有办法拉取但确保本地文件不被远程覆盖?

回答by Bob Gilmore

Well, yes, and no...

嗯,是的,也不是……

I understand that you want your local copies to "override" what's in the remote, but, oh, man, if someone has modified the files in the remote repo in some differentway, and you just ignoretheir changes and try to "force" your own changes without even lookingat possible conflicts, well, I weep for you (and your coworkers) ;-)

我知道您希望您的本地副本“覆盖”远程中的内容,但是,哦,伙计,如果有人以某种不同的方式修改了远程存储库中的文件,而您只需忽略他们的更改并尝试“强制”你自己的改变甚至没有考虑可能的冲突,好吧,我为你(和你的同事)哭泣;-)

That said, though, it's reallyeasy to do the "right thing..."

尽管如此,做“正确的事……”真的很容易。

Step 1:

第1步:

git stash

in your local repo. That will save away your local updates into the stash, then revert your modified files back to their pre-edit state.

在您的本地仓库中。这会将您的本地更新保存到存储中,然后将修改后的文件恢复到编辑前的状态。

Step 2:

第2步:

git pull

to get any modified versions. Now, hopefully, that won't get any new versions of the files you're worried about. If it doesn't, then the next step will work smoothly. If it does, then you've got some work to do, and you'll be glad you did.

获取任何修改版本。现在,希望不会得到您担心的文件的任何新版本。如果没有,那么下一步将顺利进行。如果是这样,那么你有一些工作要做,你会很高兴你做到了。

Step 3:

第 3 步:

git stash pop

That will merge your modified versions that you stashed away in Step 1 with the versions you just pulled in Step 2. If everything goes smoothly, then you'll be all set!

这会将您在第 1 步中隐藏的修改版本与您在第 2 步中提取的版本合并。如果一切顺利,那么您就大功告成了!

If, on the other hand, there were realconflicts between what you pulled in Step 2 and your modifications (due to someone else editing in the interim), you'll find out and be told to resolve them. Do it.

另一方面,如果您在第 2 步中提取的内容与您的修改(由于其他人在此期间进行编辑)之间存在真正的冲突,您会发现并被告知要解决它们。做吧。

Things will work out muchbetter this way - it will probably keep your changes without any real work on your part, while alerting you to serious, serious issues.

这样事情会好很多- 它可能会保留您的更改而无需您进行任何实际工作,同时提醒您注意严重的问题。

回答by Ryan Bigg

You can stash your local changes first, then pull, then pop the stash.

您可以先存储本地更改,然后拉取,然后弹出存储。

git stash
git pull origin master
git stash pop

Anything that overrides changes from remote will have conflicts which you will have to manually resolve.

任何覆盖远程更改的内容都会产生冲突,您必须手动解决这些冲突。

回答by Penghe Geng

So you have committed your local changes to your local repository. Then in order to get remote changes to your local repository without making changes to your local files, you can use git fetch. Actually git pullis a two step operation: a non-destructive git fetchfollowed by a git merge. See What is the difference between 'git pull' and 'git fetch'?for more discussion.

因此,您已将本地更改提交到本地存储库。然后,为了在不更改本地文件的情况下对本地存储库进行远程更改,您可以使用git fetch. 实际上git pull是一个两步操作:一个非破坏性的,git fetch然后是一个git merge. 请参阅“git pull”和“git fetch”有什么区别?更多讨论。

Detailed example:

详细示例:

Suppose your repository is like this (you've made changes test2:

假设您的存储库是这样的(您进行了更改test2

* ed0bcb2 - (HEAD, master) test2
* 4942854 - (origin/master, origin/HEAD) first

And the originrepository is like this (someone else has committed test1):

并且origin存储库是这样的(其他人已提交test1):

* 5437ca5 - (HEAD, master) test1
* 4942854 - first

At this point of time, git will complain and ask you to pull first if you try to push your test2to remote repository. If you want to see what test1 is without modifying your local repository, run this:

此时,如果您尝试推test2送到远程存储库,git 会抱怨并要求您先拉取。如果您想在不修改本地存储库的情况下查看 test1 是什么,请运行以下命令:

$ git fetch

Your result local repository would be like this:

您的结果本地存储库将是这样的:

* ed0bcb2 - (HEAD, master) test2 
| * 5437ca5 - (origin/master, origin/HEAD) test1 
|/  
* 4942854 - first 

Now you have the remote changes in another branch, and you keep your local files intact.

现在您在另一个分支中有远程更改,并且您保持本地文件完好无损。

Then what's next? You can do a git merge, which will be the same effect as git pull(when combined with the previous git fetch), or, as I would prefer, do a git rebase origin/masterto apply your change on top of origin/master, which gives you a cleaner history.

那么接下来呢?您可以执行 a git merge,其效果与git pull(与前一个 结合时git fetch)效果相同,或者,如我所愿,执行 agit rebase origin/master以将您的更改应用到 之上origin/master,这将为您提供更清晰的历史记录。