git 拉取并替换现有文件

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

pull and replace the file that is existing

git

提问by coolesting

I want to pull a branch(master branch) to current repository, and replace the file that is existing, not just merge the file, the command git pullis not appropriate to my needs, how to do that?

我想拉一个分支(主分支)到当前存储库,并替换现有的文件,而不仅仅是合并文件,命令git pull不适合我的需要,怎么做?

采纳答案by CharlesB

Why do you think it isn't appropriate?

为什么觉得不合适?

git pullwill update your branch to the same state of the remote repository, so if the file you have is at a newer version on the remote, it will be replaced.

git pull会将您的分支更新为与远程存储库相同的状态,因此如果您拥有的文件在远程版本上处于较新版本,它将被替换。

EDIT

编辑

If after the pull, merges are done with your local changes, you can reset to the state of remote repository with the following:

如果在拉取后,合并与您的本地更改完成,您可以使用以下命令重置为远程存储库的状态:

git reset origin/head -- <file-path>

回答by Yogesh

If you want to replace single file you can use this.

如果你想替换单个文件,你可以使用它。

git checkout -- filename

Click herefor more details

点击这里了解更多详情

回答by Useless

So you've made a local commit affecting this file, and wish to discard your commit and just take the remote state without merging, is that right?

所以你已经做了一个影响这个文件的本地提交,并希望放弃你的提交并只采取远程状态而不合并,对吗?

Assuming no-one else has seen your local commit (ie, you didn't pushand nobody pulled from your repo), you can just wind your local HEAD back to before the commit, and then do the pull.

假设没有其他人看到您的本地提交(即,您没有看到您的存储库中的push任何人pull),您可以将本地 HEAD 返回到提交之前,然后进行拉取。

NB. if you're not sure, run git fetchfirst, and then git statuswill tell you whether pull would be a fast-forward or a merge operation. Fast-forward means your local change is out of the picture.

注意。如果您不确定,请先运行git fetch,然后git status会告诉您拉取是快进还是合并操作。快进意味着您的本地更改不在画面中。

回答by ChrisH

It could very well be true that the question-asker actually knows exactly what he's talking about and just needs the mechanical how-tos answered, not a philosophy discussion.

问题很可能是真的,提问者实际上确切地知道他在说什么,只需要回答机械操作方法,而不是哲学讨论。

git checkout -- (double dash) then the file name, space between double dash and filename

git checkout --(双破折号)然后是文件名,双破折号和文件名之间的空格

git checkout -- [file name in repo including path]

git checkout -- [repo 中的文件名,包括路径]

example: git checkout -- views/userEditor.pug

示例:git checkout -- views/userEditor.pug

Less philosophy, more actual assistance. Sheesh.

更少的哲学,更多的实际帮助。嘘。