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.
Top comments (2)
There are also ways to clean the history from files i.e. with credentials
docs.github.com/en/free-pro-team@l...
Cool. I will definitely check it out.