Let’s write .gitignore in a better way

Mehmet Ali Baykara
2 min readMay 1, 2020

--

Git is one of the most used version control system(vsc). A version control system records changes to a file or set of files over time so that you can recall specific versions later. For more information see.

https://git-scm.com/images/logos/downloads/Git-Logo-2Color.png

I assume that you’ve already base knowledge about Git. I am gonna concentrate on .gitignorefile. A .gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git will not be affected. As we notice from the official description, the file or folder that should not be pushed to the repository has to be in .gitignore file. Actually, this is a kind of Blacklist which is the most known way of writing .gitignore file. There is also another way of writing .gitignore file → Whitelist.

What is a .gitignore as whitelist? Probably you’ve already guessed it is opposite of blacklist that’s mean, you will add explicitly exclamation mark (!) then a file name, extension or folder name that has to be in your repository.

The main intention to use the whitelist as a developer you have to always explicitly add file in .gitignore which has to be pushed. Developers especially the beginning of their carriers faced kind of issues such as pushing undesirable files to the repository. Via whitelist you don’t have to worry after hitting git add command. This is one of the best practices of git usage. Let’s see the example below to make clear.

My project structure looks as below:

$ tree my_project/
my_project/
├── src
│ ├── main.cpp
│ └── play.cpp
├── test
│ ├── playTest.cpp
│ └── testAll.cpp
├── configs
│ ├── deployment.conf
│ └── analyse.conf
└── Dockerfile
├── .gitignore
├── README.md
├── LICENCE.md
└── profile.html

In this case, let’s say you want to push all files except profile.html file. Then the .gitignore file as white list it should be written as:

*
!.gitignore
!README.md
!LICENSE.md
!Dockerfile
!src
!test
!src/*.cpp
!test/*.cpp
!config
!config/*.conf

The syntax may confuse at the beginning but is actually quite straightforward. The exclamation mark (!) tells git do not ignore those files. The rest of the files will be ignored. In our example, profile.html file will be ignored.

Now you know how to write a.gitignoreas a whitelist. No more undesirable files or folders in your repository. Happy coding!

For any critic or question, please write me on twitter.

Resources
* https://git-scm.com/images/logos/downloads/Git-Logo-2Color.png
* https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
* https://git-scm.com/docs/gitignore
* https://kapitel26.github.io/git/2014/03/06/gitignore-positivliste.rb.html

--

--

Mehmet Ali Baykara
Mehmet Ali Baykara

Written by Mehmet Ali Baykara

Don't buy me a coffee! Just Say "Hi"

No responses yet