Hi π. Welcome to this guide on removing specific files from your Git repository. Whether you're a seasoned Git user or just starting out, there will come a time when you need to clean up your repository and remove unwanted files. Perhaps you've accidentally committed sensitive information, or maybe you just need to clean up old, unnecessary files. Whatever the reason, removing files from your Git repository can seem like a daunting task, but it's actually quite simple once you know how. In this post, we'll take a step-by-step approach to explain how to safely remove specific files from your Git repository, and what to do if you've already pushed the files to a remote repository. Let's get started!
Use this command to remove file only from the git repository
git rm --cached pathtothefilewhichyouwanttoremove
After running this command you should get output like this
rm 'pathtofilewhichyouwanttoremove'
If you run git status
at this point you will get output like this
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: js/one.js
Untracked files:
(use "git add <file>..." to include in what will be committed)
js/one.js
Since I have deleted one.js file which is present in js folder, I got this as my output. Depending upon the path and filename of the file which you have deleted you will get similar output.
If you wish to keep the file at the same location as before you may add it in the gitignore
file. In this case I am adding js/one.js
to my git ignore file. You should add relative path to the file which you don't want to appear in your git repository.
js/one.js
If you wish to delete that specific file from your machine as well you this command π instead
git rm pathtothefilewhichyouwanttoremove
Now you may commit these changes and push to your remote repository. After finishing the above mentioned steps if you head over to your remote repository the file which you have removed will not be present. If you have found this blog post useful leave a like π.
Struck somewhere π₯Ά, comment down below so that I can help you out.
Happy Hacking
Top comments (0)