Chapters

Hide chapters

Git Apprentice

Second Edition · Git 2.32 · Console

Section I: Beginning Git

Section 1: 11 chapters
Show chapters Hide chapters

5. Ignoring Files in Git
Written by Bhagat Singh & Chris Belanger

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You’ve spent a fair bit of time learning how to get Git to track files in your repository, and how to deal with the ins and outs of Git’s near-constant surveillance of your activities. So it might come as a wonder that you’d ever want Git to actively ignore things in your repository.

Why wouldn’t you want Git to track everything in your project? Well, there are quite a few situations in which you might not want Git to track everything.

A good example would be any files that contain API keys, tokens, passwords or other secrets that you definitely need for testing, but you don’t want them sitting in a repository — especially a public repository — for all to see.

Depending on your development platform, you may have lots of build artifacts or generated content sitting around inside your project directory, such as linker files, metadata, the resulting executable and other similar things. These files are regenerated each time you build your project, so you definitely don’t want Git to track these files. And then there are those persnickety things that some OSes add into your directories without asking, such as .DS_Store files on macOS.

Introducing .gitignore

Git’s answer to this is the .gitignore file, which is a set of rules held in a file that tell Git to not track files or sets of files. That seems like a very simple solution, and it is. But the real power of .gitignore is in its ability to pattern-match a wide range of files so that you don’t have to spell out every single file you want Git to ignore, and you can even instruct Git to ignore the same types of files across multiple projects. Taking that a step further, you can have a global .gitignore that applies to all of your repositories, and then put project-specific .gitignore files within directories or subdirectories under the projects that need a particularly pedantic level of control.

In this chapter, you’ll learn how to configure your own .gitignore, how to use some prefabricated .gitignore files from places like GitHub, and how to set up a global .gitignore to apply to all of your projects.

Getting started

Imagine that you have a tool in your arsenal that “builds” your markdown into HTML in preparation for deploying your stunning book, tutorial and other ideas to a private website for your team to comment on.

mkdir sitehtml
touch sitehtml/all-todos.html
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
  sitehtml/

nothing added to commit but untracked files present (use "git add" to track)
touch .gitignore
*.html
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
  .gitignore

nothing added to commit but untracked files present (use "git add" to track)
*/*.html
touch index.html
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
  .gitignore
  index.html

nothing added to commit but untracked files present (use "git add" to track)

Nesting .gitignore files

You can easily nest .gitignore files in your project. Imagine that you have a subdirectory with HTML files that are referenced from your index.html. These aren’t generated by your imaginary build process but, rather, maintained by hand, and you want to make sure Git is able to track these.

mkdir htmlrefs
touch htmlrefs/utils.html
touch htmlrefs/.gitignore
!/*.html
~/GitApprentice/ideas $ git status
On branch main
Your branch is ahead of 'origin/main' by 7 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
  .gitignore
  htmlrefs/
  index.html

nothing added to commit but untracked files present (use "git add" to track)
git add .
git commit -m "Adding .gitignore files and HTML"

Looking at the global .gitignore

Execute the following command to find out if you already have a global .gitignore:

git config --global core.excludesfile
touch ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
~/GitApprentice/ideas $ git config --global core.excludesfile
/Users/chrisbelanger/.gitignore_global

Finding sample .gitignore files

This is one of those situations wherein you don’t have to reinvent the wheel. Hundreds of thousands of developers have come before you, and they’ve already figured out what the best configuration is for your particular situation.

Challenge

Challenge: Populate your global .gitignore

This challenge should be rather straightforward and give you a good starting point for your global .gitignore. Your goal is to find the correct .gitignore for your own OS, get that file from the GitHub repository, and add the contents of that file to your global .gitignore.

Key points

  • .gitignore lets you configure Git so that it ignores specific files or files that match a certain pattern.
  • *.html in your .gitignore matches on all files with an .html extension, in any directory or subdirectory of your project.
  • */*.html matches all files with an .html extension, but only in subdirectories of your project.
  • ! negates a matching rule.
  • You can have multiple .gitignore files inside various directories of your project to override higher-level matches in your project.
  • You can find where your global .gitignore lives with the command git config --global core.excludesfile.
  • GitHub hosts some excellent started .gitignore files at https://github.com/github/gitignore.

Where to go from here?

As you work on more and more complex projects, especially across multiple code-based and coding languages, you’ll find that the power of the global .gitignore, coupled with the project-specific (and even folder-specific) .gitignore files, will be an indispensable part of your Git workflow.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now