Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

git - 如何撤消“git commit --amend”而不是“git commit”(How to undo “git commit --amend” done instead of “git commit”)

I accidentally amended my previous commit.

(我不小心修改了我以前的提交。)

The commit should have been separate to keep history of the changes I made to a particular file.

(提交应该是独立的,以保留我对特定文件所做更改的历史记录。)

Is there a way to undo that last commit?

(有没有办法撤消最后一次提交?)

If I do something like git reset --hard HEAD^ , the first commit also is undone.

(如果我执行git reset --hard HEAD^ ,第一次提交也会撤消。)

(I have not yet pushed to any remote directories)

((我还没有推到任何远程目录))

  ask by Jesper R?nn-Jensen translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you need to do is to create a new commit with the same details as the current HEAD commit, but with the parent as the previous version of HEAD .

(您需要做的是创建一个新提交,其具有与当前HEAD提交相同的详细信息,但父级作为HEAD的先前版本。)

git reset --soft will move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.

(git reset --soft将移动分支指针,以便下一次提交发生在当前分支头现在的不同提交之上。)

# Move the current head so that it's pointing at the old commit
# Leave the index intact for redoing the commit.
# HEAD@{1} gives you "the commit that HEAD pointed at before 
# it was moved to where it currently points at". Note that this is
# different from HEAD~1, which gives you "the commit that is the
# parent node of the commit that HEAD is currently pointing to."
git reset --soft HEAD@{1}

# commit the current tree using the commit details of the previous
# HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the
# previous command. It's now pointing at the erroneously amended commit.)
git commit -C HEAD@{1}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...