How do I remove all files of a particular extension from my git repository (history only, only files not in the latest commit)?

1.1k views Asked by At

Question is mostly explained in the title. One of my repository contains a lot of binary files that never really change. However, I changed the format I was using for a new file extension/storage format. Now I've 1000, or so, files sitting in my git history that have no use for (since the data itself hasn't changed, just the storage format). Since they are all of the same extension, I figured it should be an easy way to purge them all, but I have no idea how.

1

There are 1 answers

2
Arkadiusz Drabczyk On

I'm still not sure I understood your question correctly. You shouldn't remove nothing from the git history. Even if it's technically possible it's usually not a good practice especially if you already pushed your branch to the remote repository and shared it with others as it modifies SHA-1 of commits that other users might already pulled. All you should do right now is just to remove all files with a given extension, .x in this example:

$ git rm "*.x"
rm 'a/a.x'
rm 'b/c/d/e/g/another.x'
rm 'c.x'

It will remove all tracked files that end with *.x and add them to the staging area:

$ git diff --cached
diff --git a/a/a.x b/a/a.x
deleted file mode 100644
index e69de29..0000000
diff --git a/b/c/d/e/g/another.x b/b/c/d/e/g/another.x
deleted file mode 100644
index e69de29..0000000
diff --git a/c.x b/c.x
deleted file mode 100644
index e69de29..0000000

You will just have to commit them.

Notice you have to put *.x in double quotes as your shell will expand * before running git command.