How to Gitignore (untrack) files already pushed to GitHub repository.
ADEMOLA OGUNMOKUN
Posted on December 29, 2020
Most developers usually initialize the folder they are working from as their GitHub repository. However, some files or/and directories may be present in that folder that you would not want to push to GitHub (files such as database, environment variables etc.)
Adding the file(s) or directories to gitignore seems to solve the issue. However, sometimes, you might have unknowingly pushed that file(s) to our GitHub repository failing or forgetting to gitignore the file(s) we did not want to push to the remote repository.
Once the file has been pushed, adding it to gitignore afterwards would not untrack the file from that commit. The file would still be present in the repository.
If you have that kind of issue, follow these simple five steps to solve the problem.
Step 1:
Git commit
Make sure the changes you have made on the projects have been added and committed, including the .gitignore file.
Do this by
git add .
git commit -m "initial commit"
Step 2:
Remove all the files in the repository.
You do that by:
git rm -r --cached .
rm is the remove command, adding -cached allow us to remove the files from the index. Our files are still present.
Step 3:
Add everything
The next step is to re-add/track our files. By doing this, the files in the . gitignore file will not be tracked.
This is done by:
git add .
Step 4:
Commit the changes.
Thus, let's commit our changes by
git commit -m "gitignore fix"
Step 5:
Push to GitHub
After we are through with everything, we still need to push it to our online GitHub repository by
git push
Thanks for reading.
Posted on December 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024