.gitignore Essentials: Tailoring Your Repository
Discover the essentials of using a .gitignore file effectively in Git, as explored in Udacity courses. This guide on Turing Taco helps you maintain cleaner repositories by managing what gets tracked, ensuring a smoother project management experience.
Overview
This tutorial delves into the underrated yet powerful tool in the Git toolkit, the .gitignore
file. This simple configuration file can save you from many headaches, like preventing data files from getting committed to your repository. We'll master the .gitignore file, which is a step towards maintaining cleaner repositories. We'll not only learn enough to pass Udacity's GIT project but also to use the .gitignore
file effectively in real projects. Join us as we demystify the art of ignoring files correctly in Git.
What files to track with GIT?
Udacity's GIT project requires students to copy data files to the workspace. The files are from another project and in CSV format. However, 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 would we 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. However, there are better tools to manage binary files, particularly bigger ones.
A text file only contains text characters and no special formatting characters. 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 issues. They include formatting information but in unique combinations of text characters.
Computer-generated files are not a good fit for GIT. They make GIT operations slower and prone to meaningless merge conflicts.
Examples of these files are OSX generates index folders called .DS_Store
and compiled Python files (.pyc
)
Telling Git Which Files to Ignore
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.
A .gitignore should be committed and pushed to the central repository in the first commit.
Conclusion
In this article, We reviewed the importance of using .gitignore
to keep unwanted files from the repository.
Now, go back and learn!
Addendum: A Special Note for Our Readers
I decided to delay the introduction of subscriptions, you can read the full story here.
In the meantime, I decided to accept donations.
If you can afford it, please consider donating:
Every donation helps me offset the running costs of the site and an unexpected tax bill. Any amount is greatly appreciated.
Also, if you are looking to buy some Swag, please visit I invite you to visit the TuringTacoTales Store on Redbubble.
Take a look, maybe you can find something you like: