4

a bit of a git newcomer and I wanted to make sure I didn't do any big damage to my git repository. Basically, I used git reset --hard HEAD to revert back to my previous commit as I messed some things up. I then realized I didn't want to lose everything I had done, so I did a git reset HEAD@{1}. Which didn't seem to do anything helpful. Then I tried a git reset --hard ORIG_HEAD.

So I figured, whatever, I'll do the little bit of work over. After I did, I went to push it, and I kept getting this error:

Updates were rejected because the tip of your current branch is behind

I tried git pull origin and it kept not allowing it for a bunch of reasons I can't totally remember.

In the end, I just ended up doing a git push -f and it seemed to work fine and my project is working well again.

Here's my git reflog output if it helps:

enter image description here

I'm just curious if I somehow really messed up either my BitBucket repository that I pushed to or my local one in terms of being able to revert back again. Is there any way to check if my git repo is still functioning properly?

EDIT: BitBucket also says I "stripped" a commit...

4

1 回答 1

9

会好的

放松。

为了减轻您的顾虑,请在不同的目录中克隆另一个副本。然后检查并引入您要推送的新代码。一切都会好起来的。

确认!提交悬而未决!

根据您的文字,我认为您可能已经重写了 master 的提交历史记录/树,因为git reset HEAD@{1}后面跟着git reset --hard ORIG_HEAD. 完全按照您所说的进行,我也“丢失”了一次提交。

这可能是您看到的错误消息。

20:19:44 (master) ~/code/wat/so_test$ git push
To git@github.com:rgbkrk/so_test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:rgbkrk/so_test.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.

强制 git push 导致主分支采用您的新路径。

还记得我说过要放松吗?好吧,那个提交并没有消失。如果您拥有正在使用的原始存储库,它应该仍然git refloggit log. 它就在外面晃来晃去。

2b47149 HEAD@{2}: commit: The bad commit
01346b9 HEAD@{3}: reset: moving to ORIG_HEAD
42af608 HEAD@{4}: reset: moving to HEAD@{3}
01346b9 HEAD@{5}: reset: moving to HEAD@{1}
2f2d2ce HEAD@{6}: commit: More junk text
01346b9 HEAD@{7}: commit: Text
42af608 HEAD@{8}: clone: from git@github.com:rgbkrk/so_test.git

如果您确实需要该提交,您可以对该提交和您当前主人的 HEAD 进行比较。git diff 2f2d2ce..HEAD在我为这个问题创建的回购的情况下。您也可以随时将其合并到 master 中。

查找“无法访问”提交的一种简单方法是使用git fsck,确保启用--no-reflogs.

20:48:02 (master) ~/code/wat/so_test$ git fsck --unreachable --no-reflogs
Checking object directories: 100% (256/256), done.
Checking objects: 100% (5/5), done.
unreachable tree 0ca2f12b74de7a33f6dc62374a85d982a96531ce
unreachable commit 2f2d2ce1b6591f0e7a5f67af81a125231ee2e9a1
unreachable blob bcb7bae71bbdfb79ba31ee41e4aa3a11b1ad2f7f

有关恢复丢失提交的更多信息,请查看git ready 的文章

未来笔记

需要指出的几件事:git reset --hard可能很危险,因为它会丢弃所有未提交的更改。如果您有一些工作想要保留,但不希望在此提交中使用,请使用git stash.

于 2013-11-02T01:09:37.647 回答