nickponline
nickponline

Reputation: 25914

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

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.

Upvotes: 12

Views: 12476

Answers (5)

Paul Oliver
Paul Oliver

Reputation: 7681

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*

Upvotes: 21

William Seiti Mizuta
William Seiti Mizuta

Reputation: 7995

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.

Upvotes: 0

Kristofor Carle
Kristofor Carle

Reputation: 1435

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.

Upvotes: 1

Thomas
Thomas

Reputation: 182000

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.

Upvotes: 1

hd1
hd1

Reputation: 34677

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.

Upvotes: 1

Related Questions