Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does git remove files from version control after they are added to .gitignore?

Tags:

git

I've been accidentally including my Python virtual environment directory venv in all my git commit and push activity.

I've just added venv to my .gitignore. But my .git folder is still massive (I'm assuming because of the previous commits and tracking of venv).

How can I get git to totally forget about tracking venv and get my .git folder down to a reasonable size again as it's making Heroku pushes a nightmare.

like image 522
nickponline Avatar asked Jan 18 '13 23:01

nickponline


5 Answers

Try deleting the files from your git repository:

git rm --cached venv*

Rich Stone mentions below, that if venv is a folder, you should use the recursive switch, -r:

git rm --cached -r venv*
like image 169
Paul Oliver Avatar answered Oct 09 '22 06:10

Paul Oliver


Try git gc, according to the manual this triggers an automatic garbage collection. If this doesn't help, leave a comment here and I'll investigate further options for you.

like image 41
hd1 Avatar answered Oct 09 '22 05:10

hd1


No, it does not. .gitignore only ensures that ignored files aren't accidentally added, but it does not delete them from history, nor from HEAD, nor even from the index if they've already been added.

To permanently delete large files from git history requires history rewriting, which is a somewhat painful operation. In particular, it breaks pushes and pulls from other repositories that have the "old" version of the history. But it can be done; see e.g. this question or this one for details.

like image 25
Thomas Avatar answered Oct 09 '22 06:10

Thomas


See this thread: How to make Git "forget" about a file that was tracked but is now in .gitignore?

It is easy to get the files out of the current commit, but removing them from the history entirely to get your file size down is a mess, see How to remove/delete a large file from commit history in Git repository? or https://help.github.com/articles/remove-sensitive-data

It might be faster to just export your latest code to a new git repo and start over and keep the old one for an archive.

like image 28
Kristofor Carle Avatar answered Oct 09 '22 05:10

Kristofor Carle


Putting a file in .gitignore doesn't remove the file from the repository.

You need to use git filter-brach to completely remove a file or directory from the repository. To do this, use the following commands:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch file/or/dir/with/complete/path' --prune-empty --all

WIth this, you will change the repository history to complete remove file/or/dir/with/complete/path from all commits. Remember that you will need to perform a git push -f because you changed the history.

like image 1
William Seiti Mizuta Avatar answered Oct 09 '22 04:10

William Seiti Mizuta