xpt
xpt

Reputation: 22994

Remove files from last git commit

This is not the same as Remove files from Git commit, because all the answers there doesn't work for this case. So here is the question,

Before git commit, we use git add. The problem is when we are doing git add, sometimes we forgot to check and only when doing git commit did we realized that those files should not be picked up by git add are being added and committed.

This is the reason why all the answers there doesn't work, because git reset HEAD^ or alike does not work for files newly being picked up, and have not git history beforehand. I learned it the hard way.

So, any solution for this case? Thx.

Upvotes: 1

Views: 297

Answers (4)

Twinkle
Twinkle

Reputation: 524

Use 'git rm --cached name_of_file ' Command. This will ignore the files even if they were added before with ' git add'.

Upvotes: 0

jholtrop
jholtrop

Reputation: 406

One way to do this after you've committed:

git rm --cached <files-you-didn't-want>
git commit --amend

Note that if you've already pushed the erroneous commit then you'll need to do a forced non-fast-forward push in order to remove the erroneous commit from a remote.

Upvotes: 2

user5038306
user5038306

Reputation:

git commit --amend when you commit too early and possibly forget to add some files, or you mess up your commit

Upvotes: 1

lichtscheu
lichtscheu

Reputation: 1

What about git reset <file>? Or just git reset to remove all files from index from the last git add

Upvotes: 0

Related Questions