The .gitignore File
Editors and other programs you use in your workflow may generate config and backup files that you don’t need to track in version control. Git allows you to ignore such files by adding them to the .gitignore
file.
In this lesson, you’ll learn how to create a .gitignore
file, how to ignore files and directories using it, and what not to add to version control.
00:00
The .gitignore
file. There are often files or folders in your project that you don’t want or need to ever go into the repository. Maybe they’re files that Python autogenerates when running, maybe it’s test data that you need for building your program, but it ultimately doesn’t need to be version controlled.
00:21
You can simply ignore these files and directories with .gitignore
. Let’s hop back into the console. So, a common use for .gitignore
is when you’re working with a Python editor, they will oftentimes put a file, like a configuration file, in your working directory that you don’t need to go into your Git repository.
00:45
It’s just configuration settings for your IDE. So, that’s an example of a file that we would .gitignore
. So, I’m just going to put a placeholder in here.
00:56
Let’s actually just touch
a file in here
01:00
to simulate that type of file. And it’ll oftentimes be called something like that, config.xml
. It doesn’t need to be tracked by Git. So when we have a file that we don’t necessarily want to commit, like if I run git status
right now, config.xml
is showing up on there.
01:18
We actually don’t care about tracking it or committing it, so we’re going to ignore it. The first thing we’re going to do is make a new file called .gitignore
.
01:29
I’m going to do this with VIM again, you can do it with whichever text editor that you’d like. And for each line in your .gitignore
file,
01:39
you can put a file, directory, whatever that you want to ignore. The first thing we want to ignore in our directory is config.xml
.
01:53
Oops. I’m going to Escape, Shift, colon (:
), w
(write), q
(quit). So now we have three files in our directory: .gitignore
, config.xml
, and zen_of_python
.
02:07
Let’s see what happens now that we’ve put config.xml
in the .gitignore
file when we run git status
. So config.xml
is gone, but now Git sees .gitignore
as an untracked file. What do we do with that?
02:24
We’re going to commit it. It is common practice to include a .gitignore
file in your repositories. So, do you remember the command to stage and then commit a file?
02:37
The first thing is git add
,
02:42
.gitignore
. That’s fine. And then when we want to commit, git commit -m
, put our message in the imperative.
02:59
Boom. git status
one more time, check it out. nothing to commit, working tree clean
. And there you go! That’s .gitignore
.
03:11 So, this is actually a good time to talk about what not to put in a repo. Most people starting off, myself included, put everything into the repo because we’re worried about missing stuff or not knowing what should be in a repo. There’s a golden rule from Pro Git, the book: Only put source files into repos, never generated files. A source file is something that you created, something you’ve coded.
03:37
A generated file is one of the files we talked about earlier. Maybe your editor makes a configuration file. Python when running will often compile code and put it in the little .pyc
files. You don’t need those either.
03:50 Some things you need to keep in mind are to be careful with binary files, JPEGs, MP3s. These are hard to diff, so Git will generally store the entire file instead of its usual snapshot, regardless of changes.
04:04 This could lead to a bloated gigantic repository over time. And definitely do not put confidential information into a repo. That goes double for public repositories, which we’ll talk about later.
04:16 So things like personal information, passwords, API keys—stuff like that. Somebody will find them, and it’s usually somebody you don’t want to have them.
Become a Member to join the conversation.
Kevin Dienst on Sept. 4, 2019
www.gitignore.io/ This is a useful site to generate your own .gitignore files for common IDEs, operating systems and tool chains. Also this is a helpful one as well from GitHub > github.com/github/gitignore