Git在git checkout后丢失了本地提交的文件

4

我正在从另一个分支切换以进行推送。在进行检出时,我的编辑器中打开了文件,这可能导致了权限被拒绝的错误。git 是否丢失了我的文件?我不知道如何找回它们。以下是我所做的 - 丢失的文件是 js/Messages 目录 templates/Messages 目录 这些文件在我执行 git 操作时已经打开了。

C:\Users\***\Documents\myproject>git pull
Password for 'https://********@bitbucket.org':
Already up-to-date.

C:\Users\***\Documents\myproject>git checkout paymentmodule
error: cannot stat 'www/js/Messages': Permission denied
error: cannot stat 'www/js/Messages': Permission denied
error: cannot stat 'www/templates/Messages': Permission denied

C:\Users\***\Documents\myproject>git checkout paymentmodule
Switched to branch 'paymentmodule'

C:\Users\***\Documents\myproject>git push origin paymentmodule
Password for 'https://******@bitbucket.org':
To https://*******@bitbucket.org/********/myproject.git
 ! [rejected]        paymentmodule -> paymentmodule (non-fast-forward)
error: failed to push some refs to 'https://********@bitbucket.org/********/myproject.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

//THIS STEP WAS A MISTAKE
C:\Users\***\Documents\myproject>git fetch origin paymentmodule
Password for 'https://********@bitbucket.org':
From https://bitbucket.org/***********/myproject
 * branch            paymentmodule -> FETCH_HEAD

//This was what i wanted to do so I did it without reverting my previous step
C:\Users\***\Documents\myproject>git fetch origin paymentmodule:tmp
Password for 'https://**********@bitbucket.org':
From https://bitbucket.org/*******/myproject
 * [new branch]      paymentmodule -> tmp

C:\Users\***\Documents\myproject>git rebase tmp
First, rewinding head to replay your work on top of it...
warning: unable to access 'www/templates/Messages/.gitattributes': Permission denied
warning: unable to access 'www/templates/Messages/.gitattributes': Permission denied
warning: unable to access 'www/templates/Messages/.gitattributes': Permission denied
warning: unable to access 'www/templates/Messages/.gitattributes': Permission denied
Applying: Implement chat services
warning: unable to access 'www/templates/Messages/.gitattributes': Permission denied
Using index info to reconstruct a base tree...
.git/rebase-apply/patch:63: trailing whitespace.
For instructions on this, start with the
.git/rebase-apply/patch:586: trailing whitespace.
#
.git/rebase-apply/patch:588: trailing whitespace.
#
.git/rebase-apply/patch:613: trailing whitespace.

.git/rebase-apply/patch:759: trailing whitespace.

warning: unable to access 'www/templates/Messages/.gitattributes': Permission denied
warning: squelched 348 whitespace errors
warning: 353 lines add whitespace errors.
Falling back to patching base and 3-way merge...
error: cannot stat 'www/templates/Messages': Permission denied
error: Failed to merge in the changes.
Patch failed at 0001 Implement chat services
The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

//This was a mistake too
C:\Users\***\Documents\myproject>git rebase --skip

C:\Users\***\Documents\myproject>git rebase --abort
No rebase in progress?

编辑:

我运行了另一条命令,输出如下。我不确定这意味着什么。

C:\Users\***\Documents\myproject>git fsck --no-reflog
Checking object directories: 100% (256/256), done.
dangling blob 8860babe2931ea1e9dbafaa589e7c4feb19f14c7
dangling commit 1da3053dbe12d11e4ae65dc3a2ba720c9b272b7b
dangling commit cd03cd391e3447be272d3b8307ddb49cd3de1189
dangling blob c9648cae0b45ef174fa88aa5ad6b9008085a9766
dangling blob 2bc76e285d27d6979cf47148711e7c91379910c8
dangling blob 410f4c2f02192a56a9d4c0f01e847d85276799b4
dangling commit 60b1d2f1e2b7f8bfa42d4d2f49857a1b66db8575
dangling blob 949297fe49f427065fe0e74568af852e2796cc19
dangling commit 50d44e0896b10d461629fc5bffe60ee26996d433
dangling commit a8d4aa02b375cb3e04b90033a1a9d17697f6583d
dangling commit f5b5c1a66f450010f30c352caa3baa2f5a3b6576
dangling blob 5af70169556610abe956f748dc25af77494cc06e
dangling blob 981d6a55406d8b2ec63f0952ac65ad1afc893d8a
dangling commit d61db3516d2bbaf24efcf3fe9b644823bdc4a7ff

我需要重新写整个代码吗?


1
你说的“本地提交的文件”,这意味着你在某个时刻运行了“git commit”,对吧?那么你的文件并没有丢失,你只需要找到该提交的哈希值,就可以把它们找回来。尝试运行git reflog命令,它会显示你最近执行的操作(checkout、commit等),以及每个操作的提交哈希值。然后,对于每个看起来像你想要的哈希值,运行git show (hash),例如git show 1da3053。这将显示该提交的日志消息和内容。 - rmunn
是的,我在某个时刻提交了。请查看我的编辑。我看到这些悬挂的提交和blob。不确定如何恢复它们。奇怪的是git reflog没有显示我丢失的最后几次提交。 - Nikhil
一个 blob 只是单个文件的内容,这不是你要找的东西。看起来你还不太熟悉 blob 和 commit 之间的区别,所以当你不再试图恢复丢失的数据时,http://www.gitguys.com/topics/all-git-object-types-blob-tree-commit-and-tag 可能会对你有用。 - rmunn
不幸的是,这些提交是旧的提交,我认为它们是在我丢失之前创建的。 - Nikhil
让我们在聊天中继续这个讨论。 - rmunn
显示剩余3条评论
1个回答

9

在聊天讨论了很久后,发现你之前提交的代码(日志信息为“实现聊天服务”)仍然在reflog中可见。(你在问题中说它不可见,但你只是没有看到它)。所以你需要做的事情(而且我从聊天记录中知道你已经做了)是:

git reflog

reflog中大约往下十行,你会看到" a8d4aa0 HEAD@{12}: commit: Implement chat services "。所以你需要的提交哈希是a8d4aa0。现在你可以简单地执行以下操作:
git checkout -b new-branch-name a8d4aa0

现在你的提交已经安全地保存在一个新分支上,并准备根据需要合并或变基。
顺便说一下,它也可以通过git fsck --no-reflog找到;当你说“不幸的是这些提交是旧的提交,我认为它们是在我丢失之前创建的”时,你放弃得太早了。以下是你发布的git fsck --no-reflog输出中的行:
dangling commit a8d4aa02b375cb3e04b90033a1a9d17697f6583d

如果你运行git show a8d4aa02b375cb3e04b90033a1a9d17697f6583d,你将找到你的提交。
教训:当寻找丢失的提交时,永远不要放弃。永远不要投降。(感谢《银河飞将》!)

1
此外,我强烈推荐阅读http://pcottle.github.io/learnGitBranching/(不是我之前发布的链接)。这将使您熟悉`rebase`和其他操作,并让您更好地可视化在提交、rebase或合并时发生的情况。我发现这是我使用过的最好的Git学习工具。 - rmunn
谢谢@rmunn!我确实放弃得太早了,忽略了reflog中的消息。通过您的故障排除步骤,我对git有了更清晰的理解。 - Nikhil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接