意外结帐后可以取回更改吗?
以下是我的仓库的状态。
[~/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")
顺便说一句,我做了 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)] ➔
我可以取回更改吗?
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.
[~/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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我正在使用 Intellij。 CTRL + z 对我有用,它会提示您“从磁盘重新加载更改”,然后单击“是”。
I'm using Intellij. CTRL + z works for me, it will prompt you "reload changes from disk", then hit Yes.
如果您使用 IDE 并且它有撤消选项,只需撤消更改,它将撤消从磁盘重新加载,从而恢复您的更改。大多数 IDE/编辑器都有此选项。
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.
1)
git reflog
你可以看到像下面的输出
2)
git checkout b0b3175f8890950218bba8906ffab0d6f84bf
1)
git reflog
you can see like below output
2)
git checkout b0b3175f8890950218bba8906ffab0d6f84bf
如果您使用 android studio,恢复更改(救了我)的最佳方法是:
1-右键单击您的根项目目录(您的包名称)。
2-当地历史->显示历史。
。
3-恢复您想要的任何更改。
我希望它有帮助
抱歉有任何错误,英语不是我的母语。
If you use android studio the best way to recover your changes (that saved me) is :
1-Right click on your root project directory (your package name) .
2-Local History -> Show History.
.
3-Revert any change you want .
I hope it helps
Sorry for any mistakes, English is not my native language.
您无法使用 git 命令执行任何操作,但如果您使用 IDE,则可以恢复您的更改。
我正在使用 PhpStorm,我们可以在其中查看文件更改历史记录。
只需右键单击文件,然后单击“显示本地历史记录”即可。
在“外部更改”选项卡下,您可以找到您意外删除的本地更改。
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.
您可以查看的另一件事是通过 IDE。我不小心签出了 2 个文件,并能够通过 IDE (Netbeans) 的“本地历史记录”恢复更改。这是多么大的祝福啊!
如果您使用的是 Eclipse,请右键单击该文件并转到“团队”->“显示本地历史记录”来执行此操作。
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!
If you're using Eclipse, do this by right clicking on the file and going to Team->Show Local History.
我认为您无法恢复这些私有数据(“私有”如“未添加到索引中,也未提交”,因此 git 不知道),除非您为当前工作目录准备了其他备份过程。
即使 Git 别名页面中没有建议这样做,我会主张使用某种别名进行结帐(就像有
alias rm /bin/rm -i
用法):,带有 '
git stash; git stash apply
' 是 Brian Campbell 在 他的答案。stason 提议在评论中:
这个问题让我想起 ycombinator 上关于此行为的辩论(摘录):
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.
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):, with the '
git stash; git stash apply
' being the "checkpoint technique" used by Brian Campbell in his answer.stason proposes in the comments:
This issue reminds me of a debate on this behavior on ycombinator (extracts):
除非您以前曾经对这些文件使用过 git add 或 git stash ,否则很遗憾没有。如果您已添加或隐藏它们,那么您应该能够通过
git reflog
。我一直对 git checkout 的这种破坏性行为感到不舒服。也许一个有用的增强功能是让这种 git checkout 在覆盖您的工作之前自动创建一个存储(以便通过引用日志捕获文件)。
Unless you have ever used
git add
orgit stash
with those files previously, then unfortunately no. If you have added or stashed them, then you should be able to find their hashes throughgit reflog
.I've never been comfortable with this destructive behaviour of
git checkout
. Perhaps a useful enhancement would be to have this sort ofgit checkout
automatically create a stash (so that files are captured through the reflog) before overwriting your work.如果您使用IntelliJ Idea,您可以在选定的项目文件夹上单击鼠标右键 -> 当地历史 ->你可以看到所有的文件。
If you use IntelliJ Idea you can click a right button mouse on a selected project folder -> Local History -> you can see all the files.
使用VS Code,可以通过 CTRL+Z 顺利地将本地更改从磁盘取回。所以尝试使用IDE。
Using VS Code it was smooth sailing getting the local changes from disk back, via CTRL+Z. So try using the IDE.
如果您在 Linux 上使用 vim,以下内容可能适用。
如果文件在活动缓冲区中打开,那么只要不在 vim 中重新加载文件,您就可以获得文件内容,并且可以通过保存来恢复。 .
如果文件未在活动缓冲区中打开,而是脏的,则源目录中应该有一个 .swp 文件,其中还包含可通过 vim -r file.swp 恢复的内容副本.
如果文件既没有在 antitive 缓冲区中打开也没有脏,并且您的工作副本位于 ext3 或 ext4 分区上,则 extundelete 也许能够找到最近删除的 .swp 文件和/或旧版本的源文件。将分区重新挂载为只读,例如
mount -o remount,ro /mnt/point
,然后运行如果包含工作副本的分区是根分区,它可能会拒绝重新挂载,然后尝试杀死所有服务,如果仍然不行,则关闭并使用 Live CD/USB/PXE 启动,如 GRML,然后运行上面的代码。我通过这种方式成功恢复了三分之一丢失的文件。
The following may apply in case you are using vim on Linux.
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 extundelete might 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 runIf 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.
如果您使用 Eclipse 作为 IDE 和 EGit,您可以使用团队菜单访问您的文件:
您将看到本地保存的所有版本,没有任何保存名称,在我的例子中,您可以轻松地检查 git 功能中所有未跟踪的更改并恢复丢失的代码。
if you use Eclipse as IDE and EGit, you have onto your file the Team-menu:
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.
您可以使用 vscode 文件时间线,只需右键单击文件
you can use vscode file timeline just right click file
如果IDE是Android Studio则打开已更改的文件并进入VCS ->当地历史 ->显示历史。打开的文件将显示在那里。
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.
正如 @VonC 提到的,
git checkout
的歧义是这里的根本原因。但是创建检查点可能会让你的藏品变得一团糟,如果维护不当,最终将无法使用。我最终只使用
git switch
而不是git checkout
用于分支切换。是的,你需要改变你的习惯,但从我的角度来看,这是最终的解决方案。Just as @VonC mentioned, the ambiguity of
git checkout
is the root cause here. But creating checkpoints might leave your stash with a huge mess and eventually not usable if not well maintained.I end up just using
git switch
instead ofgit checkout
for branch switching. Yes, you need to change your habit, but that's the ultimate solution from my point of view.