Learning How to Git: Ignoring Files and Folders using GitIgnore

Haydar Ali Ismail
3 min readJan 21, 2020

--

Sometimes, our project not only consists the code that we created. There are some files that are generated by our IDE or our framework that aren’t actually needed. These files or folders sometimes also may generate errors when copied to other PC, so that’s why we need to ignore those unnecessary files or folders.

Introducing GitIgnore

GitIgnore file is a file that specifying the files or folders that we want to ignore. There are several ways to specifying those

The first one is specifying by the specific filename. Here is an example, let’s say we want to ignore a file called readme.txt, then we just need to write readme.txt in the .gitignore file.

The second one we can also write the name of the extension. For example, we are going to ignore all .txt files, then write *.txt.

There is also a method to ignore a whole folder. Let’s say we want to ignore folder named test. Then we can just write test/ in the file.

There are other ways to define the ignore files. But, as a starter, you will probably only use those 3 ways.

Hands On

Let’s try to create the .gitignore file. I’m going to continue my repository. Let’s define our rules of the files that need to be keep. It is just an example.

  1. For readme in Github, we will only need a README.md file. Let’s ignore if there is an README.txt file.
  2. We also don’t need a folder called output.
  3. Because there will be no Java files added to the repository, let’s ignore all of Java files (.java).

You can check the rules based on the previous section to convert those rule into a .gitignore file. You might get a similar file like this.

Save those file as .gitignore file and put it into the root folder of the Git project. Now, if you try to add those files to the Git repository, it will be ignored. Because we already define the rule for files that going to be ignored by the Git.

Notice that I try to add README.txt, but it is not listed when trying to check with `git status`.

One thing to note, if the files or folders already added to the Git project before we add it to the .gitignore file, then those files or folders will still available in the Git project. Because we try to ignore those files or folders after we already add those to the Git tracking system.

Wrap Up

Today, we have been talked about ignoring files and folders. Ignoring files and folders is important because not all of those files are needed when we try to move those project to other devices. If you want to learn more about how to specify those ignore files and folders, you can check links on references. Next up, we are going to create a whole .gitignore files that are specific to each project you are currently working on. Thank you for reading.

References

--

--

Haydar Ali Ismail
Haydar Ali Ismail

Written by Haydar Ali Ismail

Half Data Engineer, Half Software Engineer

No responses yet