本文介绍了如何将HEAD移回先前的位置? (独立的头)&撤消提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Git中,我试图通过合并到另一个分支中来执行squash commit,然后通过以下方式将HEAD重置为先前的位置:

In Git, I was trying to do a squash commit by merging in another branch and then resetting HEAD to the previous place via:

git reset origin/master

但是我需要走出这一步.如何将HEAD移回先前的位置?

But I need to step out of this. How can I move HEAD back to the previous location?

我具有将其移到的提交的SHA-1片段(23b6772).我该如何返回到此提交?

I have the SHA-1 fragment (23b6772) of the commit that I need to move it to. How can I get back to this commit?

推荐答案

在回答之前,让我们添加一些背景知识,以说明HEAD是什么.

Before answering, let's add some background, explaining what this HEAD is.

HEAD只是对当前分支上当前提交(最新)的引用.
在任何给定时间(git worktree除外)只能有一个HEAD.

HEAD is simply a reference to the current commit (latest) on the current branch.
There can only be a single HEAD at any given time (excluding git worktree).

HEAD的内容存储在.git/HEAD内部,并且包含当前提交的40个字节的SHA-1.

The content of HEAD is stored inside .git/HEAD and it contains the 40 bytes SHA-1 of the current commit.

如果您不在最新提交上-表示HEAD指向历史记录中的先前提交,则称为 detached HEAD .

If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history it's called detached HEAD.

在命令行上,它看起来像这样-SHA-1而不是分支名称,因为HEAD并不指向当前分支的尖端:

On the command line, it will look like this - SHA-1 instead of the branch name since the HEAD is not pointing to the tip of the current branch:

git checkout <commit_id>
git checkout -b <new branch> <commit_id>
git checkout HEAD~X // x is the number of commits t go back

这将签出指向所需提交的新分支.
该命令将签出给定的提交.
此时,您可以创建一个分支并从此开始工作.

This will checkout new branch pointing to the desired commit.
This command will checkout to a given commit.
At this point, you can create a branch and start to work from this point on.

# Checkout a given commit.
# Doing so will result in a `detached HEAD` which mean that the `HEAD`
# is not pointing to the latest so you will need to checkout branch
# in order to be able to update the code.
git checkout <commit-id>

# Create a new branch forked to the given commit
git checkout -b <branch name>


git reflog

您也可以始终使用reflog.
git reflog 将显示更新HEAD的所有更改,并且签出所需的reflog条目会将HEAD设置回此提交.


git reflog

You can always use the reflog as well.
git reflog will display any change which updated the HEAD and checking out the desired reflog entry will set the HEAD back to this commit.

每次修改HEAD时,reflog

Every time the HEAD is modified there will be a new entry in the reflog

git reflog
git checkout HEAD@{...}

这将使您回到所需的提交

This will get you back to your desired commit

移动"您的HEAD返回所需的提交.

"Move" your HEAD back to the desired commit.

# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts if you've modified things which were
# changed since the commit you reset to.

  • 注意:(自Git 2.7起 ),您也可以使用git rebase --no-autostash.
    • Note: (Since Git 2.7) you can also use the git rebase --no-autostash as well.
    • 撤消"给定的提交或提交范围.
      重置命令将撤消"命令.给定提交中所做的任何更改.
      带有撤消补丁的新提交将被提交,而原始提交也将保留在历史记录中.

      "Undo" the given commit or commit range.
      The reset command will "undo" any changes made in the given commit.
      A new commit with the undo patch will be committed while the original commit will remain in the history as well.

      # Add a new commit with the undo of the original one.
      # The <sha-1> can be any commit(s) or commit range
      git revert <sha-1>
      


      此架构说明了哪个命令可以执行什么操作.
      如您所见,reset && checkout修改HEAD.


      This schema illustrates which command does what.
      As you can see there, reset && checkout modify the HEAD.

      这篇关于如何将HEAD移回先前的位置? (独立的头)&amp;撤消提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:29