Hossein
Hossein

Reputation: 41921

Removing a file from a certain commit (back in history) in git

Imagine that you have committed several changes (in separate commits). For example, we have 3 commits: A , B and C.

Now, you have found out that in the commit B you have added a certain file (including some changes) that you shouldn't have committed.

How would you remove that file in that commit?

An example would be great.

Thanks

Upvotes: 0

Views: 3031

Answers (2)

Raber
Raber

Reputation: 2080

you could also do

git rebase --interactive A

and edit commit B not to include the file in question

but same warning applies and if you made changes to file in any commit after B these will fail to apply

Upvotes: 1

Karthik Bose
Karthik Bose

Reputation: 36084

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

This command will remove a file from all commits [from history].

Warning: If you've already shared your work with others, it's not recommended to use this command. Please make sure you understood the full implications before using this command.

Upvotes: 1

Related Questions