如何删除当前HEAD之后的提交? [英] How can I delete commits that are after the current HEAD?

查看:89
本文介绍了如何删除当前HEAD之后的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Git存储库中,我连续创建了三个提交:commit1commit2commit3.

In my Git repository, I created three commits in a row: commit1, commit2, and commit3.

然后我意识到自己搞砸了commit2commit3,并决定回到commit1.为此,我跑了

I then realized I messed up in commit2 and commit3, and decided to go back to commit1. To do that, I ran

git checkout commit1

现在我在commit1中.如何删除commit2commit3?

Now I am in commit1. How do I delete commit2 and commit3?

推荐答案

签出您的分支,然后将其重置

根据您的描述,并假设您在签出commit1(在我的下图中的C1之前)位于某个名为mybranch的分支上,您必须处于以下情况:

Check your branch out and then reset it

Based on your description and under the assumption you were on some branch called mybranch before checking out commit1 (C1 in my graphs below), you must be in the following situation:

C1 [HEAD]
 \
  C2 -- C3 [mybranch]

提交C2C3仍出现在git log的输出中,因为它们仍然可以从mybranch引用中访问.另外,请注意HEAD是分离的.你应该做的是...

Commits C2 and C3 still appear in the output of git log because they're still reachable from the mybranch refererence. Also, note that HEAD is detached. What you should do is...

  1. 通过运行将HEAD重新连接到mybranch

git checkout mybranch

这应该使您处于以下情况:

This should put you in the following situation:

C1
 \
  C2 -- C3 [HEAD -> mybranch]

  • 通过运行将mybranch分支重置为其尖端的祖父母

  • Reset the mybranch branch to its tip's grandparent, by running

    git reset --hard mybranch~2
    

    那应该使您处于以下情况:

    That should put you in the following situation:

    C1 [HEAD -> mybranch]
    

  • 由于提交C2C3现在变得不可访问(即已删除"),因此它们没有显示在最后一张图上.

    Because commits C2 and C3 have now become unreachable (i.e. "deleted"), they're not shown on this last graph.

    这可能有点厚脸皮,但这是为什么其他两个答案不起作用的解释.正如cmbuckley在他的评论

    This may be a bit cheeky, but here is an explanation of why the other two answers won't work. As correctly pointed out by cmbuckley in his comment,

    git reset重置您所在的当前分支的状态(因此您需要在该分支上才能执行此操作).如果您已签出commit1,则可能不在分支(HEAD状态为分离状态)上.

    git reset resets the state of the current branch you’re on (so you’d need to be on the branch to do that). If you’ve checked out commit1, you’re probably not on a branch (detached HEAD state).

    由于OP(Imray)处于分离的HEAD状态,因此在将HEAD重新连接到分支之前运行git-reset ,将移动有问题的分支引用.这是一个说明这一点的玩具示例.

    Since the OP (Imray) is in detached HEAD state, running git-reset before reattaching HEAD to the branch will not move the branch reference in question. Here is a toy example illustrating this.

    # set things up
    $ mkdir test
    $ cd test
    $ git init
    Initialized empty Git repository in /Users/jubobs/Desktop/test/.git/
    
    # create a first commit
    $ touch README
    $ git add .
    $ git commit -m "add README"
    [master (root-commit) 85137ba] add README
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 README
    
    # create a second commit
    $ printf "foo\n" > README
    $ git commit -am "write 'foo' in README"
    [master 3948e84] write 'foo' in README
     1 file changed, 1 insertion(+)
    
    # inspect the log
    $ git log --graph --decorate --oneline --all
    * 3948e84 (HEAD, master) write 'foo' in README
    * 85137ba add README
    
    # check out the second commit (which detaches the HEAD)
    $ git checkout 3948e84
    Note: checking out '3948e84'.
    # (boilerplate stdout is omitted...)
    HEAD is now at 3948e84... write 'foo' in README
    
    # reset to the first commit (equivalent to 'git reset --hard 85137ba')
    $ git reset --hard HEAD^
    HEAD is now at 85137ba add README
    $ git log --graph --decorate --oneline --all
    * 3948e84 (master) write 'foo' in README
    * 85137ba (HEAD) add README
    

    请注意,git reset命令将HEAD移动到了初始提交,但 not 却没有移动master分支.第二个提交未被删除",因为它仍然可以从master到达.因此,它在git log的输出中列出.

    Note that the git reset command moved HEAD to the initial commit, but did not move the master branch whatsoever. The second commit is not "deleted", because it's still reachable from master; it is therefore listed in the output of git log.

    这篇关于如何删除当前HEAD之后的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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