如何删除当前HEAD之后的提交? [英] How can I delete commits that are after the current HEAD?
问题描述
在我的Git存储库中,我连续创建了三个提交:commit1
,commit2
和commit3
.
In my Git repository, I created three commits in a row: commit1
, commit2
, and commit3
.
然后我意识到自己搞砸了commit2
和commit3
,并决定回到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
中.如何删除commit2
和commit3
?
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]
提交C2
和C3
仍出现在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...
-
通过运行将
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]
由于提交C2
和C3
现在变得不可访问(即已删除"),因此它们没有显示在最后一张图上.
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 outcommit1
, 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屋!