Starting with an Unversioned Project
Let's start with an existing project that is not yet under version control. Change into the project's root folder on the command line and use the "git init" command to start versioning this project:
$ cd path/to/project/folder $ git init
Now take a moment to look at the files in that directory (including any hidden files):
$ ls -la
You'll see that a new, hidden folder was added, named ".git". All that happened is that Git created an empty local repository for us. Please mind the word "empty": Git did not add the current content of your working copy as something like an "initial version". The repository contains not a single version of your project, yet.
The root folder of your project is often called the "working copy" (or "working directory"). It's the directory on your local computer that contains your project's files.
You can always ask the version control system to populate your working copy with any version of your project. But you always only have one working copy with one specific version on your disk - not multiple in parallel.
Typically, in every project and on every platform, there are a couple of files that you don't want to be version controlled: on Mac OS, e.g., those pesky ".DS_Store" files aren't worth versioning. In other projects, you might have build or cache files that make no sense in a version control system. You'll have to decide yourself which files you don't want to include.
Which Files Should I Ignore?
As a simple rule of thumb you'll most likely want to ignore files that were created automatically (as a "by-product"): temporary files, logs, cache files...
Other examples for excluded files range from compiled sources to files that contain passwords or personal configurations.
A helpful compilation of ignore rules for different projects and platforms can be found here: github.com/github/gitignore
The list of files to ignore is kept in a simple file called ".gitignore" in the root folder of your project. It's highly recommended to define this list at the very beginning of your project - before making your first commit. Because once files are committed, you'll have to jump through some hoops to get them out of version control, again.
Now, let's get going: Create an empty file in your favorite editor and save it as ".gitignore" in your project's root folder. If you're on a Mac, e.g., you'll want to make sure it contains at least the following line:
If there are other files you want to ignore, simply add a line for each one. Defining these rules can get quite complex. Therefore, to keep things simple, I'll list the most useful patterns which you can easily adapt to your own needs:
- Ignore one specific file: Provide the full path to the file, seen from the root folder of your project.
- Ignore all files with a certain name (anywhere in the project): Just write down the file's name, without giving a path.
- Ignore all files of a certain type (anywhere in the project):
- Ignore all files in a certain folder:
Making Your First Commit
With some ignore rules in place, it's time to make our initial commit for this project. We'll go into great detail about the whole process of committing a little later in this book. For now, simply execute the following commands:
$ git add -A $ git commit -m "Initial commit"