gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:55:16 25 4
gpt4 key购买 nike

我正在清理我的分支并 merge 它们。完成 master 分支后,我尝试 check out 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 所建议的,我运行 reflog。这是输出!

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 或新创建的分支下看到我的旧更改。

最佳答案

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

  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。这样你就会知道 merge 的分支实际上已经 merge 了。


还有一个问题:你的reflog和你写的命令不匹配。您的 reflog 应该显示您正在 checkout otherbranch,但事实并非如此。这表明您正在执行 check out 的存储库和您执行重新登录的存储库是不同的存储库。这或许可以解释为什么缺少某些东西。

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

关于git - 删除分支后丢失所有内容 - 无需提交(工作目录干净),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33003409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com