Bits for hackers.

Gitting Your App Ready With .gitignore

Though Git is a revolutionary tool to enable version control and collaboration between developers, there are a lot of intricacies related to Git that go well beyond just adding, merging, and commiting. Getting a strong handle on Github allows you to more seamlessly build an application by yourself or with other collaborators.

One VERY important aspect to get a handle on very early in one’s career as a programmer is the .gitignore file that is created once you initialize your application (using the command git init). Essentially, the gitignore file is a document that keeps track of all of the files that you DON’T want to track with Git. For example, when you type the command git add . into your terminal, you tell git to start tracking all of your files. Then when you commit these changes and push these changes up to a repository (e.g., Github), all of the files in your application get pushed to the repository.

However, there are some files that you do not want to push up to any sort of public repository. These are files that:

  1. Contain sensitive information about your application
  2. Contain temporary data or data that is only specific to your local environment (these tend to create a lot annoying of merge conflicts when you are collaborating with others)

So what are these files specifically? As the majority of my web development has been focused on Rails apps, here is a good model for a simple .gitignore file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Your log files are not necessary to push up to a respository. Additionally, it can be 
# a privacy risk to provide others with an inside look at how your app functions through
# your log. 

/log/*

#These are any temporary files generated by your app when you run it locally. These can
#include cached sprockets files. Pushing this up while collaborating with others will
#lead to a number of annoying merge conflicts.
/tmp/*

# It can be very annoying when collaborating with others in development to not add 
# this to the gitignore. It leads to a number of merge conflicts since your databases 
# will be different when in development. Additionally, it is a privacy risk to give 
# up your database structure and data.
/db/*.sqlite3

# Add these to make sure that any app tokens and that your Rails app secret are not revealed and pushed to a repository
config/initializers/secret_token.rb
config/secrets.yml

# This file shouldn't be pushed up. It is regenerated when you bundle install an application.
# Including this often leads to merge conflicts which you have to fix; however, you should
# never have to do any work within the Gemfile.lock
Gemfile.lock

#Add these files if you are running any rspec or capybara tests on your application.
capybara-*.html
.rspec
/spec/tmp

If you’re like me and you like sticking to your terminal when interacting with git, you can add files to your gitignore using the following method: echo file_or_folder_name >> .gitignore

It is good practice to add all your gitignore files at the beginning of every Rails app so get in the habit of setting this up early. The template provided above is a very barebones gitignore file. As you develop your application, always try to think whether a file really needs to be tracked by git or whether it is better left untracked.