how to remove a renamed file from last git commit?

34 Views Asked by At

I know how to

but what I want is to exclude the renamed file from last git commit, but retain anything else (as git mv get into my commit unintentionally).

How can I do that?

(Seems I can only revert everything or not at all...)

1

There are 1 best solutions below

0
LeGEC On BEST ANSWER

You can search the internet for "git how can I change my latest commit content", you will find documentation pages such as :


One generic way to "change the latest commit" is to edit files and git add them as you want, then run git commit --amend.

This works for any kind of modifications: the commit will simply contain the new content you git added.

For example:

  • to "undo" a git mv old.txt new.txt:
git mv new.txt old.txt
git commit --amend
  • to still remove old.txt but not add new.txt in the commit:
git rm --cached new.txt  # --cached will make git keep the file on disk
git commit --amend
  • to edit a typo:
# edit file foo.txt ...
git add foo.txt
git commit --amend

etc ...