How many times you commited files on git in an entry phase of a project and only after a while you noticed that the files are useless? (For me: at least 3–4 times every project — damn android builds files on react-native)
Even if you update your .gitignore, the files will continue to be tracked and inside your repository.
So how you can remove them? Here 3 simple steps to do it:
1. Be sure to have committed all changes
You shouldn’t have any uncommitted files in your working directory. To be sure about that, use
git status
If it returns
nothing to commit, working tree clean
You are ready to go! 🚀 If not, add and commit your files or stash them.
Step 2: Remove everything from the repository
To clear your repo, use:
git rm -r --cached .
- rm is the remove command of git
- -r means recursive, so it will remove also inside folders
- –cached meas that will only remove files from the git index, not from your current directory; your files are safe ✨
.
indicates to remove all files. If you want to untrack only one file, add the path instead of.
, eg: git rm — cached .env
Step 3: Add everything again
That’s the easy part:
git add .
Step 4: Commit and party
Now commit your changes
git commit -m "removed useless files"
And now your repository follows your .gitignore rules! It’s time to party!