Create a repository

Key points
  • git init initialises a repository.
  • Git stores all of its repository data in the .git directory.

Once Git is configured, we can start using it.

We will create a repository with all our favourite recipes.

First, let’s create a new directory in the Desktop folder for our work and then change the current working directory to the newly created one:

cd ~/Desktop
mkdir recipes
cd recipes

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

git init

It is important to note that git init will create a repository that can include subdirectories and their files—there is no need to create separate repositories nested within the recipes repository, whether subdirectories are present from the beginning or added later. Also, note that the creation of the recipes directory and its initialisation as a repository are completely separate processes.

If we use ls -a to show everything, we can see that Git has created a hidden directory within recipes called .git:

. .. .git

Git uses this special subdirectory to store all the information about the project, including the tracked files and sub-directories located within the project’s directory. If we ever delete the .git subdirectory, we will lose the project’s history.

Nested subdirectories

You do not need to initialise a repository in a subdirectory of a directory that is already a repository – in fact, it is best not to!

Removing files from a Git repository needs to be done with caution. But we have not learned yet how to tell Git to track a particular file; we will learn this in the next episode. Files that are not tracked by Git can easily be removed like any other “ordinary” files with:

rm filename

Similarly a directory can be removed using

rm -r dirname

If the files or folder being removed in this fashion are tracked by Git, then their removal becomes another change that we will need to track, as we will see in the next episode.

Git keeps all of its files in the .git directory. To recover from this little mistake, remove the .git folder in the subdirectory by running the following command from inside the parent directory:

rm -rf subdirectory/.git

Check status

We can now start using one of the most important git commands, which is particularly helpful to beginners. git status tells us the status of our project, and better, a list of changes in the project and options on what to do with those changes. We can use it as often as we want, whenever we want to understand what is going on.

We can now start using one of the most important git commands, which is particularly helpful to beginners. git status tells us the status of our project, and better, a list of changes in the project and options on what to do with those changes. We can use it as often as we want, whenever we want to understand what is going on.

git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)