首页 文章

如何将HEAD移回以前的位置? (独立头)

提问于
浏览
89

在git中,我试图通过合并另一个分支来进行压缩提交,然后通过以下方式将 HEAD 重置为之前的位置:

git reset origin/master

但我需要走出这一步 . 如何将HEAD移回上一个位置?

我有将其移动到的提交的SHA1 frag( 23b6772 ) .
我怎样才能回到这个提交?

4 回答

  • 3

    这是一种非常简单易记的方法 . 检查2个条件并以1命令结束 . 然后你又回到了正轨 .

    如果

    你在'detached head'
    (即输入 git status ;你看 HEAD detached at <commit_id>

    并且

    现有的分支机构适合您的需求
    (即输入 git branch -v ;您会看到一个分支名称,其中包含代表您要继续工作的相关提交消息)

    然后

    只需检查该分支(即键入 git checkout <branch_name> ;您看到 Switched to branch <branch_name> ) .

    结果

    您现在可以像以前一样继续添加和提交您的工作;将在 <branch_name> 上跟踪更改 .

    请注意,如果您在分离HEAD时保存了工作,则在大多数情况下,工作将在上述过程中自动合并 . 如果您看到有关合并冲突的消息,请不要惊慌 . 有几个很棒的教程,包含修复冲突和完成合并的简单步骤 .

  • 223

    这个问题可以解读为:

    I was in detached-state with HEAD at 23b6772 and typed git reset origin/master (because I wanted to squash). Now I've changed my mind, how do I go back to HEAD being at 23b6772?

    直截了当的答案是: git reset 23b6772

    但我遇到了这个问题,因为每次我想引用之前的 HEAD 时,我都厌倦了输入(复制和粘贴)提交哈希或其缩写,并且谷歌搜索是否有任何类型的速记 .

    原来有!

    git reset - (或在我的情况下 git cherry-pick -

    顺便提一句,与 cd - 相同,返回* nix中的上一个当前目录!所以欢呼,一举两得 .

  • 3

    在回答之前,我们添加一些背景,解释这是什么 HEAD .

    首先是什么是HEAD?

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

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


    分离了HEAD

    如果您没有进行最新提交 - 意味着 HEAD 指向历史记录中的先前提交,则称为 detached HEAD .

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


    关于如何从分离的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
    

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

    # 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 设置回此提交 .

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

    git reflog
    git checkout HEAD@{...}
    

    这将使您回到所需的提交


    git reset --hard <commit_id>

    将HEAD“移动”回所需的提交 .

    # 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.
    
    • 注:(Since Git 2.7
      你也可以使用 git rebase --no-autostash .


    git revert <sha-1>

    "Undo"给定的提交或提交范围 .
    reset命令将"undo"在给定的提交中进行任何更改 .
    将提交具有撤消补丁的新提交,而原始提交也将保留在历史记录中 .

    # 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>
    

    此模式说明了哪个命令执行的操作 .
    如你所见 reset && checkout 修改 HEAD .

  • 9

    git reset 23b6772
    

    看你是否在正确的位置:

    git status
    

    你会看到一些东西

    在分支主机上你的分支由17个提交在'origin / master'后面,并且可以快进 .

    然后将HEAD修复为当前提交:

    git push --force
    

相关问题