Ignore files
It won’t be necessary to keep track of all the files in a project folder via Git. For example, it is not a good idea to track a node_modules
(see later) or uploads
folder in your repository. Also hidden system files, such as .DS_Store
are of no use in a repository.
Let us work with an example. Create a dummy project like this:
If everything went well, you should have a file structure like this:
We will commit everything, including notme
, “by accident” to set it straight afterwards
Delete files
You have now added the entire project, including the notme
, to your git repository.
Whoops, we will now correct this error:
The —cached option causes the file to be deleted from the repository index, but remains in your file system.
A git status now gives the following result:
Add commit this deletes by using the flag -A in the add command:
Delete files & folders from the commit history
We just created a commit removing the notme
folder. However, there is still a commit where these notme
are present, this way our repo can become unnecessary large. Or if we committed sensitive data like passwords and such, this can become a problem. If you want to remove folders or files from the entire history of your project, you have to go one step further.
You can edit the git history using the git filter-branch
command. To clear our notme
folder, let’s do the following:
The folder will be deleted on your file system and in the history. But this workflow isn’t ideal. We could easely add that directory again. It’s impossible to keep track of all the files you want or don’t want to track yourself. We will fix this by using a .gitignore
file.
.gitignore
We will now specify which files we don’t want to track in the future. This can be done with a .gitignore
file. This is a text file in your repository that specifies which files and directories are allowed to be ignored by git.
Create a new file called .gitignore
in the root of your repository. Give this the following content:
This will cause the .DS_Store
file and the notme
folder (& all its subfolders) to be ignored in the future.
Add and commit this file to include it in your repository.
In practice, you will create such a .gitignore
file as one of your first files. This way you avoid “contaminating” your repository with unnecessary files, and you avoid drastic actions such as deleting folders & files from the history.
A list of useful .gitignore files can be found here: https://github.com/github/gitignore