git pull 实际上并没有从远程恢复我丢失的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16803904/
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 doesn't actually restore my missing files from remote
提问by Malloc
I have been working on a branch. I committed and pushed it up to the remote repository. Now some files on that branch are missing. Hopefully they are still available on the remote branch, so I tried to do git pull
:
我一直在一个分支上工作。我提交并将其推送到远程存储库。现在该分支上的一些文件丢失了。希望它们在远程分支上仍然可用,所以我尝试这样做git pull
:
git pull origin feature/my_branch
However, git said all is synchronized with remote:
但是,git 说所有都与远程同步:
* branch feature/my_branch -> FETCH_HEAD
Already up-to-date.
How can that be up to date? I cannot find my missing files locally, and my project didn't compile because of those missing files. Again, I can see those files in the commit history of the remote branch on bitbucket.
怎么可能是最新的?我在本地找不到丢失的文件,并且由于丢失的文件,我的项目没有编译。同样,我可以在 bitbucket 上远程分支的提交历史记录中看到这些文件。
回答by Daniel Gomes
git pull
corresponds to the sequence of a git fetch
and then git merge
. So if you delete the files from your local repository, doing a git pull
will notrestore them.
git pull
对应于 agit fetch
和 then的序列git merge
。因此,如果您从本地存储库中删除文件,执行 agit pull
将不会恢复它们。
In order to restore them you need to checkout that files from the remote repository using:
为了恢复它们,您需要使用以下命令从远程存储库中检出该文件:
git checkout <branch name> -- <path/to/file>
<branch name>
can be local or remote.
<branch name>
可以是本地的或远程的。
Example: Using remote repository
示例:使用远程存储库
- Remote upstream name:origin
- Remote branch:feature/xpto
- Local branch:feature/xpto
- File missing on local branch:example.js
- Recover the file to the local branch from remote branch:
git checkout origin/feature/xpto -- example.js
- Now you have
example.js
back on your local branch
- 远程上游名称:origin
- 远程分支:功能/xpto
- 本地分支:feature/xpto
- 本地分支上缺少文件:example.js
- 将文件从远程分支恢复到本地分支:
git checkout origin/feature/xpto -- example.js
- 现在你
example.js
回到了当地的分支机构
Example 2: Rollback a commit
示例 2:回滚提交
git log
- Current commit hash
7bb2154cf23b68feb0a0bbbd95446f2cd8bf3a44
- Want to rollback to this commit hash
dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad
git checkout dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad
- Only want to recover the file
example.js
from the above commit hashgit checkout dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad -- example.js
git log
- 当前提交哈希
7bb2154cf23b68feb0a0bbbd95446f2cd8bf3a44
- 想要回滚到这个提交哈希
dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad
git checkout dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad
- 只想
example.js
从上面的提交哈希中 恢复文件git checkout dbc04c23b2c6f2dd7bc9b5ef4aef22611a57c3ad -- example.js
回答by torek
Assuming you've described the problem correctly, @danielcsgomes's methods should work fine.
假设您已正确描述问题,@danielcsgomes 的方法应该可以正常工作。
You can find out which files are in your current commit, but not in your working directory, by running git status
. For instance (using -s
to keep the output short):
您可以通过运行git status
. 例如(-s
用于保持输出简短):
$ rm structy.py
$ git status -s
D structy.py
To get just that one file back:
只取回那个文件:
$ git checkout -- structy.py
and it's back.
它回来了。
Sometimes simply checking out some other branch or revision, then going back to where you were, will do the trick. For instance, the most recent commit on master
here was to add mp.py
, so if I do this:
有时只需检查一些其他分支或修订,然后返回到您所在的位置,就可以解决问题。例如,master
这里最近的提交是添加mp.py
,所以如果我这样做:
$ rm mp.py
$ git status -s
D mp.py
$ git checkout HEAD^
Note: checking out 'HEAD^'.
[verbiage about detached HEAD etc, snipped]
$ git checkout master
Previous HEAD position was 171ce6f... ignore *.log files
Switched to branch 'master'
$ git status -s
$ head -1 mp.py
from multiprocessing import Process, Queue
But this does not always work. It works for mp.py
because the previous rev (HEAD^
) doesn't have it, and the current one (master
) does, so moving forward from HEAD^
to master
restores the file. The file structy.py
has been around for quite a while though, so if I remove it and check out HEAD^
or some other branch or whatever, it remains deleted-from-work-dir. So something like git checkout -- structy.py
is an easier way to restore it, unless you want to go for the Very Big Hammer: git reset --hard
.
但这并不总是有效。它mp.py
之所以有效,是因为前一个 rev ( HEAD^
) 没有它,而当前一个 ( master
) 有,所以从HEAD^
到master
恢复文件。不过,该文件structy.py
已经存在了很长一段时间,因此如果我将其删除并签出HEAD^
或其他某个分支或其他任何内容,它仍然会从工作目录中删除。所以像这样的东西git checkout -- structy.py
是一种更简单的方法来恢复它,除非你想使用非常大的锤子:git reset --hard
。
If you're sure you want to abandon any changes you've made, you can git reset --hard HEAD
. For instance, consider:
如果您确定要放弃所做的任何更改,则可以git reset --hard HEAD
. 例如,考虑:
$ git rm mp.py
rm 'mp.py'
$ git status -s
D mp.py
(by the way, note that the "D" is in a different position here: I "git rm"ed, not just "rm"ed, so I'm telling git that if I do a commit, mp.py should be removed in the new commit, it's not just an accident that I removed it). Then I decide, no, this is wrong, I want everything back to the way it was in HEAD:
(顺便说一句,请注意“D”在这里的位置不同:我“git rm”ed,而不仅仅是“rm”ed,所以我告诉 git,如果我提交,mp.py 应该是在新提交中删除,我删除它不仅仅是一个意外)。然后我决定,不,这是错误的,我希望一切都回到 HEAD 中的样子:
$ git reset --hard HEAD
HEAD is now at 523bacb add multiprocessing example
$ git status -s
$
So, only use reset --hard
if you're quite sure.
所以,只有reset --hard
在你非常确定的情况下才使用。