意外结帐后可以取回更改吗?

发布于 2024-09-03 13:34:29 字数 1146 浏览 4 评论 0原文

以下是我的仓库的状态。

[~/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

や三分注定 2024-09-10 13:34:30

我正在使用 Intellij。 CTRL + z 对我有用,它会提示您“从磁盘重新加载更改”,然后单击“是”。

I'm using Intellij. CTRL + z works for me, it will prompt you "reload changes from disk", then hit Yes.

一袭白衣梦中忆 2024-09-10 13:34:30

如果您使用 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.

落墨 2024-09-10 13:34:30

1) git reflog

你可以看到像下面的输出

f7de337 (HEAD -> master, origin/master) HEAD@{0}: checkout: moving from b0b3175f8890950218bba8906ffab0d6f84bf to master

2) git checkout b0b3175f8890950218bba8906ffab0d6f84bf

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

泪意 2024-09-10 13:34:30

如果您使用 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.

enter image description here.

3-Revert any change you want .

I hope it helps
Sorry for any mistakes, English is not my native language.

吹泡泡o 2024-09-10 13:34:30

您无法使用 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.

蓝海似她心 2024-09-10 13:34:29

您可以查看的另一件事是通过 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.

无妨# 2024-09-10 13:34:29

我认为您无法恢复这些私有数据(“私有”如“未添加到索引中,也未提交”,因此 git 不知道),除非您为当前工作目录准备了其他备份过程。

即使 Git 别名页面中没有建议这样做,我会主张使用某种别名进行结帐(就像有 alias rm /bin/rm -i 用法):

[alias]
co = !sh -c 'git stash; git stash apply; git checkout "$@"'

,带有 'git stash; git stash apply' 是 Brian Campbell他的答案

stason 提议在评论中

co = "!git stash push -m \"co backup\"; git stash apply; git checkout \"$@\"" 

注意,我添加了一条消息来告知其他人的备份存储。 –

这个问题让我想起 ycombinator 上关于此行为的辩论(摘录):


我用 git 丢失了大量数据。
大部分与听起来无害的命令有关,这些命令在删除数据时不要求确认
例如,git checkout filename 相当于 svn revert filename
当然,git checkoutbranchname 做了完全不同的事情。
如果分支和文件共享相同的名称,git 将默认切换分支,但这并不能阻止 bash 自动完成功能破坏这一天。

这是一个疯狂的想法:如果您有一个无害的操作和一个危险的操作,请不要使用相同的命令来标记它们。


也许很烦人,但这是用户错误,而不是设计错误。使用 git,如果我想无损地丢弃我的工作副本,我只需“git stash”即可。
根据您的逻辑,“rm”是有缺陷的,因为当您传递 -f 而不是 -i 时,它不会要求确认。嗯,是的。抱歉。


如果rm somename相当于apt-get update,而rm othernamerm,那么你的类比会更准确-fr 其他名称
尽管如此,“get checkout foo”根据当前目录中是否存在名为 foo 的文件来执行两种完全不同的操作之一,这不可能是正确的


这是另一个疯狂的想法:不要在脏工作树上运行“git checkout ...”。问题已解决。
另一条:不要重复使用文件名作为分支名称。
老实说:我也有同样的问题,不小心调用 'rm' 毁了我的一天,但是当我嘟囔着咒骂时,这是我的懒惰/愚蠢,而不是 bash 完成或 ' 的行为rm'

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):

[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 Campbell in his answer.

stason proposes 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):


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 to svn revert filename.
Of course git 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.


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.


Your analogy would be more accurate if rm somename was the equivalent of apt-get update, and rm othername was rm -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


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'

风和你 2024-09-10 13:34:29

除非您以前曾经对这些文件使用过 git add 或 git stash ,否则很遗憾没有。如果您已添加或隐藏它们,那么您应该能够通过 git reflog

我一直对 git checkout 的这种破坏性行为感到不舒服。也许一个有用的增强功能是让这种 git checkout 在覆盖您的工作之前自动创建一个存储(以便通过引用日志捕获文件)。

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.

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.

你与清晨阳光 2024-09-10 13:34:29

如果您使用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.

挥剑断情 2024-09-10 13:34:29

使用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.

我不是你的备胎 2024-09-10 13:34:29

如果您在 Linux 上使用 vim,以下内容可能适用。

  • 如果文件在活动缓冲区中打开,那么只要不在 vim 中重新加载文件,您就可以获得文件内容,并且可以通过保存来恢复。 .

  • 如果文件未在活动缓冲区中打开,而是脏的,则源目录中应该有一个 .swp 文件,其中还包含可通过 vim -r file.swp 恢复的内容副本.

  • 如果文件既没有在 antitive 缓冲区中打开也没有脏,并且您的工作副本位于 ext3 或 ext4 分区上,则 extundelete 也许能够找到最近删除的 .swp 文件和/或旧版本的源文件。将分区重新挂载为只读,例如mount -o remount,ro /mnt/point,然后运行

    extundelete --recover-directory /path/to/working/copy /dev/sdaX
    

    如果包含工作副本的分区是根分区,它可能会拒绝重新挂载,然后尝试杀死所有服务,如果仍然不行,则关闭并使用 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 run

    extundelete --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.

捎一片雪花 2024-09-10 13:34:29

如果您使用 Eclipse 作为 IDEEGit,您可以使用团队菜单访问您的文件:

  1. 中右键单击您的文件
  2. 在列表项“Team” -> “显示本地历史记录”

您将看到本地保存的所有版本,没有任何保存名称,在我的例子中,您可以轻松地检查 git 功能中所有未跟踪的更改并恢复丢失的代码。

if you use Eclipse as IDE and EGit, you have onto your file the Team-menu:

  1. right click on your file from within
  2. 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.

聆听风音 2024-09-10 13:34:29

您可以使用 vscode 文件时间线,只需右键单击文件

在此处输入图像描述

you can use vscode file timeline just right click file

enter image description here

π浅易 2024-09-10 13:34:29

如果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.

慕烟庭风 2024-09-10 13:34:29

正如 @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 of git checkout for branch switching. Yes, you need to change your habit, but that's the ultimate solution from my point of view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文