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

时间:2021-12-25 00:01:18

I have a project I am working on which I started on a local repo. I recently had someone create a github repo and I uploaded the local repo to github (there were various errors and suggestions, but it eventually looked like it was up there.) After doing that, and pulling from origin/master, I noticed I was missing a month or so of work. Apparently my process wiped out a branch named "echo."

我有一个我正在研究的项目,我从一个本地回购开始。我最近有人创建了一个github仓库,我将本地仓库上传到github(有各种各样的错误和建议,但最终看起来就像是在那里。)做完之后,从原点/主人那里拉,我注意到我是缺少一个月左右的工作。显然我的进程消灭了一个名为“echo”的分支。

I now want to recover these changes from the lost branch. Here is the current picture:

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

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.

and:

和:

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

Thanks to the wonderful pages here: http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html , I can see the bigger picture:

感谢这里精彩的网页: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

(Note the "checkout: moving from master to echo.") Anyways, it looks to me like the head of my missing branch is bb4cbe2. The question is, how to I restore the work and git it up to the origin/master?

(注意“结帐:从主人移动到回声。”)无论如何,它看起来像我缺少的分支的头部是bb4cbe2。问题是,如何恢复工作并将其归结为原点/主人?

Also,

也,

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

Do I start with : git reset --hard bb4cbe2

我应该从:git reset --hard bb4cbe2开始

If I do that, I get:

如果我这样做,我得到:

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)

Looks like the files are good. Now what? Do I pull from origin? Then push to origin?

看起来文件很好。怎么办?我是否从原点拉?然后推到原点?

I should mention that I have a full disk backup from before the git reset.

我应该提一下,我在git reset之前有一个完整的磁盘备份。

David

大卫

3 个解决方案

#1


0  

I'd do a rebase onto origin/master, then a push to origin/master. You could also do a merge onto master if you don't care about losing your commit history.

我会对origin / master做一个rebase,然后推送到origin / master。如果您不关心丢失提交历史记录,也可以合并到master。

#2


0  

If you see the desired state that you had before within the reflog that you posted, you can just checkout that state from the reflog, eg:

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

git checkout HEAD@{3}

And that will bring you to a branch with that state as identified by the reflog. If that was before your pull that's likely what you want.

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

#3


0  

git checkout b7642f4fa87f52b0991f88ce08e29d5aaa43edd3

This will check out the dangling commit. If it's what you want to get back, then just create a new branch from that commit and you're golden. If it's not, then you may want to explain your problem a little better.

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

#1


0  

I'd do a rebase onto origin/master, then a push to origin/master. You could also do a merge onto master if you don't care about losing your commit history.

我会对origin / master做一个rebase,然后推送到origin / master。如果您不关心丢失提交历史记录,也可以合并到master。

#2


0  

If you see the desired state that you had before within the reflog that you posted, you can just checkout that state from the reflog, eg:

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

git checkout HEAD@{3}

And that will bring you to a branch with that state as identified by the reflog. If that was before your pull that's likely what you want.

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

#3


0  

git checkout b7642f4fa87f52b0991f88ce08e29d5aaa43edd3

This will check out the dangling commit. If it's what you want to get back, then just create a new branch from that commit and you're golden. If it's not, then you may want to explain your problem a little better.

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