4 Steps to safely remove files that you mistakenly pushed to Git?
Don't delete the Repository. Git offers a simple workaround.
2 min read
Have you pushed changes to Git or Github, only to realise later that you pushed unnecessary files? What would you do to reverse this action?
Have seen many inexperienced developers delete the repository completely. I used to do this too but I wasted a lot of time and lost some if not all of my commits.
Some developers use the delete feature on GitHub only to find the file when they push updates.
In this article, I will show you the safe way you can use to remove any files from a git repository without causing unnecessary damage.
The process is straightforward. And it works for Github, Bitbucket and Gitlab.
The git rm command removes files from the working tree and git index.
4 Steps to Remove files from git.
1a) Run the command below if you want to remove an individual file.
git rm --cached <filename>
1b) Run this one if you want to remove a directory/folder. Note the -r flag which represents recursively.
git rm -r --cached <directory>
2) After removing the files you should add the changes to the stage(git index)
git add .
3) Go ahead and commit but don't forget a concise message for these changes.
git commit -m "Appropriate message"
4) If your repository is hosted on a service, it's only professional to push the changes.
git push <repo-alias> <branch>
Note: Steps 1a and 1b are optional. Use the one which is appropriate for your scenario.
1) You shouldn't push node_modules, .DS_store, and .env to GitHub or related service.
2) In case you removed .env or any file that contains secret keys. Make sure to generate new keys and destroy the old ones. You never know how fast secret keys get in the wrong hands.
3) git "index" aka staging area is where you place files you want to commit to the git repository.
We've looked at how to remove files/directories from git and GitHub in 4 simple steps. We've also looked at some files which shouldn't be published to an online repository. This will save time and your job.
Let me know if this helps.
Are there other safe ways to remove files from a git/github repository?
Would you like to learn more about:-
💰Making Money Online
👨🏽💻👩🏾💻Getting a Remote Job
I regularly publish on Twitter. Hope you follow me @davidofug and Subscribe to my Newsletter here newsletter.davidofug.com