How to undo “git commit --amend” done instead of “

2019-01-03 11:20发布

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.

(I have not yet pushed to any remote directories)

9条回答
Anthone
2楼-- · 2019-01-03 11:41

You can always split a commit, From the manual

  • Start an interactive rebase with git rebase -i commit^, where commit is the commit you want to split. In fact, any commit range will do, as long as it contains that commit.
  • Mark the commit you want to split with the action "edit".
  • When it comes to editing that commit, execute git reset HEAD^. The effect is that the HEAD is rewound by one, and the index follows suit. However, the working tree stays the same.
  • Now add the changes to the index that you want to have in the first commit. You can use git add (possibly interactively) or git-gui (or both) to do that.
  • Commit the now-current index with whatever commit message is appropriate now.
  • Repeat the last two steps until your working tree is clean.
  • Continue the rebase with git rebase --continue.
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-03 11:43

Almost 9 years late to this but didn't see this variation mentioned accomplishing the same thing (it's kind of a combination of a few of these, similar to to top answer (https://stackoverflow.com/a/1459264/4642530).

Search all detached heads on branch

git reflog show origin/BRANCH_NAME --date=relative

Then find the SHA1 hash

Reset to old SHA1

git reset --hard SHA1

Then push it back up.

git push origin BRANCH_NAME

Done.

This will revert you back to the old commit entirely.

(Including the date of the prior overwritten detached commit head)

查看更多
淡お忘
4楼-- · 2019-01-03 11:45

You can do below to undo your “git commit —amend”

  1. $ git reset --soft HEAD^
  2. $ git checkout files_from_old_commit_on_branch
  3. $ git pull origin your_branch_name

====================================

Now your changes are as per previous. So you are done with the undo for “git commit —amend” Now you can do “git push origin “, to push to branch

查看更多
叛逆
5楼-- · 2019-01-03 11:47
  1. Checkout to temporary branch with last commit

    git branch temp HEAD@{1}

  2. Reset last commit

    git reset temp

  3. Now, you'll have all files your commit as well as previous commit. Check status of all the files.

    git status

  4. Reset your commit files from git stage.

    git reset myfile1.js (so on)

  5. Reattach this commit

    git commit -C HEAD@{1}

  6. Add and commit your files to new commit.

查看更多
聊天终结者
6楼-- · 2019-01-03 11:50

Find your amended commits by:

git log --reflog

Note: You may add --patch to see the body of the commits for clarity. Same as git reflog.

then reset your HEAD to any previous commit at the point it was fine by:

git reset SHA1 --hard

Note: Replace SHA1 with your real commit hash. Also note that this command will lose any uncommitted changes, so you may stash them before. Alternatively use --soft instead.

Then cherry-pick the other commit that you need on top of it:

git cherry-pick SHA1
查看更多
我只想做你的唯一
7楼-- · 2019-01-03 11:53

Possibly worth noting that if you're still in your editor with the commit message, you can delete the commit message and it will abort the git commit --amend command.

查看更多
登录 后发表回答