When developing projects sometimes, it may only consist of the code that we created while in some cases it has codes 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 especially when pushing our code to GitHub

What is GitIgnore?

GitIgnore is a file that specifies the files or folders that we want to ignore most especially when pushing your code to GitHub/ gitlab and any other collaboration tool. We may also want to ignore them when commiting changes in our repository. 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.md in the .gitignore file.

Here is how your .gitignore will look like.

readme.md

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.

How to create .gitignore file

First just create .gitignore file in your root directory. Let’s define our rules of the files that need to be keep. It is just an example.

  • For readme in Github, we will only need a README.md file. Let’s ignore if there is any file README.txt file.
  • We also don’t need a folder called venv.
  • 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.

.gitignore file
.gitignore file

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.

What to GitIgnore

In most cases, we do ignore:

  1. IDE autogenerated files
  2. compiled versions of different files
  3. Some configuration files

Conclusion

Ignoring files and folders is important because not all of those files are needed when we try to move those project to other devices.


Thank you for reading. let me know on the comment section how helpful it was.

If you like the Article you can give it a clap 👏.

If you feel like you owe me 🍺 after learning something valuable you can always leave a comment and share with your network 😃


LEAVE A REPLY

Please enter your comment!
Please enter your name here