20141119 remove a file and its history from a git repo - plembo/onemoretech GitHub Wiki

title: Remove a file and its history from a git repo link: https://onemoretech.wordpress.com/2014/11/19/remove-a-file-and-its-history-from-a-git-repo/ author: phil2nc description: post_id: 8847 created: 2014/11/19 17:28:42 created_gmt: 2014/11/19 22:28:42 comment_status: closed post_name: remove-a-file-and-its-history-from-a-git-repo status: publish post_type: post

Remove a file and its history from a git repo

Another do-over technique for when you need it. Git is hard. But not impossible, although given a limited amount of time to figure things out it might as well be sometimes. Here is how I permanently removed a file and its history from a git repository. First and foremost, always do a git pull (or a fetch and merge) from the remote repo to your local copy. Most problems you'll encounter in working with remote repos happen because things have gotten out of sync. Don't let that happen. Next, I used the filter-branch method to remove the file and have git leave out all mention of it when rewriting the repository's history:

[me@mine testutils]$ git filter-branch --tree-filter \
'rm -rf tools/xmpp_test.pl' HEAD

Producing this response:

Rewrite ddca5e902b62a0658666785786dc21adc169e701 (479/479)
Ref 'refs/heads/master' was rewritten

Then I forced the change upstream:

[me@mine testutils]$ git push origin --force --all

That resulted in:

Counting objects: 5449, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5288/5288), done.
Writing objects: 100% (5326/5326), 17.30 MiB | 111.00 KiB/s, done.
Total 5326 (delta 895), reused 4050 (delta 0)
To https://[email protected]/myteam/testutils.git
 + ddca5e9...3f2abca master -> master (forced update)

Finally, never forget your tags:

[me@mine testutils]$ git push origin --force --tags

Errors of various sorts can (and probably will) show up, mostly having to do with your local repo being out of sync with the remote. That's why it's so important to do a pull (or fetch and merge) before you begin. If you don't you'll spend some time clawing back steps and possibly mucking things up even worse. References: Rewriting History in Scott Chacon and Ben Straub (2014), Pro Git. APress. Steve Ostermiller (2014), Removing and purging files from git history.

Copyright 2004-2019 Phil Lembo