The .gitignore File

Henrique Siebert Domareski
5 min readJul 20, 2020

--

When we work with Git, there are some files and directories that we do not want to send to the repository, and to make sure that those files will never be committed we can work with the .gitignore file.

A gitignore file specifies intentionally untracked files that Git should ignore.

A .gitignore file is a text file and each line contains a pattern for files or directors that we want to ignore. So we can create a file named .gitignore (make sure to remove the extension of the file) and inside of this file, we can add what we do not want to send to the repository. You can have more than one gitignore files, but generally, we have just one file and this file is added on the root of the folder of the project.

The structure of the .gitignore file

Each line on the gitignore file specifies a pattern, a blank line matches no files, so you can use blank lines to separate the patterns and make the file more readable.

Ignore a specific file

If you want to add a specific file to be ignored, you can add the name of the file and the extension, for example:

test.txt

Then when commit the code, the file ‘test.txt’ will be ignored.

Single Asterisk

The * matches anything except the ‘/’. For example, to add all .txt files to the gitignore file, you can add using this:

*.txt

This way all the files that ends with ‘.txt’ will be ignored.

Comments

To comment a line, it’s necessary to use the ‘# on the beginning of the line, for example, the first line is a comment:

# Common node modules locations
/node_modules
/wwwroot/node_modules

Ignore directory

To ignore an entity directory, add the name of the folder and add the ‘/’ in the end:

bin/
obj/

Negation

You can also use negation to specify a file that is set to be ignored but you do not want to ignore. Let’s suppose that you add to the ignore list, all .txt files, but you do not want to ignore the ‘test.txt’ file, then you need to use the ‘!’, for example:

*.txt
!test.txt

On this case, all .txt files will be ignored, excepted the ‘test.txt’.

Double Asterisk

You can use two consecutive asterisks (‘**’) to match any numbers of directories. For example:

  • ‘**/test’ matches file or directory ‘test’ anywhere.
  • ‘**/test/txt’ matches file or directory ‘txt’ anywhere that is directly under the directory ‘test’.
  • ‘test/**’ matches all files that are inside the directory ‘test’ — this is relative to the location where the .gitignore file is,
  • A slash followed by two asterisks, matches zero or more directories, for example: ‘test/**/project’ matches ‘test/project’, ‘test/x/project’, ‘test/a/b/c/project’ and so on.

What files should be ignored?

You can add to gitignore everything that it’s not necessary to be in the repository, and also the files that should not be in the repository, for example:

  • Log files
  • Files with sensitive data / API keys / Credentials
  • Files that are generated automatically like the bin folders in .NET projects, the node_modules in Angular projects, and others
  • External dependencies which can be downloaded from a package manager
  • Files that you only use locally
  • Any other filed that you do not want to send to the repository

How to create the .gitignore file

To create a gitignore file, you can create a text file and rename to ‘.gitignore’ (remember to remove the extension ‘.txt’), and inside of the file you can add manually all the configuration that you want to set, or you can also use a tool like gitignore.io, where you can select the IDE, the framework and the programming language that you are using, and gitignore.io it will automatically generate the configuration of the gitignore file. You can access the gitignore.io website clicking here.

On the gitignore.io website, you can add everything that you are using, for example: VisualStudio, DotNetCore, VisualStudioCode, Angular, and click in Create:

After click in ‘Create’ it will generate the code for your gitignore file. You need to copy the code and then edit the ‘.gitignore’ file that you created and paste the code inside this file and save:

How untrack files after the commit

You need to add the configuration on the gitignore file before you commit the files, otherwise, git will not ignore those files. If you forgot to add some configuration and already committed the files, you need to untrack them. For it, you need to fix your gitignore file adding the configuration that is missing and execute the remove command:

If is just one file, you can do it using the command:

git rm --cached FileName

If you need to untrack multiples files. you can execute this command:

git rm -r --cached .

Next step is to re-add all the files again:

git add .

And then commit:

git commit -m "Fixed .gitignore file"

Done. Now your repository is updated following the gitignore configuration.

Conclusion

It’s important that you have a gitignore file, because then you can configure what you do not want to send to the repository, and don’t need to worry about those files when you do your commits. This way you can keep your files with sensitive data save, and will avoid sending to the repository unnecessary files.

If you want to know more about gitignore, you can check here.

Thanks for reading!

--

--