1

I had some unstaged files. I added them with git add. Then by mistake (I thought I had created another branch but didn't) I committed them.

So now I have one commit more in my original_branch.

How do I uncommit this last commit?

Take into account that :

  • I don't want to lose my files
  • My objective is to have these files as I had them in the beginning, uncommited and ready to be committed in a different branch
KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

2 Answers2

1

Assuming you haven't pushed yet upstream, you can:

git reset --soft HEAD~1

This will "remove" the last commit but will preserve the files ready-to-be-committed.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • 1
    Any reason for downvoting? I use this method regularly and it never lets me down :) Seriously, if the downvoter has a better solution please post it here – Mark Bramnik Dec 01 '20 at 07:47
1

You can simply undo the last commit:

git reset HEAD^

This will keep the accidentally-commited files in tact.

Alternatively, if you have other changes in the last commit you still want to retain but at the same time remove the accidentally-commited files, try:

git reset HEAD^ -- file1 file2 file3...
git commit --amend --no-edit

(where file1 file2 file3... are the files you want to remove from the last commit).

costaparas
  • 5,047
  • 11
  • 16
  • 26
  • 1
    I don't know why someone gave you a minus. I applied your solution that and it worked. it would be nice that any minus point came with an explanation – KansaiRobot Dec 01 '20 at 08:07