Skip to main content

Getting Started with Git · Chapter 1 / 5

Install Git and create your first repository

No theory to start: we'll install Git, set it up, and create your first repository. Every command is explained step by step. No prior knowledge needed.

Chapter 1 / 5

Step 01 / 13

In this chapter

Welcome! This first chapter is 100% hands-on: we'll install Git, set it up, and create your first repository. You don't need any prior knowledge.

By the end of this chapter, you will be able to:

  • Install Git and check that it works
  • Introduce yourself to Git with your name and email
  • Explain what a local repository and a remote repository are
  • Create a repository with git init and connect it to GitHub
  • Download an existing project with git clone
  • Write a README that describes your project

Tip

How to read the dark boxes: the $ sign marks a command to type in the terminal (don't type the $ itself). Lines that start with # are comments: you don't need to type them. The text below the command is Git's response.

Step 02 / 13

What is Git for?

You've probably seen files like report.docx, report-v2.docx, report-final.docx, report-final-REALLY-final.docx. People keep copies so they don't lose anything, but it quickly gets confusing.

Git solves this problem. It's a version control system: it keeps track of every version of your project in a single folder, with no extra copies. With Git, you can:

  • Go back in time: return to a version that worked before you made a mistake.
  • Understand the history: see who changed what, when, and why.
  • Work as a team: everyone works on their own side, then all the work is combined.
  • Experiment safely: try out an idea without breaking what already works.

The analogy

Picture a photographer documenting a construction site. At each important stage, they take a photo and paste it into a notebook with the date and a caption. Git does the same for your project: each saved version is a complete photo, stored in order. We'll come back to this image throughout the course.

Warning

Git is not GitHub. Git is the software installed on your computer. GitHub is a website that hosts Git projects online so you can back them up and share them. You can use Git without GitHub. GitLab and Bitbucket are alternatives to GitHub.

“Git and GitHub are the same thing.” True or false?

Step 03 / 13

Install Git

Git runs in a terminal: a window where you type commands instead of clicking. Here's how to install it, depending on your system.

  • Windows: download the installer from git-scm.com and keep the default options. It also installs Git Bash, the terminal you'll use throughout this course.
  • macOS: open the Terminal app and type git --version. If Git isn't installed, macOS will offer to install it. You can also use Homebrew: brew install git.
  • Linux (Ubuntu, Debian): open a terminal and type sudo apt install git.

To check that the installation worked, type this command. It displays the installed version:

$ git --version

git version 2.47.1

Tip

Your version number may be different. That's fine. If you see "command not found", close the terminal, reopen it, and try again. On Windows, make sure you're using Git Bash.

Which command checks that Git is installed?

Step 04 / 13

Introduce yourself to Git

Every version you save carries its author's signature: a name and an email address. Before you start, tell Git who you are. You only need to do this once per computer.

$ git config --global user.name "Camille Martin"

$ git config --global user.email "camille@example.com"

Replace the name and email with your own. The --global option means "for all my projects on this computer".

While you're at it, set the default name of the main branch for your future projects. GitHub uses main, so let's do the same:

$ git config --global init.defaultBranch main

To check your configuration, display it:

$ git config --global --list

user.name=Camille Martin
user.email=camille@example.com
init.defaultbranch=main

Practice

Use the same email address as on your GitHub account. That way, GitHub recognizes your commits and links them to your profile.

Concept

Local configuration: for a single project (for example, a work project that uses a different email), go into the project folder and run the same command without --global, for example git config user.email "camille@company.com". This setting only applies to that project and overrides the global setting.

What does --global mean in git config --global user.name?

Step 05 / 13

What is a repository?

Concept

Repository (or repo): a project folder tracked by Git. It contains your files and a hidden folder, .git, where Git stores the project's entire history.

There are two kinds of repositories, and you will often use both at the same time:

  • The local repository: it's on your computer. This is where you work, even without an internet connection.
  • The remote repository (or simply "remote"): it's online, on GitHub for example. It serves as a backup and lets you share your work.

Your machine

your local history

Remote repository

GitHub, GitLab…

You work locally: push sends your commits, and fetch/pull brings in everyone else’s.

You work locally, then you send your versions to the remote repository. Your teammates do the same and retrieve your work. We'll cover these exchanges in chapter 3.

Warning

Never delete the .git folder. It holds your project's entire history: without it, your folder goes back to being an ordinary folder and the history is lost.

There are two ways to get started: either your project has never been tracked by Git (you create the repository), or it already exists on GitHub (you download it). Let's look at both.

Local repository
— On your computer, where you work
Remote repository
— Online, for backing up and sharing
.git folder
— The hidden folder holding the history
GitHub
— A website that hosts remote repositories

Step 06 / 13

Create a repository: git init

First case: you have a project folder on your computer, and Git isn't tracking it yet. Move into that folder with cd (change directory), then run git init:

$ cd my-portfolio

$ git init

Initialized empty Git repository in /Users/camille/my-portfolio/.git/

Git has just created the hidden .git folder: your folder is now a repository. To check, list all the files, including hidden ones:

$ ls -a

.  ..  .git  index.html  style.css

git status also confirms that the repository exists, but that no version has been saved yet:

$ git status

On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	index.html
	style.css

nothing added to commit but untracked files present (use "git add" to track)

Practice

Run git init only once per project, at the root of the project folder. Never run it in your home folder or on your Desktop: Git would then track all your files.

Where should you run git init?

Step 07 / 13

Connect your repository to GitHub: git remote

Your repository exists locally. To back it up online, create an empty repository on GitHub:

1

Sign in to github.com (accounts are free).

2

Click the "New" button to create a repository.

3

Give it the same name as your folder, for example, my-portfolio.

4

Don't check "Add a README file": your project already exists locally.

5

Click "Create repository" and copy the address shown (it ends in .git).

Then tell your local repository where the remote repository is:

$ git remote add origin https://github.com/camille-dev/my-portfolio.git

origin is a short name for the remote repository's address. You could choose another name, but origin is the convention: everyone uses it. To check:

$ git remote -v

origin  https://github.com/camille-dev/my-portfolio.git (fetch)
origin  https://github.com/camille-dev/my-portfolio.git (push)

Tip

Nothing has been sent to GitHub yet: you've only saved the address. You'll send your work with git push once you have saved some versions (chapter 3).

Step 08 / 13

Download an existing project: git clone

Second case: the project already exists on GitHub (your own project from another computer, a teammate's, or an open source project). You download it with git clone:

$ git clone <repository-address>

Replace <repository-address> with the address you copied from the green "Code" button on the project's GitHub page. For example, with GitHub's small demo repository:

$ git clone https://github.com/octocat/Hello-World.git

Cloning into 'Hello-World'...
remote: Enumerating objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Receiving objects: 100% (13/13), done.

git clone does three things at once:

  • It creates a Hello-World folder with all the project's files.
  • It downloads the entire history: every version, from the very beginning.
  • It registers the remote repository under the name origin.

$ cd Hello-World

$ git remote -v

origin  https://github.com/octocat/Hello-World.git (fetch)
origin  https://github.com/octocat/Hello-World.git (push)

Practice

After git clone, you don't need git init or git remote add: everything is already set up. Just remember to move into the folder with cd.

“After git clone, you need to run git init.” True or false?

Step 09 / 13

The README: your project's manual

Almost every project has a README.md file at its root. It's the project's manual: the first thing people read when they visit the project. GitHub displays it automatically below the list of files.

It's written in Markdown, a very simple text format: # for a heading, - for a list item. Here is a minimal README:

# Contents of the README.md file

# My portfolio

My personal website, built with HTML and CSS.

## See the site

Open the index.html file in your browser.

## Contents

- index.html: the home page
- style.css: the styling

A good README answers three questions:

  • What is this project for?
  • How do I install or run it?
  • How do I use it?

Tip

You can create the file in your code editor (VS Code, for example). It isn't saved in the history yet: you'll learn how in the next two chapters.

Step 10 / 13

Recap: starting a project

1

Install Git and check it with git --version

2

Introduce yourself with git config --global user.name and user.email

3

Create the repository with git init (or download a project with git clone)

4

Connect the repository to GitHub with git remote add origin <address>

5

Add a README.md that describes the project

Your cheat sheet for this chapter:

$ git --version # check the installation

$ git config --global user.name "Name" # your name

$ git config --global user.email "email" # your email

$ git init # create a repository

$ git remote add origin <address> # connect a remote repository

$ git remote -v # list remote repositories

$ git clone <address> # download an existing project

git --version
— Check the installation
git config --global
— Introduce yourself (name, email)
git init
— Create a repository
git remote add origin
— Connect a remote repository
git clone
— Download an existing project

Step 11 / 13

Common mistakes

These messages can be scary the first time you see them. They almost always have a simple cause:

  • "fatal: not a git repository": you're not inside a repository. Check where you are with pwd, then move into the right folder with cd. If the project has never been initialized, run git init.
  • "Please tell me who you are": Git doesn't know your name and email yet. Run the two git config --global commands from this chapter.
  • "remote origin already exists": an origin address is already saved. To replace it: git remote set-url origin <new-address>.
  • git init in the wrong folder: delete only the .git folder it created there by mistake, then run git init again in the right folder.
  • Mixing up Git and GitHub: Git is the software on your computer, GitHub is the website that hosts your repositories online.

Step 12 / 13

Bonus: under the hood

Tip

This section is optional, for curious learners who want to know what's inside the .git folder. You can skip straight to the quiz.

The .git folder is just a regular folder. You can look inside:

$ ls .git

HEAD  config  description  hooks  info  objects  refs
  • objects: every version of your files, in compressed form. This is the core of the history.
  • refs: the branches (we'll cover them in chapter 4).
  • HEAD: indicates which branch you're currently on.
  • config: settings specific to this repository, including the remote repository's address.

$ cat .git/config

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/camille-dev/my-portfolio.git
	fetch = +refs/heads/*:refs/remotes/origin/*

You can see the address you added with git remote add. Git commands just read and write these files: there's no magic involved.

Warning

Look, but don't edit anything in .git by hand: always use Git commands.

Step 13 / 13

Test your knowledge

Before moving on to the next chapter, make sure you remember the key points. Click the answer you think is correct: each one comes with an explanation.

Quiz · Chapter 1

1. What is the difference between Git and GitHub?

2. Why do you set user.name and user.email?

3. You have a project folder that Git doesn't track yet. Which command turns it into a repository?

4. What does git clone <address> do?

5. In git remote add origin <address>, what is origin?

6. What is the README.md file for?

Chapter 1 of 5

The investigation is just getting started. The next clue is waiting...

Your feedback matters

Help us improve this course by sharing your thoughts.

So we can follow up if needed.