Skip to content

2. Setting Up a Git Repository

When starting a project with Git, there are various ways to begin, depending on your situation. You could for example:

  • Create an empty repository: the classic setup where you start from scratch, either on GitHub, GitLab, or another platform.
  • Initialize a local project: if you already have a project on your local machine, you can easily turn it into a Git repository and push it to a remote hosting platform.
  • Join an existing project: if you’re joining a team or contributing to an open-source project, you can clone a remote repository to your local machine.
  • Sync a local project with a remote: if your project exists locally and you have a remote repository, you can set up Git to link the two, allowing synchronization.

While these scenarios may seem different, the core methodology remains the same, and you can follow a consistent flow regardless of the entry point.

When starting a new Git project on your local machine, you can initialize a repository with the following command:

Terminal window
git init # Init a git repository from a directory on your local machine

This command initializes a new Git repository in the current directory. It creates a hidden .git folder that contains all the information Git needs to manage version history for the project. This includes metadata like the current active branch, repository configuration, and other important files necessary for Git operations.

Once you’ve initialized the repository, you can start adding files, making commits, and setting up remotes.

If you want to contribute to an existing project or work on a project that already has a remote repository, you can clone the repository to your local machine:

Terminal window
git clone <remote-repository-url> # Clone a remote hosted git repository on your local machine

This command will create a local copy of the remote repository, including all files, commit history, and configuration. It also sets up the remote origin, so you can easily fetch, pull, and push changes back to the remote repository. This is commonly used when starting to work on an open-source project or collaborating with a team.

After running the git init command, Git will create a hidden .git directory, which contains all the metadata and version history for your repository. On a brand new repository it looks like this:

.git
├── HEAD # points at the branch you are on
├── config # this repository's settings
├── description # only used by the GitWeb viewer
├── hooks/ # sample hook scripts (see Chapter 9)
├── info/ # extra config, e.g. a repo-specific exclude file
├── objects/ # every blob, tree, commit and tag
└── refs/ # branches and tags

That is everything a fresh repo needs and nothing more. Other entries you’ll often see, like index, logs/, COMMIT_EDITMSG, FETCH_HEAD, and ORIG_HEAD, are not there yet. They appear only once you actually stage a file, commit, fetch, or reset. We take a full tour of .git, including the staging index, in Chapter 7.

Let’s break down the most important files and directories.

The objects/ directory stores snapshots of your file system in a key-value format. A Git object can be one of the following types:

  • Blob: represents file contents.
  • Tree: represents a directory structure.
  • Commit: points to a tree and contains metadata (author, message, timestamp).
  • Tag: a reference to a commit (often used for versioning).

Git’s entire version history is stored as objects in this directory. We’ll explore exactly how these fit together in Chapter 7.

The config file contains the repository-specific Git settings. This includes:

  • User information (name & email)
  • Default branch
  • Remote repository details
  • Custom settings like line endings and aliases

This file is similar to the global Git configuration (~/.gitconfig), but it only applies to the current repository.

The HEAD file tracks the currently active branch. In most cases, it points to the latest commit of the active branch:

Terminal window
ref: refs/heads/main

Attached: .git/HEAD holds ref: refs/heads/main. HEAD points to the branch main, which points to the latest commit C3.

The refs/ directory stores pointers (references) to Git objects, which help keep track of different parts of the repository. It includes:

  • refs/heads/ → tracks all branches in the repository.
  • refs/tags/ → stores tags (named commits).
  • refs/remotes/ → tracks remote branches.

These references allow Git to efficiently manage versions and branches.