如何从丢失的 git 分支中恢复



我有一个项目,我正在做一个本地存储库。我最近有人创建了一个 github 存储库,我将本地存储库上传到 github(有各种错误和建议,但最终看起来它就在那里。这样做之后,从原籍/大师那里拉出来,我注意到我错过了一个月左右的工作。显然,我的过程消灭了一个名为"echo"的分支。

我现在想从丢失的分支中恢复这些更改。这是当前图片:

git status
  On branch master
  Your branch is up-to-date with 'origin/master'.
  nothing to commit, working directory clean
git branch
  * master
git checkout echo
  error: pathspec 'echo' did not match any file(s) known to git.

和:

git log --pretty=format:"%h - %an, %s"
  5825339 - David Rogers, Merge branch 'master' of https://github.com/...
  3c41742 - Someone Else, Initial commit
  6db6654 - unknown, Labels V 1.1.8
  2085c27 - David Rogers, Remove site 29 XML subdirectory from installer
  956c939 - David Rogers, Update user settings on upgrade
  1c39bbc - David Rogers, Update user settings on upgrade
  ac48392 - David Rogers, Initial commit

感谢这里的精彩页面:http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html ,我可以看到更大的图景:

git reflog
  5825339 HEAD@{0}: pull origin master: Merge made by the 'recursive' strategy.
  6db6654 HEAD@{1}: checkout: moving from echo to master
  b7642f4 HEAD@{2}: pull origin master: Merge made by the 'recursive' strategy.
  bb4cbe2 HEAD@{3}: commit: before github
  9288903 HEAD@{4}: commit: before github
  2e8336a HEAD@{5}: commit: prior to github
  6db6654 HEAD@{6}: checkout: moving from master to echo
  6db6654 HEAD@{7}: commit: Labels V 1.1.8
  2085c27 HEAD@{8}: commit: Remove site 29 XML subdirectory from installer
  956c939 HEAD@{9}: commit: Update user settings on upgrade
  1c39bbc HEAD@{10}: commit: Update user settings on upgrade
  ac48392 HEAD@{11}: commit (initial): Initial commit

(请注意"结帐:从主控移动到回声"。无论如何,在我看来,我丢失的分支的头部是 bb4cbe2。问题是,我如何恢复作品并将其交给原点/母版?

git fsck --lost-found
  Checking object directories: 100% (256/256), done.
  dangling commit b7642f4fa87f52b0991f88ce08e29d5aaa43edd3

我是否从 : git reset --hard bb4cbe2

如果我这样做,我会得到:

git reset --hard bb4cbe2
  HEAD is now at bb4cbe2 before github
git status
  On branch master
  Your branch and 'origin/master' have diverged,
  and have 3 and 2 different commits each, respectively.
    (use "git pull" to merge the remote branch into yours)
  Untracked files:
    (use "git add <file>..." to include in what will be committed)
        xxx.vshost.exe.manifest
        xxx/bin/Release/
        xxx/obj/Release/
        xxx/Release/
nothing added to commit but untracked files present (use "git add" to track)

看起来文件很好。现在怎么办?我从原产地拉吗?然后推到原点?

我应该提到我有一个完整的磁盘备份 从git reset之前开始。

大卫

我会对原点/主站进行变基,然后推送到原点/主站。 如果您不关心丢失提交历史记录,也可以合并到 master 上。

如果您在发布的 reflog 中看到之前拥有的所需状态,您可以从 reflog 中签出该状态,例如:

git checkout HEAD@{3}

这将带您进入具有由 reflog 标识的状态的分支。如果那是在你的拉动之前,那可能是你想要的。

git checkout b7642f4fa87f52b0991f88ce08e29d5aaa43edd3

这将检查悬空提交。如果这是你想要返回的内容,那么只需从该提交创建一个新分支,你就是黄金。如果不是,那么您可能需要更好地解释您的问题。

相关内容

  • 没有找到相关文章

最新更新