FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.4 Ignoring Files

There are always going to be a few files that you don’t want to commit. Do you just have to continually remember not to stage and commit them? Nope: it’s the `.gitignore` file to the rescue!

2.4 Ignoring Files

As the used Git with larger projects, you'll find that there are some files in your project directory that you don't want Git to track. They're really not part of the project and they probably change a lot so you don't wanna have to worry about continually committing them or continually just manually leaving them out of your commits. This might be something like a swap file or something similar created by your text editor. I use Vim as my text editor, and I know that there are some settings where you can't have Vim create a swap file, which is just basically a temporary version of the file that tracks the changes made within a single file, so that you can actually undo past the last opening of your file. That would definitely be something you want Git to ignore. Another type of file you probably want Git to ignore is files output by running a script on the other parts of your project. For example if you're building a CoffeeScript application, you probably don't want the JavaScript files that CoffeeScript puts out committed into your project repository. You wan the history only to track the actual quote code and not the quote binaries that you're creating and for anyone else looking to repository they'll probably want to generate the JavaScript files themselves just to make sure they're using the latest version of CoffeeScript etc. So in this greencast we're gonna look at how to exclude these files from your Git repository. As always, we'll start by opening up a terminal and moving into our project directory. And if I run Git status you can see that our working directory is clean, just as we left it, so there are no current changes to the file. So let's make some files and folders that we want Git to ignore. If you work with Mac OS X you may have noticed that, occasionally, Mac OS X will create a .DS_Store file. It begins with a dot so you wouldn't actually see it in the Finder, however these will pop up occasionally. And if you get them in a Git repository you really don't want to commit them cuz they're pretty much meaningless. So I'm gonna create one to simulate the wackiness of Mac OS X. And now let's also make a directory and we'll call it temp, T-M-P for short. And in there I'll just create another and call it temp1, and that's a file that we don't want Git to tracks. In fact, we dont want Git to track anything that goes on in the whole temp directory. We dont want it to even know there's a temp directory there. So if I run git status now, you can see that Git is recognizing both the file and the folder as untracked files, and it's telling us you probably want to commit those. We don't want to commit them, so we could just ignore them manually. For example, let's open up our to do file, here, and mark our second to do as done. Cuz we're learning about ignoring files, right now. Now, if I run, Git status, you'll see the to do file has been modified. And so if I want to commit it, I have to do GIT add, to do. Or I could just do git commit -a, and now if I run Git status, you can see that the to-do file has been staged for commit however, the untracked files are still untracked. And now I can just do git commit -m, and I can just say updated to-do. So I've made a commit, I'll clear my screen here and run git status and now you can see we were successful in ignoring those two files. However, you're gonna want to occasionally probably do something like git add dot and if you do that, then those will be staged. And while there are ways to unstage files as we'll see later on in this screencast series. And if you accidentally commit them, well, then you're in trouble and you can't do things to get them out of there, but It's much more difficult. It's much more easier in the first place to just tell git to ignore them, so you don't have to remember to do that yourself. And this is done through a .gitignore file. So I'm gonna open a file named .gitignore on each line on this file, I just list a file or folder that I want Git to ignore. So on the first line here, I can just say, ignore all files called DS Store and now that will be ignored. Next line, I can say ignore the temp directory entirely. And it doesn't matter that that's a directory and not a file. Git will know the difference and we'll just make sure not to track that entire directory at all. We can do more complex things, for example, we could say, do not track any file ending with swp tilde. Or you can ignore specific files within the directory. So, for example, let's say we have a test directory and in there, you have a bunch of text files that are just kind of scrap paper/whiteboard, where you just scribble down some stuff to remember it. But you don't want, actually, to be committed to your repository. However, let's say, within that test directory, you do have one text file, your master text file, that you do want to be committed and track. Well, then, what you can do, is, on this line, have bang, or !Test/master.txt. Whenever you have a bang this means make sure that you actually do track this no matter what other rules I've set in telling you what not to track. So test/*.txt is gonna say don't track any txt files in the test folder. However this line will say make sure you do test However make sure you do track the master.txt file and I'll create those in a second here just to show you how exactly that works. So let's quit that and now if I run Git status you'll see that our status has changed. We don't have The DS store and temp folder anymore. We just get this .gitignore file that we've just created. That's our only untracked file, because git is looking inside the .gitignore file and saying, I see we have a DS store file that we're not tracking, but you don't want us to track it, so I'll just leave it out. In fact, if right now, I get gitadd dot to add every unstaged change to the staging area you might think that that would add the .DS_Store file and the tmp folder, and if we hadn't added them to the .gitignore file, it would. However since we have added them, Git only stages the stuff that we have not told it to ignore. So while it does see the tmp folder and the .DS_Store file It says are these are unstaged changes. However, you dont' want me to stage these, you want me to just ignore them, so it leaves them out. All right, so now let's commit this, and I'll just say git commit -m added a .gitignore file. And we've made our commit, and now if I run git status, you can see that we are clean. Everything has been committed, and let me show you what I was saying about the making sure we do track a specific file. If I make a test directory here, and now I'm going to just touch a one.txt file, if I run git status, you can see that there's nothing to commit. Git sees the test folder. It sees the one.txt file, however, it knows I'm not supposed to track that. However, if I touch a test/master.txt, and now I run git status, Git does see the test folder and it does just show the test folder. It doesn't Specifically give you the file path if I say, getadd., let me clear this screen first so it doesn't scroll for you. Git status, you can see that we have created a new file text/master.txt, the test/1.txt has been ignored. And I'm just going to actually use git reset head test/master to un-stage that and now I'm just going to remove -r the test folder entirely, and now if we do git status, we're clean. And I know you have not learned the git reset command yet, but you will learn it later on. I guess that's a teaser to keep you watching. So that is how you ignore files within Git.

Back to the top