How to Add a .gitignore File to an Existing Repository
This article outlines the steps to apply a .gitignore
file to an existing repository in Git. The .gitignore
file allows us to dictate what Git will not track in our repository.
If you have an existing Git repo with dozens of tracked files, you can apply the .gitignore
file by following these simple steps.
Add a .gitignore
File to an Existing Repository
We must commit all pending changes in our repository to add a .gitignore
file to an existing repo. Once done, we can proceed to the second step.
Create and add the .gitignore
file to your local repository. We will then have to remove all files from the index by running the command below.
$ git rm -r --cached .
Note the .
at the end. It instructs Git to remove all the files in our local repository from the index.
If you only have one file you want to include in the .gitignore
file, you can feed in the filename, as shown below.
$ git rm --cached filename
Make sure you have added your .gitignore
file to the repo and run the command below.
$ git add .
This will add all our files to the index, including the .gitignore
file. We can then commit the changes for the file to take effect.
$ git commit -m "Gitignore added"
This file will instruct Git to stop tracking files in the .gitignore
file. It is an easy way to untrack large numbers of files.
We can push the changes to the remote repository using the git push
command.
In conclusion, Git allows you to add a .gitignore
file to an existing repository to stop tracking certain files. Always commit your changes before proceeding with the git add .
step.
Failure to which you will lose all your file changes. Be cautious when pushing and pulling from this repository.
John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.
LinkedIn