Cleaning up git github repository without deleting .git directory
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.
Issues:
- 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
Attention:
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
Solutions:
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!
Optional:
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.
Step 1:
Commit all your changes, including your .gitignore file.
git add .
git commit -m “messages”
Step 2:
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
- The
.
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>
example:
git rm --cached foo.txt
If you want un-track a single directory do this:
git rm --cached -r <directory>
example:
git rm --cached -r MyFolderName
Step 3:
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!
Warning!
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/config
). Deleting .git/
always causes huge issues when you have git submodules.
I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.
https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs