如何在使用 `git checkout` 后找回我最近的 Git 提交记录。

huangapple go评论68阅读模式
英文:

How to get back my recent commits in git after losing them with `git checkout`

问题

我在尝试解决一个问题时遇到了与Git相关的问题。由于这次尝试,我丢失了一些我最近创建的提交记录。其中包括一个合并提交和另一个我还没有推送到远程仓库的提交。

我执行了 git push 命令,但输出了 fatal: You are not currently on a branch.。这很奇怪,因为我的本地分支与远程分支同名。就在几个小时前,我成功执行了 git push

我在 Stack Overflow 上找到了一个页面,上面说在这种情况下我应该执行 git checkout origin:remote-branch-name。于是我这样做了。以下是输出:

警告:您将丢失一个提交,它不属于您的任何分支:

这绝对不是我想要做的事情。

通过执行 git reflog 命令,我仍然可以看到我的最新提交记录。在上一次成功推送后,我执行了 git pull origin --rebase,因为那时分支出现了分歧,我随后解决了这个问题。从那时起,我只进行了一次提交。

我如何让我的本地分支回到执行 git checkout origin:remote-branch-name 前的状态?唯一不同的应该是头指针的位置。

我更喜欢一个不会创建新提交记录的过程。我希望提交历史保持原样。

英文:

I'm have a problem with git resulting from an attempt at fixing another problem. I have lost some recent commits I have made. There was a merge commit and another that I had not yet pushed to the remote repository.

I did git push. It output fatal: You are not currently on a branch.. This is strange, as my local branch has the same name as a remote one. I did a git push just a few hours ago successfully.

I found a page on Stack Overflow that said I should do git checkout origin:remote-branch-name in such a situation. So I did. Here was the output:

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

This is certainly not what I wanted to do.

I can still see my latest commits by doing git reflog. After the last time I successfully pushed, I did git pull origin --rebase because there were diverging branches at that time which I then resolved. I only made one more commit since then.

How do I get my local branch to the state it was in before I did git checkout origin:remote-branch-name? The only thing that should be different is that the head should be attached.

I much prefer a process that doesn't make any new commits. I want the commit history to be as it was.

答案1

得分: 2

你尚未实际丢失任何提交(至今为止)。正如你所说,你之前的所有提交状态都记录在你的git reflog中,每个提交仍然带有其父级链。因此,你可以通过以下方式返回到其中任何状态:

git checkout HEAD@{n}

其中n是在git reflog中显示的数字,用于指定此提交状态,即你认为是你最后一个“良好”状态的地方。

换句话说,在git reflog中找到该状态,然后检出它。

现在你将处于分离头状态,所以下一步要做的是进入一个分支。要做到这一点,只需简单地说:

git switch -c branchname

其中branchname是你希望分配给此提交状态的分支的名称。如果branchname已经存在,Git会拒绝该命令,显示致命错误;要覆盖Git,将该命令更改为:

git switch -C branchname

请注意,如果这样做,你将丢失之前由branchname指向的提交(可能还有一些其父级链),因此在采取如此激烈的行动之前,请尽量确定你的立场。例如,下面的命令可以为你提供关于你现有分支的当前状态的非常全面的图像(也许有点过于详细):

git log --all --graph --oneline --decorate

当然,

git log --graph --oneline --decorate HEAD@{n}

将显示你打算检出的提交的父级链。因此,这些信息应该使你能够更明智地判断你想从哪里继续。

英文:

You have not actually lost any commits (yet). As you rightly say, all previous commit states you've been in are in your git reflog, and each of those commits still comes with its chain of parentage. So you can return to any of those states by saying

git checkout HEAD@{n}

where n is the number shown in the reflog in designating this commit state, the one you regard as the last "good" place where you were:

> How do I get my local branch to the state it was in before I did...

In other words, find that state in the reflog, and check it out.

You will now be in detached head mode, so the next thing to do is to get on a branch. The way to do that is simply to say

git switch -c branchname

where branchname is the name of the branch you wish to assign to this commit state. If branchname already exists, Git will refuse that order, with a fatal error; to override Git, change that command to

git switch -C branchname

Note that if you do that, you lose the commit (and possibly some its parent chain) that was previously pointed to by branchname, so try to be sure of your ground before doing something so drastic. A command such as

git log --all --graph --oneline --decorate

can give you a very complete picture (perhaps too complete) of the current state of play of your existing branches; and of course

git log --graph --oneline --decorate HEAD@{n}

will show you the parent chain of the commit you are proposing to check out. So that information should allow you to make more of an informed judgment about where you want to go from here.

huangapple
  • 本文由 发表于 2023年6月29日 06:32:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定