ARC Training

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 planets
% cd planets

Then we tell Git to make planets 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

Initialising our repository apparently did nothing, but if we add the -a flag to show everything, we can see that Git has created a hidden directory within planets 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)

If you are using a different version of git than I am, then the exact wording of the output might be slightly different.

When and where to create git repositories

Dracula starts a new project, moons, related to his planets project. Despite Wolfman’s concerns, he enters the following sequence of commands to create one Git repository inside another:

% cd             # return to home directory
% mkdir planets  # make a new directory planets
% cd planets     # go into planets
% git init       # make the planets directory a Git repository
% mkdir moons    # make a sub-directory planets/moons
% cd moons       # go into planets/moons
% git init       # make the moons sub-directory a Git repository

Why is it a bad idea to do this?

Keep repositories separate

Notice here that the planets project is now also tracking the entire moons repository.

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

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

% rm -rf moons/.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

  • git init initializes a repository.
  • Keep gits separate