This article contains a general solution for the following issues. Why I insist on not delete the .git directory for the following kind of purposes? answer: read the Warning! section at the end of this article.
- Un-track files already added to git repository based on the new updated .gitignore
- Removing files from git repository without deleting it from the local repository (disk)
- Applying .gitignore to committed files in git Github
- Reducing git repository size
- Clearing Github history
- Making the current commit the only (initial) commit in a git GitHub repository
- Deleting all commit history in Github
- You have already added/committed some files to your git repository and you then add some of them to your .gitignore. But these files will still be present in your repository index and want to get rid of them.
- Ignoring files that have already been committed to the repo.
- Removing files from git after adding/updating .gitignore
- Ignoring a directory from a git repo after it’s been added.
- git rm a file without deleting it from disk
- Removing all git histories
Before proceeding anything:
- Make sure your .gitignore file is up-to-date and contains all the correct patterns you want to ignore.
- Use gitignore.io service to create useful .gitignore files for your project
1- Solution for both Remote repository and Local :
Attention! This solution will not keep your old commit history around since it will delete the master branch! and delivers you a new master branch
git checkout --orphan newBranch
git add -A # Add all files and commit them
git commit -am "Clean Repo"
git branch -D master # Deletes the master branch
git branch -m master # Rename the current branch to master
git push -f origin master # Force push master branch to github
That’s it! It’s done and you have a clean history and repository!
At the end to cleanup unnecessary files and optimize the local repository do this:
git gc --aggressive --prune=all # remove the old files
Read more about: git gc command here
2- Solution for local :
If you don’t have a remote repository and all are in local (disk) you can simply.
Commit all your changes, including your .gitignore file.
git add .
git commit -m “messages”
Remove everything from the repository or un-track all files in your git repository.
$ git rm -r --cached .
- rm is for remove
- -r allow recursive removal
- - -cached will only remove files from the index. Your files will still be in disk
.indicates that all files in the current directory will be untracked.
- If you want un-track a specific single file do this:
git rm --cached <file>
git rm --cached foo.txt
If you want un-track a single directory do this:
git rm --cached -r <directory>
git rm --cached -r MyFolderName
Re add everything except those that match rules in your
.gitignore and then commit them.
$ git add .
$ git commit -m "Clean up ignored files"
Then your repository is clean!
If you follow the brute-force approach:
rm -rf .git
And want to reconstruct the git repo with only the current content and finally do:
git push -u --force origin master
This might bring some problems! It also removes the configuration of the git repository (
.git/ always causes huge issues when you have git submodules.