删除分支后丢失所有内容 - 无需提交(工作目录干净)

Lost everything after deleting branch - nothing to commit (working directory clean)

我正在清理我的分支并合并它们。完成 master 分支后,我尝试检出 otherbranch 但它不允许我这样做,因为有 2 个文件将被覆盖!我使用 -f 来切换分支(我只是希望看到那个分支发生了什么变化)。 otherbranch 什么也没有。所以我没有看到删除它有任何危险。之后,我丢失了 master 分支中的所有其他更改。这是发生了什么事!请告诉我如何恢复主分支中的所有更改?

[kali@core core]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   blabla.cgi
#   deleted:    blabla2.cgi
#   modified:   blabla3.cgi
#   new file:   blabla4.cgi
#   modified:   blabla5.cgi
#
[kali@core core]$ git merge otherbranch
Already up-to-date.
[kali@core core]$ git merge master
Already up-to-date.
[kali@core core]$ git checkout otherbranch
error: Your local changes to the following files would be overwritten by checkout:
    blabla3.cgi
    blabla5.cgi
Please, commit your changes or stash them before you can switch branches.
Aborting
[kali@core core]$ git checkout -f otherbranch
Switched to branch 'otherbranch'
[kali@core core]$ git status
# On branch otherbranch
nothing to commit (working directory clean)
[kali@core core]$ git checkout otherbranch
Already on 'otherbranch'
[kali@core core]$ git branch -d otherbranch
error: Cannot delete the branch 'otherbranch' which you are currently on.
[kali@core core]$ git checkout master
Switched to branch 'master'
[kali@core core]$ git branch -D otherbranch
Deleted branch otherbranch (was d3c9c6f).
[kali@core core]$ git branch
* master
[kali@core core]$ git status
# On branch master
nothing to commit (working directory clean)

编辑:正如 Stony 所建议的,我 运行 重新登录。这是输出!

d302fab HEAD@{0}: checkout: moving from otherbranch to master
d3c9c6f HEAD@{1}: checkout: moving from master to otherbranch
d302fab HEAD@{2}: checkout: moving from otherbranch to master
d3c9c6f HEAD@{3}: checkout: moving from otherbranch to otherbranch
d3c9c6f HEAD@{4}: checkout: moving from master to otherbranch
d302fab HEAD@{5}: pull: Fast-forward
f1569af HEAD@{6}: pull: Fast-forward
d0f72c6 HEAD@{7}: pull: Fast-forward
4c007c4 HEAD@{8}: pull: Fast-forward
d3c9c6f HEAD@{9}: checkout: moving from otherbranch to master
d3c9c6f HEAD@{10}: merge master: Fast-forward
506d77d HEAD@{11}: checkout: moving from master to otherbranch
d3c9c6f HEAD@{12}: checkout: moving from otherbranch to master
506d77d HEAD@{13}: checkout: moving from master to otherbranch
d3c9c6f HEAD@{14}: checkout: moving from otherbranch to master
506d77d HEAD@{15}: checkout: moving from master to otherbranch
d3c9c6f HEAD@{16}: pull: Fast-forward
72b9fee HEAD@{17}: pull: Fast-forward
2a7f380 HEAD@{18}: pull: Fast-forward
506d77d HEAD@{19}: checkout: moving from otherbranch to master
506d77d HEAD@{20}: checkout: moving from master to otherbranch
506d77d HEAD@{21}: checkout: moving from otherbranch to master
506d77d HEAD@{22}: checkout: moving from master to otherbranch
506d77d HEAD@{23}: commit: (Ticket ####)
0cb7e3a HEAD@{24}: pull: Fast-forward
fef5044 HEAD@{25}: pull: Fast-forward
a92f38f HEAD@{26}: pull: Fast-forward
1715fc0 HEAD@{27}: pull: Fast-forward
8cad089 HEAD@{28}: pull: Fast-forward
ecb5708 HEAD@{29}: pull: Fast-forward
87fd764 HEAD@{30}: commit: (Ticket ###)
745c5ad HEAD@{31}: clone: from git@oldseqcore:/opt/git/seqcore.git/

我使用 HEAD@{0}、HEAD@{5}、HEAD@{9} 创建了几个分支,但对我没有帮助。我仍然无法在 master 或新创建的分支下看到我的旧更改。

您可以尝试使用 reflog。

git reflog

然后就可以看到reflog了。使用 ID,您可以恢复上次更改。

22490d2 HEAD@{58}: commit: xxxx
262a092 HEAD@{59}: commit: xx2
0a168bc HEAD@{60}: commit: xx2

git checkout -b my_new_branch HEAD@{60}

例如。这样你就可以在那个点上创建一个新的分支。

如果您正在寻找对 blabla3.cgiblabla5.cgi 的更改,它们将永远消失(除非您有与 git 无关的备份解决方案)。当您 git checkout -f otherbranch 时,它们就消失了。之后你做了什么并不重要。

遗憾的是,git reflog 这次无法帮助您,因为看起来您从未犯过这些错误。 git reflog 如果您将某些内容提交到一个分支然后删除了该分支,则可以为您提供帮助。它也可以帮助你,如果你提交了一些东西,然后把提交搞砸了(例如通过 rebase 或 --amend)

git 当它警告你

时真的意味着它
error: Your local changes to the following files would be overwritten by checkout:
    blabla3.cgi
    blabla5.cgi
Please, commit your changes or stash them before you can switch branches.
Aborting

当你说你丢失了 master 分支中的更改时,我不清楚你的意思。从你写的,我看到有两件事丢失了。

  1. 未提交的更改 blabla*.cgi
  2. 分行otherbranch.

第一个不见了。未提交的更改,即使已暂存,也无法恢复。对不起。暂存区已被 git checkout -f 覆盖。 Git警告过你。

$ git checkout otherbranch
error: Your local changes to the following files would be overwritten by checkout:
    blabla3.cgi
    blabla5.cgi
Please, commit your changes or stash them before you can switch branches.
Aborting

Git 在单个工作副本中切换分支的方法可能很难习惯。从中吸取的教训:

  1. 永远不要使用 git checkout -f
  2. 在切换分支之前存储您的更改,git stash save
  3. 听 Git 当它不允许您做某事时。

otherbranch 是可以恢复的。 Git 有助于告诉您它所在的 ID。

$ git branch -D otherbranch
Deleted branch otherbranch (was d3c9c6f).

您可以使用该 ID 重新创建分支,d3c9c6f

git branch otherbranch d3c9c6f

不需要使用git branch -D。您应该始终首先使用 git branch -d。这样你就会知道合并的分支实际上已经合并了。


还有一个问题:你的reflog和你写的命令不匹配。您的 reflog 应该显示您签出 otherbranch,但它没有。这表明您正在执行签出的存储库和您执行重新登录的存储库是不同的存储库。这或许可以解释为什么缺少某些内容。

我猜你删除了你的工作副本并克隆了一个新的存储库。如果是这种情况,您可能在 master 中有未推送的提交。他们走了。