1

Git Newbie here,

How do I remove all traces of a file from my Git commits - e.g. delete it everywhere in every branch, both locally and remote.

Most of the articles and Stack Overflow stuff I've seen states that some variation of this is the way to go:

 git filter-branch --tree-filter 'rm -f file_to_remove.py' HEAD

or

 git filter-branch --tree-filter 'rm -f file_to_remove.py' ..HEAD

or

 git filter-branch --force --tree-filter 'rm -f file_to_remove.py' -- --all

etc...

But, I can't seem to get any to work.

For one, I keep getting an error like this one or another similar error for other branches.

WARNING: Ref 'refs/remotes/origin/master' is unchanged" 

I ran the last one and it went through all the revisions and "looked" like it was doing something, but then I executed....

git show <<REVISION_HASH_NUM>>:file_to_remove.py 

Which shows that the file is still present in the commit history. Likewise, my Git commit history for that file is still present it TFS.

Also, there are 247 commits and 16+ other developers that use this repo. I can't nuke the Master branch.

P.S. Please don't refer me other Stack Overflow posts. I've already looked at the majority of them. Also, please don't say to rephrase my question. If I knew enough to ask the question correctly, then I'd know enough to fix it myself.

Thanks in advance!

ZerO_Cool
  • 21
  • 1
  • 4
  • Welcome! As the existing answers have covered, you cannot rewrite the remote history without causing a ton of problems - although you could probably figure something out if you got everyone on the team to cooperate with you. However, if you can provide some context around why you want to do this, someone may come up with another way to solve your problem. My initial questions are: 1) What is this file and why does it need to be removed from the history? 2) When was this initially commited, and how many branches have been impacted? – Vlad274 Sep 20 '18 at 21:23

2 Answers2

2

Fist: A much simpler tool than git filter-branch is the BFG Repo-Cleaner - it is already mentioned in the manual of git filter-branch. This tool makes deleting objects much easier.

Second: Your test

git show <<REVISION_HASH_NUM>>:file_to_remove.py 

is flawed: Even after filtering the contents of the existing commits are not changed. Instead filter-branch creates new commits and hence new hashes. Also the old commits are NOT deleted immediately - they are just unreachable from the rewritten branches and tags. After a while they are garbage collected.

So a simple test is to make a clone of your repo using the option --no-hardlinks and check the history there.

Third:

Also, there are 247 commits and 16+ other developers that use this repo. I can't nuke the Master branch.

No chance - the content of a commmit is tied to the hash and vice versa. If you change the content you change the hash - you are "rewriting the history" (Google search term). These 16+ developers will hate you :-)

A.H.
  • 63,967
  • 15
  • 92
  • 126
0

The problem with those commands you've listed in your question is that, as you probably know, they all operate on local branches. You can rewrite your local git repository to remove the file in question, but that leaves the remote repository unchanged.

And here lies your problem: how do you edit the remote repository in a way that doesn't mess up the other users who have pulled from that remote repository.

Unfortunately, that can't be done. Each commit in a git repository builds upon previous commits and depends upon the contents of that commit. So rewriting even one commit with a tiny change somewhere in the history means changes to all the commits that come after it.

This wouldn't be a huge problem, except that everyone who has cloned that remote repository has their own copy of that repository, and if you change a series of commits on the remote then what they have will no longer match what is on the remote. I'm sure someone else can give a more technical description of what happens in a case like this, but suffice it to say this will break every one of the the other 16 developers' local copies.

Kryten
  • 15,230
  • 6
  • 45
  • 68