git 意外结账后找回更改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2961240/
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
Get back the changes after accidental checkout?
提问by Autodidact
The following was the status of my repo.
以下是我的回购状态。
[~/rails_apps/jekyll_apps/nepalonrails (design)?] ? gst
# On branch design
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: _layouts/default.html
# deleted: _site/blog/2010/04/07/welcome-to-niraj-blog/index.html
# deleted: _site/blog/2010/04/08/the-code-syntax-highlight/index.html
# deleted: _site/blog/2010/05/01/showing-demo-to-kalyan/index.html
# deleted: _site/config.ru
# deleted: _site/index.html
# deleted: _site/static/css/style.css
# deleted: _site/static/css/syntax.css
# modified: static/css/style.css
#
no changes added to commit (use "git add" and/or "git commit -a")
Accedently, I did git checkout -f
and now the changes are gone which I wasnt supposed to do.
顺便说一句,我做到了git checkout -f
,现在我不应该做的更改已经消失了。
[~/rails_apps/jekyll_apps/nepalonrails (design)?] ? git co -f
[~/rails_apps/jekyll_apps/nepalonrails (design)] ? gst
# On branch design
nothing to commit (working directory clean)
[~/rails_apps/jekyll_apps/nepalonrails (design)] ?
Can I get back the changes back?
我可以取回更改吗?
采纳答案by VonC
I don't think you can recover those private data ("private" as in "not added in the index, nor committed", so unknown to git), unless you have other backup process in place for your current working directory.
我认为您无法恢复这些私有数据(“私有”如“未添加到索引中,也未提交”,因此 git 未知),除非您为当前工作目录准备了其他备份过程。
Even if this is not proposed in the Git Aliases page, I would argue for some kind alias for checkout (like there is the alias rm /bin/rm -i
usage):
即使在Git Aliases 页面中没有提出这一点,我也会为结帐alias rm /bin/rm -i
使用某种别名(就像有用法一样):
[alias]
co = !sh -c 'git stash; git stash apply; git checkout "$@"'
, with the 'git stash; git stash apply
' being the "checkpoint technique" used by Brian Campbellin his answer.
,' git stash; git stash apply
'是布赖恩坎贝尔在他的回答中使用的“检查点技术” 。
stasonproposes in the comments:
co = "!git stash push -m \"co backup\"; git stash apply; git checkout \"$@\""
Note, I added a message to tell backup stashes from others. –
请注意,我添加了一条消息来告诉其他人的备份存储。——
This issue reminds me of a debate on this behavior on ycombinator(extracts):
这个问题让我想起了在ycombinator(摘录)上关于这种行为的辩论:
I've lost plenty of data with git.
Most of it has to do with innocuous-sounding commands that don't ask for confirmation when deleting data.
For example,git checkout filename
is equivalent tosvn revert filename
.
Of coursegit checkout branchname
does something completely different.
If a branch and a file share the same name, git will default to switching branches, but that doesn't stop bash autocomplete from ruining the day.Here's a crazy idea: If you have an innocuous action and a dangerous action, do not label them with the same command.
我用 git 丢失了大量数据。
其中大部分与听起来无害的命令有关,这些命令在删除数据时不要求确认。
例如,git checkout filename
相当于svn revert filename
。
当然git checkout branchname
做一些完全不同的事情。
如果一个分支和一个文件共享相同的名称,git 将默认切换分支,但这并不能阻止 bash 自动完成破坏一天。这是一个疯狂的想法:如果你有一个无害的动作和一个危险的动作,不要用相同的命令标记它们。
Annoying, maybe, but this is user error, not design error. With git, if I want to losslessly discard my working copy, I can just "
git stash
".
By your logic, "rm" is flawed because it doesn't ask for confirmation when you pass-f
instead of-i
. Well, yeah. Sorry.
烦人,也许,但这是用户错误,而不是设计错误。使用 git,如果我想无损地丢弃我的工作副本,我可以只用“
git stash
”。
按照你的逻辑,“rm”是有缺陷的,因为当你通过-f
而不是-i
. 嗯,是的。对不起。
Your analogy would be more accurate if
rm somename
was the equivalent ofapt-get update
, andrm othername
wasrm -fr othername
.
Still, it can't be right that "get checkout foo
" does one of two COMPLETELY different things depending on whether or not there is a file called foo in the current directory
如果
rm somename
相当于apt-get update
,并且rm othername
是,则您的类比会更准确rm -fr othername
。
尽管如此,get checkout foo
根据当前目录中是否有一个名为 foo 的文件," " 执行两种完全不同的事情之一是不对的
Here's another crazy idea: don't run '
git checkout ...
' on a dirty work tree. Problem solved.
Another one: don't reuse filenames as branch-names.
To be honest: I have the same problem with careless invocations of 'rm
' ruining my day but when I'm muttering curses it is at my lazyness/stupidity and not at bash completions or the behavior of 'rm
'
这是另一个疯狂的想法:不要
git checkout ...
在肮脏的工作树上运行 ' '。问题解决了。
另一个:不要重用文件名作为分支名称。
老实说:我有同样的问题,粗心地调用 'rm
' 毁了我的一天,但是当我喃喃自语时,这是我的懒惰/愚蠢,而不是 bash 完成或 'rm
'的行为
回答by gigi2
Another thing you can look at is through your IDE. I accidentally checkout 2 files and was able to bring back the changes through the 'local history' of my IDE (netbeans). What a blessing!
您可以查看的另一件事是通过您的 IDE。我不小心检出了 2 个文件,并且能够通过我的 IDE (netbeans) 的“本地历史记录”恢复更改。多么幸福啊!
回答by Greg Hewgill
Unless you have ever used git add
or git stash
with those files previously, then unfortunately no. If you have added or stashed them, then you should be able to find their hashes through git reflog
.
除非您以前使用过git add
或git stash
使用过这些文件,否则很遗憾没有。如果您添加或隐藏了它们,那么您应该能够通过git reflog
.
I've never been comfortable with this destructive behaviour of git checkout
. Perhaps a useful enhancement would be to have this sort of git checkout
automatically create a stash (so that files are captured through the reflog) before overwriting your work.
我对git checkout
. 也许一个有用的增强是git checkout
在覆盖您的工作之前让这种自动创建一个存储(以便通过引用日志捕获文件)。
回答by alexei
The following may apply in case you are using vim on Linux.
如果您在 Linux 上使用 vim,以下内容可能适用。
If files are open in an active buffer then you've got the file content as long as you don't reload the file in vim, and can restore by saving it. .
If files are not open in an active buffer, but are dirty, then there should be an .swp file in the source directory which also has a copy of the content that is recoverable via
vim -r file.swp
.If the files are neither open in antive buffer nor dirty, and if your working copy is on an ext3 or ext4 partition, then extundeletemight be able to find the recently deleted .swp files and/or an older version of the source files. Remount the partition as read-only, e.g.
mount -o remount,ro /mnt/point
, and runextundelete --recover-directory /path/to/working/copy /dev/sdaX
If the partition that contains the working copy is the root partition, it may refuse to remount, then try killing all services, and if still no go, then shutdown and boot using a Live CD/USB/PXE, like GRML, and run the above. I was successful in recovering one out of three lost files this way.
如果文件在活动缓冲区中打开,那么只要您不在 vim 中重新加载文件,您就可以获得文件内容,并且可以通过保存来恢复。.
如果文件未在活动缓冲区中打开,而是脏的,则源目录中应该有一个 .swp 文件,该文件还包含可通过
vim -r file.swp
.如果文件既不是在反缓冲区中打开也不是脏文件,并且您的工作副本位于 ext3 或 ext4 分区上,那么extundelete可能能够找到最近删除的 .swp 文件和/或旧版本的源文件。将分区重新挂载为只读,例如
mount -o remount,ro /mnt/point
,然后运行extundelete --recover-directory /path/to/working/copy /dev/sdaX
如果包含工作副本的分区是根分区,它可能会拒绝重新挂载,然后尝试杀死所有服务,如果仍然不行,则使用Live CD / USB / PXE关闭并启动,例如GRML,并运行以上。通过这种方式,我成功地恢复了三分之一丢失的文件。
回答by Jonathan
if you use Eclipse as IDEand EGit, you have onto your file the Team-menu:
如果您使用Eclipse 作为 IDE和EGit,则您的文件中的团队菜单:
- right click on your file from within
- List item "Team" -> "Show Local History"
- 从内部右键单击您的文件
- 列表项“团队”->“显示本地历史”
you will see all the version save locally without any saving name, in my case but, you can easily check all the untracked changes out of git features and restore your missing code.
您将看到所有版本都保存在本地而没有任何保存名称,在我的情况下,您可以轻松地从 git 功能中检查所有未跟踪的更改并恢复丢失的代码。
回答by Deep
If IDE is Android Studio then open the file that has been changed and go to VCS -> Local History -> Show History. Opened file will be shown there.
如果 IDE 是 Android Studio,则打开已更改的文件并转到 VCS -> Local History -> Show History。打开的文件将显示在那里。
回答by Oscar Zhang
I'm using Intellij. CTRL+ zworks for me, it will prompt you "reload changes from disk", then hit Yes.
我正在使用 Intellij。CTRL+z对我有用,它会提示你“从磁盘重新加载更改”,然后点击是。
回答by BloodLoss
1) git reflog
1) git reflog
you can see like below output
你可以看到如下输出
f7de337 (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from b0b3175f8890950218bba8906ffab0d6f84bf to master
2) git checkout b0b3175f8890950218bba8906ffab0d6f84bf
2) git checkout b0b3175f8890950218bba8906ffab0d6f84bf
回答by Sushil88
There is nothing you can do with git commands but if you are using IDE then you can recover your changes. I am using PhpStorm where we can see file changes history. Simply right-click on file and click on show local history. Under External changes tab, you can find your local changes which you have accidentally removed.
您无法使用 git 命令执行任何操作,但是如果您使用的是 IDE,那么您可以恢复您的更改。我正在使用 PhpStorm,我们可以在其中查看文件更改历史记录。只需右键单击文件,然后单击显示本地历史记录。在外部更改选项卡下,您可以找到意外删除的本地更改。
回答by ShankarDaruga
If you are using IDE and if it has undo option, simply undo the changes, it'll undo the reload from disk which will bring back your changes. Most IDEs / editors have this option.
如果您使用的是 IDE 并且它有撤消选项,只需撤消更改,它就会撤消从磁盘重新加载,这将带回您的更改。大多数 IDE/编辑器都有这个选项。