Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completely removing a folder from git [duplicate]

Tags:

git

To remove a folder named build from git I use:

git rm --cached -r build

As I learned from this answer.

However, IIUC, the directory remains in the git's database.

I tested the matter:

  1. Initialised a new git repository.
  2. Added a new directory, named build, with a large file (7MB) and committed.
  3. performed:

    git rm --cached -r build
    

The size of the .git directory remained 7mb which means the untracking and deletion did not change the git history, presumptuously as it should.

What is the way to completely clean up a wrongly committed directory which was committed a few commits back?

In my case as I was only on my third commit with little concern to the history of the project, I deleted the .git folder and started over but I am wondering whether there is a better way.

like image 641
Guy Avatar asked May 24 '26 07:05

Guy


1 Answers

The answer is the same as for removing a single file from all of history (with all the same caveats), except that instead of directing the filter to remove one file, you direct it to remove all files starting with that path, e.g., git rm --cached -rf path/to/directory as your --index-filter with git filter-branch. See, e.g., this answer to How to remove/delete a large file from commit history in Git repository? See also Completely remove file from all Git repository commit history.

Note that after using git filter-branch (which you should do on a clone), the simplest way to shrink the repository—which now has up to twice as many commits, since filters cannot alter existing commits, only copy them to corrected new ones—is to clone the filtered clone.

like image 73
torek Avatar answered May 25 '26 21:05

torek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!