Creating a repository

Jose Robledo

04 March 2025

What is a Git repository?

Creating a directory

  • We will help Alfredo with his new project, create a repository with all his 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

Initializing a repository

$ git init

It is important to note that git init will create a repository that can include subdirectories and their files. Also, note that the creation of the recipes directory and its initialization as a repository are completely separate processes.

  • When we ran git init, a hidden folder was created in the current directory. It is always called .git and you can see it by listing the files in the current directory with an additional flag to show all files and folders (including hidden ones):
$ ls -a

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.

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

Output:

On branch main

No commits yet

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

Exercise

Along with tracking information about recipes (the project we have already created), Alfredo would also like to track information about desserts specifically. Alfredo creates a desserts project inside his recipes project with the following sequence of commands:

$ cd ~/Desktop    # return to Desktop directory
$ cd recipes      # go into recipes directory, which is already a Git repository
$ ls -a           # ensure the .git subdirectory is still present in the recipes directory
$ mkdir desserts # make a sub-directory recipes/desserts
$ cd desserts    # go into desserts subdirectory
$ git init        # make the desserts subdirectory a Git repository
$ ls -a           # ensure the .git subdirectory is present indicating we have created a new Git repository

Is the git init command, run inside the desserts subdirectory, required for tracking files stored in the desserts subdirectory?

Git repositories can interfere with each other if they are “nested”: 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

Alfredo would like now to go back to a single git repository. How can Alfredo undo his last git init in the desserts subdirectory?

REMIMNDER: To delete if

it’s a file:

$ rm filename

it’s a directory:

$ rm -rf dirname

Takeaway

  • git init initializes a repository.
  • Git stores all of its repository data in the .git directory.

Continue with

04. Tracking changes

Content

01. Intro to Git

02. Setting up Git

03. Creating a repository

04. Tracking changes

05. Exploring History

06. Ignoring things

07. Remotes in GitHub

08. Collaborating

09. Branching and Merging