如何将HEAD移回到以前的位置? (分离头) [英] How to move HEAD back to a previous location? (Detached head)

查看:429
本文介绍了如何将HEAD移回到以前的位置? (分离头)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在git中,我试图通过在另一个分支中合并,然后通过以下方法将 HEAD 重置为上一个地方来执行压缩提交:

  git reset origin / master 

但我需要走出这一步。
如何将HEAD移回原位置?

我需要将其提交到的提交SHA1 frag( 23b6772 )。

我该如何回到这个提交?

解决方案

在回答之前,让我们添加一些背景知识, code> HEAD 。



首先什么是HEAD? code $ $ b $ HEAD 仅仅是对当前提交的引用(最新)。

在任何给定时间只能有一个 HEAD 。 (不包括 git worktree


$ b HEAD 存储在 .git / HEAD 内部,它包含当前提交的40字节SHA-1。




$

分离的HEAD



如果你不是最新的提交 - 这意味着 HEAD 指向历史上的一个先前的提交,它被称为 detached HEAD


$ b



在命令行中,它看起来像这样 - SHA-1而不是分支名称,因为 HEAD 不指向当前分行的小费








关于如何从分离的HEAD中恢复的几个选项:




< a href =https://git-scm.com/docs/git-checkout =noreferrer> git checkout



  git checkout< commit_id> 
git checkout -b<新分支> < commit_id>
git checkout HEAD〜X // x是提交的数量t返回

将检出指向所需提交的新分支。

此命令将检出到给定的提交。

此时,您可以创建分支并从此开始工作。

 #签出一个给定的提交。 
#这样做会导致`detached HEAD`,这意味着`HEAD`
#没有指向最新的,所以你需要结账分支
#以便能够更新代码。
git checkout< commit-id>

#为给定的提交创建一个新的分支
git checkout -b<分支名称>











git reset HEAD --hard< commit_id>



将您的头移回所需的提交。

 #这会破坏所有本地修改。 
#如果您想保留未提交的工作,请不要这样做。
git reset --hard 0d1d7fc32

#或者,如果有工作要保留:
git stash
git reset --hard 0d1d7fc32
git stash pop
#这将保存修改,然后在重置后重新应用该修补程序。
#如果您修改了重置为提交后
#已更改的内容,则可能会发生合并冲突。




  • 注意:(既然Git 2.7

    ,您也可以使用 git rebase --no-autostash









git revert< sha-1>



提交或提交范围。

重置命令将撤消给定提交中所做的任何更改。

提交时使用撤销修补程序的新提交将被提交,而原始提交将保留在历史上也是如此。

#使用原来的撤消操作添加新的提交。
#< sha-1>可以是任何提交或提交范围
git revert< sha-1>






这个模式说明哪个命令做了什么。 >
正如你所看到的那样 reset&& checkout 修改 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

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

I have the SHA1 frag (23b6772) of the commit that I need to move it to.
How can i get back to this commit?

解决方案

Before answering lets add some background, explaining what is this HEAD.

First of all what is 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)

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


detached HEAD

If you are not on the latest commit - meaning that HEAD is pointing to a prior commit in history its called detached 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 the tip of the current branch


A few options on how to recover from a detached HEAD:


git checkout

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

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.

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


git reset HEAD --hard <commit_id>

"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.

  • Note: (Since Git 2.7)
    you can also use the git rebase --no-autostash as well.


git revert <sha-1>

"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 commited while the original commit will remain in the history as well.

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


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

这篇关于如何将HEAD移回到以前的位置? (分离头)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆