git 用远程版本替换本地版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5288172/
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 replace local version with remote version
提问by ryudice
How can I tell git to ignore my local file and take the one from my remote branch without trying to merge and causing conflicts?
如何告诉 git 忽略我的本地文件并从我的远程分支中取出一个而不尝试合并并导致冲突?
回答by Olivier Verdier
This is the safest solution:
这是最安全的解决方案:
git stash
Now you can do whatever you want without fear of conflicts.
现在你可以做任何你想做的事而不必担心冲突。
For instance:
例如:
git checkout origin/master
If you want to include the remote changes in the master branch you can do:
如果要在 master 分支中包含远程更改,可以执行以下操作:
git reset --hard origin/master
This will make you branch "master" to point to "origin/master".
这将使您分支“master”以指向“origin/master”。
回答by sehe
I understand the question as this: you want to completely replace the contents of one file (or a selection) from upstream. You don't want to affect the index directly (so you would go through add + commit as usual).
我理解这个问题:您想从上游完全替换一个文件(或选择)的内容。你不想直接影响索引(所以你会像往常一样通过 add + commit )。
Simply do
简单地做
git checkout remote/branch -- a/file b/another/file
If you want to do this for extensive subtrees and instead wish to affect the index directly use
如果您想对广泛的子树执行此操作,而希望直接影响索引,请使用
git read-tree remote/branch:subdir/
You can then (optionally) update your working copy by doing
然后,您可以(可选)通过执行以下操作来更新您的工作副本
git checkout-index -u --force
回答by Almir Campos
My understanding is that, for example, you wrongly saved a file you had updated for testing purposes only. Then, when you run "git status" the file appears as "Modified" and you say some bad words. You just want the old version back and continue to work normally.
我的理解是,例如,您错误地保存了一个仅用于测试目的而更新的文件。然后,当您运行“git status”时,该文件显示为“已修改”并且您说了一些不好的话。您只想要旧版本并继续正常工作。
In that scenario you can just run the following command:
在这种情况下,您只需运行以下命令:
git checkout -- path/filename
回答by Birol Efe
I would checkout the remote file from the "master" (the remote/origin repository) like this:
我会像这样从“主”(远程/源存储库)检出远程文件:
git checkout master <FileWithPath>
Example: git checkout master components/indexTest.html
示例:git checkout master components/indexTest.html
回答by csi
Use the -s
or --strategy
option combined with the -X
option. In your specific question, you want to keep all of the remote files and replace the local files of the same name.
将-s
or--strategy
选项与选项结合使用-X
。在您的具体问题中,您希望保留所有远程文件并替换同名的本地文件。
Replace conflicts with the remote version
替换与远程版本的冲突
git merge -s recursive -Xtheirs upstream/master
will use the remote repo version of all conflicting files.
将使用所有冲突文件的远程仓库版本。
Replace conflicts with the local version
替换与本地版本冲突
git merge -s recursive -Xours upstream/master
will use the local repo version of all conflicting files.
将使用所有冲突文件的本地 repo 版本。