Github is one of the most popular platforms when it comes to code collaboration and repository sharing. In layman terms, Github is basically a version control software that lets users track and take control of the distributed version and Source Code Management (SCM).
While generally everything works exactly how you’d expect it to, sometimes Github can run into issues as well. One such problem is when the .gitignore file stops working.
Also read: GitHub vs Git vs GitLab vs Bitbucket
What is .gitignore?
Github divides your entire repository in three different file types as follows.
- Tracked: These files get committed to the repository or are already staged.
- Untracked: These files aren’t yet committed or staged.
- Ignored: Github specifically ignores these files.
These ignored files can contain complied code, hidden system files, caches, IDE configuration files or any other build artefacts that aren’t necessary for the end-user to run the code in the repository.
Any files that you want Github to ignore the need to be specified in the .gitignore file. According to Github, there’s no specific command for this, and you have to add your files instead manually.
The file contains patterns matched against other files in your repository to determine which files should be ignored. Anything from a badly configured Git installation to encoding errors while saving the .gitignore file can cause it to stop working.
Also read: How to create a Git repository? How to connect it to GitHub?
3 ways to fix Gitignore not working issue?
Here are three ways using which you can fix Gitignore issues.
Check the file encoding
Your .gitignore file needs to have ANSI coding. If you’re using notepad, it saves files in UNICODE by default. This can lead to the file not working.
Step 1: Finish writing your .gitignore file and click on File followed by Save As…
Step 2: Make sure the Encoding dropdown is set to ANSI. You need to ensure that the Save as type dropdown has All Files (*.*) selected.
Test the file again with Git to see if it works now.
Also read: How to create a GitHub repository?
Check the file you’re ignoring
.gitignore will not ignore a file if it’s already present or is part of a repository. In other words, Git can only ignore untracked files.
Take a good look at your structure, and make sure you’re trying to ignore the file that isn’t already committed to your repository. If it is, remove the file from the repository and try again. This should fix the Gitignore not working issue.
Re-adding files to the repository
If the files you’re trying to ignore are already added to the repository, another thing you can do is remove everything from Git’s index and add everything back again.
Type the following commands in the Git terminal one by one and hit enter after each one.
git rm -r --cached
This command will unstage and remove all paths to your files from the git index.
git add .
This command will all the files back into the repository, this time ignoring the files specified in .gitignore.
git commit -m "This is the commit message"
Now commit all the files to the repository by using the command above.