GITnoring the right way
By: Oscar Mauricio Forero Carrillo
Tags: Post Your Work On Github, Git, Gitignore, Udacity

I am an Udacity mentor, and I helped many students move forward with their GitHub project.

My previous article was about the problems faced by Windows users.

Now we will discuss the second most common mistake, but with a bigger impact on real-life projects.

What files to track with GIT?

The project requires the students to copy a set of data files to the workspace.

The files are from another project and in CSV format. But they should ensure the CSV files are not committed to the repository.

Academic projects have to be of limited scope. This makes the importance of some requirements hard to explain.

Why we would copy files into a workspace if we need to ignore them in the Source Control tool.

The reason is simple. GIT is an outstanding tool for tracking the contents of text files. But it does a poor job tracking binary files, especially bigger files.

A text file is one that only contains text characters and no special attributes for formatting. A text file could be a source file for a program, a simple text file; but not a Word Document or a Rich Text Format file.

Notable exceptions are HTML and Markdown files. They are plain text files, and GIT can track them without any issues. They include formatting information but in special combinations of text characters.

Computer-generated files are not a good fit for GIT. They make GIT operations slower, and prone to get meaningless merge conflicts.

Examples of these files are OSX generates index folders called .DS_Store and compiled Python files ( .pyc )

Dealing with binary files

To avoid this, GIT provides a mechanism to instruct it which files from the workspace not to track.

.gitignore is a simple text file, usually in the workspace’s root folder, but it can be present in any folder. GIT will apply the ignore rules to the current folder and all its subfolders.

The following list addresses the most common mistakes in the contents of the file:

.gitignore has to be a regular text file, not a directory. .gitignore should contain names (E.g. chicago.csv) or expressions which match the files or directories you want to exclude (E.g. *.csv) Each line is either a comment if it starts with a pound sign # or a pattern or name to match files against. GIT uses the ignore file during the staging ( git add ) phase. GIT ignores a file or directory if its name matches any of the lines inside .gitignore GIT checks each file against the patterns in the ignore file in the same folder or any of its parents. If the file matches any of the patterns it is not staged. This pattern applies to all the subdirectories unless the pattern includes folder hierarchies. If a line includes slashes / it will only match specific paths. If the line starts with a slash the path will be absolute from the root of the repository. If the line finishes with a slash, GIT will ignore only folders with that name but not files or symbolic links. Adding a pattern to `.gitignore` won’t remove any committed files from the repository. We will have to use `git rm` and `git commit` for that, and even then they will still be in the repository’s history. To avoid committing unwanted files to the repository; a ignore file should be part of the initial or other early commits.

Working in teams

The other issue with academic projects is that only one person in a single machine works on them.

Real-life projects need teams. In such cases, we need to ensure nobody in the team commit any unwanted file by mistake.

This is why a .gitignore should be committed and pushed to the central repository in the very first commit.

Conclusion

In this article, We reviewed the importance of using .gitignore to keep unwanted files out of the repository.

Now, go back and learn!

If you enjoyed the article and found it useful, please consider buying me a coffee.