我正在清理我的树枝并合并它们。完成主分支后,我试图检查其他分支,但它不允许我这样做,因为有2个文件将被覆盖!我用-f来切换分支(我只希望看到那个分支发生了什么变化)。其他分支中没有任何东西。所以我没有看到删除它的任何危险。之后,我丢失了主分支中的所有其他更改。这是发生了什么!请告诉我如何恢复主分支中的所有更改?
[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}创建了几个分支,但它对我没有帮助。在主人或新创建的分支下,我仍然无法看到我的旧变化。
答案 0 :(得分:2)
如果您正在寻找blabla5.cgi
和git checkout -f otherbranch
的更改,它们将永远消失(除非您有与git无关的备份解决方案)。当你做git reflog
时它们就消失了。你之后所做的并不重要。
可悲的是,这次git reflog
无法帮助您,因为它似乎从未提交过它们。如果您将某些内容提交到分支中然后删除了该分支,git
可以帮助您。它也可以帮助你,如果你提交了一些东西,然后搞砸了提交(例如通过rebase或--amend)
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
当它警告你
{{1}}
答案 1 :(得分:2)
当你说你在主分支中丢失了变化时,我不清楚你的意思。从你所写的内容中我看到了两件事。
blabla*.cgi
。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在单个工作副本中切换分支的方法很难习惯。从中吸取教训:
git checkout -f
。git stash save
之前存储您的更改。 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
,但它没有。这表示您正在执行检出的存储库以及您执行reflog的存储库是不同的存储库。这可以解释为什么缺少某些东西。
我猜你会删除你的工作副本并克隆一个新的存储库。如果是这种情况,你可能在master中提交了未被推送的提交。他们走了。
答案 2 :(得分:1)
您可以尝试使用reflog。
22490d2 HEAD@{58}: commit: xxxx
262a092 HEAD@{59}: commit: xx2
0a168bc HEAD@{60}: commit: xx2
然后你可以看到reflog。使用id,您可以恢复上次更改。
git checkout -b my_new_branch HEAD@{60}
TransformBlock
。有了它,你可以在这一点上建立一个新的分支。