git pull:用远程版本替换本地版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17336895/
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 pull: replace local version with the remote version
提问by ben.IT
There is something I don't understand with the git pull
command.
I have a foobar
Git repository with two files, named f1
and f2
.
First, I clone this repo using this command:
这个git pull
命令有一些我不明白的地方。我有一个foobar
与两个文件,命名为Git仓库f1
和f2
。首先,我使用以下命令克隆这个 repo:
git clone git@mycompany:foobar/foobar.git
I make some wrong modifications to both the f1
and f2
files, add them to Git index and commit then.
我对f1
和f2
文件进行了一些错误的修改,将它们添加到 Git 索引然后提交。
git add -A
git commit -m 'a test wrong modification'
I decide now that this modifications were wrong and I want to replace my files with the remote version. So I use git pull
to do this.
我现在决定这个修改是错误的,我想用远程版本替换我的文件。所以我git pull
经常这样做。
git pull
Already up-to-date.
Git answers that project is already up to date. What's wrong with what I'm doing? How should I proceed to replace my local version with the remote version?
Git 回答该项目已经是最新的。我在做什么有什么问题?我应该如何继续用远程版本替换我的本地版本?
回答by Tala
git fetch origin
git reset --hard origin/master
Here is the good explanation about git pull git pull
这是关于 git pull git pull的很好解释
The git fetch
command imports commits from a remote repository into your local repo. The resulting commits are stored as remote branches instead of the normal local branches that we've been working with. This gives you a chance to review changes before integrating them into your copy of the project.
该git fetch
命令将提交从远程存储库导入本地存储库。结果提交存储为远程分支,而不是我们一直在使用的普通本地分支。这使您有机会在将更改集成到您的项目副本之前查看更改。
Command git pull <remote>
fetches the specified remote's copy of the current branch and immediately merge it into the local copy. This is the same as git fetch <remote>
followed by git merge origin/<current-branch>
. Since it is doing merge your commits were still there.
命令git pull <remote>
获取当前分支的指定远程副本并立即将其合并到本地副本中。这与git fetch <remote>
后面的 相同git merge origin/<current-branch>
。由于它正在合并,因此您的提交仍然存在。
After doing fetch
you can reset your working copy with reset command. Hard
is to ignore any changes in your local copy. git reset --hard origin/master
完成后,fetch
您可以使用 reset 命令重置您的工作副本。Hard
是忽略本地副本中的任何更改。git reset --hard origin/master
回答by bozdoz
To reset specific files, use git checkout
:
要重置特定文件,请使用git checkout
:
git checkout HEAD f1
git checkout HEAD f2
You could possibly also use wildcards (*), but I've never tried.
您也可以使用通配符 (*),但我从未尝试过。
Similar question: How to checkout only one file from git repository?
类似问题: 如何从 git 存储库中仅检出一个文件?
Resource on git checkout: http://gitready.com/beginner/2009/01/11/reverting-files.html
git checkout 资源:http: //gitready.com/beginner/2009/01/11/reverting-files.html
回答by AmirHd
Pulling is mainly to merge your remote repository with your local one. Short answer is nothing is wrong with your git setup as your latest remote changes are already there in your local repository. If someone else push changes to the remote repository that is when pull comes handy. Those changes get pulled and merged into your local repository after you do the pull.
拉取主要是将您的远程存储库与本地存储库合并。简短的回答是您的 git 设置没有问题,因为您的本地存储库中已经存在最新的远程更改。如果其他人将更改推送到远程存储库,那么 pull 就派上用场了。拉取后,这些更改会被拉取并合并到您的本地存储库中。