My initial commit contained some log files. I\'ve added *log
to my .gitignore
, and now I want to remove the log files from my repository.
Above answers didn't work for me. I used filter-branch
to remove all committed files.
Remove a file from a git repository with:
git filter-branch --tree-filter 'rm file'
Remove a folder from a git repository with:
git filter-branch --tree-filter 'rm -rf directory'
This removes the directory or file from all the commits.
You can specify a commit by using:
git filter-branch --tree-filter 'rm -rf directory' HEAD
Or an range:
git filter-branch --tree-filter 'rm -rf vendor/gems' t49dse..HEAD
To push everything to remote, you can do:
git push origin master --force
From the man file:
When
--cached
is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.
So, for a single file:
git rm --cached mylogfile.log
and for a single directory:
git rm --cached -r mydirectory
Note : this does not deal with history for sensitive information.
This process definitely takes some undertanding of what is going on with git. Over time, having gained that, I've learned to do processes such as:
.gitignore
to ignore them - in many cases such as yours, the parent directory, e.g. log/
will be the regex to use..gitignore
file change (not sure if push needed mind you, no harm if done).git remove --cached some_dir/
git add .
git commit -m"removal"
Also, if you have commited sensitive data (e.g. a file containing passwords), you should completely delete it from the history of the repository. Here's a guide explaining how to do that: http://help.github.com/remove-sensitive-data/
A more generic solution:
Edit .gitignore
file.
echo mylogfile.log >> .gitignore
Remove all items from index.
git rm -r -f --cached .
Rebuild index.
git add .
Make new commit
git commit -m "Removed mylogfile.log"
This depends on what you mean by 'remove' from git. :)
You can unstage a file using git rm --cached see for more details. When you unstage something, it means that it is no longer tracked, but this does not remove the file from previous commits.
If you want to do more than unstage the file, for example to remove sensitive data from all previous commits you will want to look into filtering the branch using tools like the BFG Repo-Cleaner.