撤消 git pull
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4719205/
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
Undo a git pull
提问by Keyo
The questions I've seen for undoing a git pull a slightly different to mine.
我所看到的撤消 git pull 的问题与我的略有不同。
This is what I've done:
这就是我所做的:
There is a project in directory A
(not a repo). I initialized a repository in it, added the files, but did not commit anything. Then I pulled from repository B, which overwrote a bunch of my staged files.
目录中有一个项目A
(不是 repo)。我在其中初始化了一个存储库,添加了文件,但没有提交任何内容。然后我从存储库 B 中提取,它覆盖了我的一堆暂存文件。
I was under the impression I could use git reset --hard
to undo the merge. Of course that just checked out the HEAD of the commits I had just pulled in.
我的印象是我可以git reset --hard
用来撤消合并。当然,这只是检查了我刚刚引入的提交的 HEAD。
I should have branched and commited something before I did this pull, hindsight is nice. Is there some way I can get my old unstaged files back?
在我做这个 pull 之前,我应该分支并提交一些东西,事后看来很好。有什么方法可以让我找回旧的未暂存文件吗?
采纳答案by geoffreyd
It seems there is an answer to recovering staged files here:
似乎这里有一个恢复暂存文件的答案:
回答by Gintautas Miliauskas
A git pull
is the same as git fetch
+ git merge
. It is the merge step that overwrote your changes. To revert to the state before the merge, use git log
to find your latest commit and then use git reset --hard 1234abcd
where 1234abcd is the hash of the desired commit.
Agit pull
与git fetch
+相同git merge
。合并步骤覆盖了您的更改。要恢复到合并前的状态,请使用git log
查找您的最新提交,然后使用git reset --hard 1234abcd
where 1234abcd 是所需提交的哈希值。
Note that after the reset git pull
will merge the changes again. To revert the changes for good, use git revert
which will create an additional commit reversing the changes.
请注意,重置后git pull
会再次合并更改。要永久还原更改,请使用git revert
which 将创建一个额外的提交来还原更改。
回答by poke
Either just reset to the previous HEAD (i.e. the commit you had in your repository, that wasn't updated from the remote), or use git reflog
. The latter displays a list of actions that were made in your repository, the git pull
command should be part of that. Simply git reset --hard X
to a state before that command.
或者只是重置为之前的 HEAD(即您在存储库中的提交,未从远程更新),或者使用git reflog
. 后者显示在您的存储库中进行的操作列表,git pull
命令应该是其中的一部分。只需git reset --hard X
到该命令之前的状态。
Or just branch off the previous HEAD (you may delete/overwrite the previous branch), do your changes there and git pull again...
或者只是从前一个 HEAD 分支(您可以删除/覆盖前一个分支),在那里进行更改并再次 git pull ...
回答by J-16 SDiZ
I think you cannot full recover now. The best you can do is git status
to check which file(s) are still intact. If the uncommitted files are not here, they are lost.
我想你现在不能完全康复。您能做的最好的事情就是git status
检查哪些文件仍然完好无损。如果未提交的文件不在这里,它们就会丢失。
Next time, consider using git stash
before git pull
, or, better yet, use git fetch
.
下次,考虑使用git stash
before git pull
,或者更好的是,使用git fetch
.
IF YOU ARE A ADVANCED GIT USER, AND THE FILES ARE REALLYIMPORTANT:
如果你是一个高级的Git用户,并且这些文件真正重要:
If the files are staged, you may be able to extract them from the object repository. But these go right into git guts, which I don't think you should try.
如果文件已暂存,您也许能够从对象存储库中提取它们。但这些直接进入 git 胆量,我认为你不应该尝试。
If you really want, read these:
如果你真的想要,请阅读这些:
回答by Matthew Rankin
Update January 17, 2011, 6:53 PM
2011 年 1 月 17 日下午 6:53 更新
Just reread your question. I overlooked that you were pulling from project b as opposed to copying files into your project. Not sure if my answer still holds given your scenario. I think your best bet is to follow geoffreyd's answerand checkout the answer to the StackOverflow question Recovering added file after doing git reset --hard HEAD^.
只需重新阅读您的问题。我忽略了您是从项目 b 中提取而不是将文件复制到您的项目中。鉴于您的情况,不确定我的答案是否仍然成立。我认为您最好的选择是遵循geoffreyd 的回答并查看 StackOverflow 问题的答案Recovering added file after git reset --hard HEAD^。
Original Answer
原答案
Just do a git checkout -- <file>
.
只需做一个git checkout -- <file>
.
Here's the example I ran to confirm the above...
这是我运行以确认上述内容的示例...
# Create a test directory and some test files
$ mkdir blah
$ echo "I've been overwritten" > test1.txt
$ cd blah
$ echo "I'm the real test1.txt" > test1.txt
$ git init .
Initialized empty Git repository in /Users/matthew/blah/.git/
$ git add .
$ cp ~/test1.txt .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test1.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: test1.txt
#
$ cat test1.txt
I've been overwritten
$ git checkout -- test1.txt
$ cat test1.txt
I'm the real test1.txt
When you perform git status
it tells you to use git checkout -- <file>
to discard changes in the working directory. If I'm reading your question correctly, that's what you want to do.
当您执行git status
它时,它告诉您使用git checkout -- <file>
放弃工作目录中的更改。如果我正确地阅读了您的问题,那么这就是您想要做的。