Practical Git: Remove unnecessary git tracking with .gitignore files

Trevor Miller
InstructorTrevor Miller

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 3 years ago

Most projects have automatically generated files or folders from the operating system, applications, package managers etc. Usually, we don't want to include these types of things in our remote repos because they can clutter the git history/storage and are not applicable to everyone that works on the project. In this lesson, we show how to create a .gitignore file to ignore files and folders from being tracked by git.

[00:00] We're inside a directory called Utility Functions, which is a git repo. Our working directory is currently clean and our project contains exactly what we would expect. There are a few files as well as .git folder.

[00:12] There are some types of programs that can automatically generate files we may not want in our git repo. For example, let's open up this folder, inside of the Mac OS X Finder. If I make a change in this program, it automatically generates this new .dsstore file.

[00:30] Back in our command line, if we run GetStatus, it recognizes that there's a new on track. We don't to include user specific or automatically generated files or folders in our repo though. What we can do is create a new file called a .gitignore, not to open that in our code editor, now we can add our our .dsstore file type to our gitignore file. This will tell git that we don't want to track any files that match this pattern.

[00:57] Let's save and close this file and go back to our command line. Now, if we run GetStatus, we see that see that our dsstore file is no longer included in the tracked files and we're free to commit and push this gitignore file as normal.

[01:10] If we had already started tracking a file or folder before we added it to our gitignore, only to run gitrm--cached in order to clear the git cache, to remove the tracked files...

[01:26] Going back to our gitignore file, there are many different types of patterns of files and folders that you may want to ignore in your projects. Here are a few examples. The .dsstore files automatically generated by Mac OS X Finder, Windows automatically generates a thumbs.db file, certain editors automatically generate files and folders such as .ida or .sublimeworkspace. This one I've added an asterisk to because we want to glob or match all patterns that come before this pattern.

[01:58] We also may want to ignore files and folders automatically generated by package managers such as node modules or npm debug. We may also want to remove binary files like .png or remove cache folders that are automatically created from a script.

[02:13] As you can see, there are many different types of files and folders that we could ignore from our projects. What you choose to add to you gitignore will be different depending on the project and what type of technology you're working with.

[02:24] The bottom line is that generally you shouldn't include anything that's automatically generated or that's user specific in your git repos because it can clutter your git diffs and your remote storage.