Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I undo a bad commit --amend? [duplicate]

Tags:

git

This may sound weird, but I just amended a commit to test my EDITOR environment variable. I wasn't aware that the commit would be amended even though I didn't edit the commit message or any files. The commit in question was merged from the develop branch into the master branch and tagged. After the amend though, the branch structure went all wonky:

Screen shot

I don't know how bad this is or how many issues it will cause in the future, but I don't dare touch the repository until I know more. I'd really like to just rewind the repository back to before I amended the "Added docs." commit. The develop branch should be pointing to the "Added docs." commit that's merged into the master branch, and the current "Added docs." commit should not exist.

Is that possible?


Related:

  • How to undo "git commit --amend" done instead of "git commit"
like image 460
Hubro Avatar asked Jul 09 '13 23:07

Hubro


People also ask

How do I undo a bad commit?

The git reset and revert commands are commonly confused, but they apply to distinct use cases. To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, use git reset.

How do you cancel a commit -- amend?

If you delete the commit message (no need to delete the ones starting with # ) you will abort the git commit --amend command. You will get output like this: Aborting commit due to empty commit message. Save this answer.

How do I fix a bad commit in git?

Use git reset if the commit is not pushed yet and you don't want to introduce a bad commit to the remote branch. Use git revert to revert a merge commit that has already pushed to the remote branch. Use git log to review the commit history. If you prefer, you can also create a new commit with the fix.


1 Answers

You should be able to do a

git reflog

Then find out the commit you were in before your “commit --amend”. Usually this would be HEAD@{1}. Now do a

git reset --soft HEAD@{1}
like image 134
Debajit Avatar answered Sep 28 '22 00:09

Debajit