0 votes
1 view
in Devops and Agile by (20k points)

I accidentally amended my previous commit. The commit should have been separate to keep a 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.

(I have not yet pushed to any remote directories)

1 Answer

0 votes
by (28.4k points)

All you need to do is to create a new commit with the same details as your current HEAD commit, but with the parent as the previous version of HEAD. The command, 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.

# First move the current head so that it's pointing at the old commit

# Now, leave the index intact for redoing the commit.

# The command, [email protected]{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 [email protected]{1}

# Now you need to commit the current tree using the commit details of the previous

# HEAD commit. Remember that [email protected]{1} is pointing somewhere different from the previous command. It's now pointing at the erroneously amended commit.

git commit -C [email protected]{1}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...