Version Control with Git

Creating a Repository

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • Where does Git store information?

Objectives
  • Create a local Git repository.

Once Git is configured, we can start using it. Let’s create a directory for our work and then move into that directory:

$ mkdir guac
$ cd guac

Then we tell Git to make guac a repository—a place where Git can store versions of our files:

$ git init

If we use ls to show the directory’s contents, it appears that nothing has changed:

$ ls

But if we add the -a flag to show everything, we can see that Git has created a hidden directory within guac called .git:

$ ls -a
.	..	.git

Git stores information about the project in this special sub-directory. If we ever delete it, we will lose the project’s history.

We can check that everything is set up correctly by asking Git to tell us the status of our project:

$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

Places to Create Git Repositories

We start a new project, family_recipes, related to our guac project. We enter the following sequence of commands to create one Git repository inside another:

$ cd                      # return to home directory
$ mkdir guac              # make a new directory guac
$ cd guac                 # go into guac
$ git init                # make the guac directory a Git repository
$ mkdir family_recipes    # make a sub-directory guac/family_recipes
$ cd family_recipes       # go into guac/family_recipes
$ git init                # make the family_recipes sub-directory a Git repository

Why is it a bad idea to do this? (Notice here that the guac project is now also tracking the entire family_recipes repository.) How can we undo our last git init?

Solution

Git repositories can interfere with each other if they are “nested” in the directory of another: the outer repository will try to version-control the inner repository. Therefore, it’s best to create each new Git repository in a separate directory. To be sure that there is no conflicting repository in the directory, check the output of git status. If it looks like the following, you are good to go to create a new repository as shown above:

$ git status
fatal: Not a git repository (or any of the parent directories): .git

Note that we can track files in directories within a Git:

$ touch dad mom aunt_phoebe maria               # create recipe files
$ cd ..                                         # return to guac directory
$ ls family_recipes                             # list contents of the family_recipes directory
$ git add family_recipes/*                      # add all contents of guac/family_recipes
$ git status                                    # show family_recipes files in staging area
$ git commit -m "add recipes from relatives"    # commit guac/family_recipes to guac Git repository

Similarly, we can ignore (as discussed later) entire directories, such as the family_recipes directory:

$ nano .gitignore # open the .gitignore file in the texteditor to add the family_recipes directory
$ cat .gitignore # if you run cat afterwards, it should look like this:
family_recipes

To recover from this little mistake, we can just remove the .git folder in the family_recipes subdirectory. To do so we can run the following command from inside the ‘family_recipes’ directory:

$ rm -rf family_recipes/.git

But be careful! Running this command in the wrong directory, will remove the entire git-history of a project you might wanted to keep. Therefore, always check your current directory using the command pwd.

Key Points