I need to programmatically delete all versions of a particular file from git (including the commit messages from when the files was updated). The versions could be scattered throughout the list of commits.
Example: if there are 10 total commits, and the file in question was updated at commit 3, 4, and 7. I need to delete the file from commit 3 onwards and delete the commit messages at commit 3, 4, and 7.
If the commit history is as follows, X
marks the commits which I want to delete from commit history along with the file.
O - O - X - X - O - O - X - O - O - O
The solution which worked for me was git command: "git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch foo.txt' --msg-filter 'sed "s/foo.txt/DELETED/g"'
This command has two parts:
index-filter 'git rm --cached --ignore-unmatch foo.txt'
- This
deletes all versions of the file named foo.txt msg-filter 'sed "s/foo.txt/DELETED/g"'
- This alters the commit messages which contain the filename foo.txt to specify that the file has been deletedThis command removes foo.txt and alters the commit messages which contain foo.txt to specify that the file has been deleted.
Thanks to @unutbu for pointing me to the required filter-branch options.