简体   繁体   中英

delete files from git using gitpython

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
  1. Is it even possible?
  2. If yes, what's the easiest way to do it programmatically? I know that GitPython doesn't have any builtin functionality for this, but it does allow running of git commands on the shell using python.

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:

  1. index-filter 'git rm --cached --ignore-unmatch foo.txt' - This deletes all versions of the file named foo.txt
  2. 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 deleted

This 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM