Skip to content

Set up Git

  • Lesson 2
  • 20 min
  • Module 1: Understand a repository
Lesson 2 of 15 · Git and Team Development EssentialsSet up Git

What you will be able to do

Make Git ready to use: install it, give it your name, and make your first repository. After this lesson, Git can record your work in the ticket-notes project.

Before you startWhat to know and to have readyAbout 20 minutes

What you need to know

What you need on your computer

Tested with Git 2.54 on macOS, Windows (Git Bash) and Linux. Any Git version from 2.40 works for this lesson.

A repository (or “repo”) is a project folder that Git tracks. It looks like a normal folder, with one addition: a hidden folder called .git. Git keeps the full history of the project in .git.

You do not open or change the .git folder yourself. You use Git commands, and Git updates .git for you. If you delete .git, the folder becomes a normal folder again, and the history is gone.

A repository can exist in more than one place:

  • A local repository is on your computer. You work in it, and Git does not need the internet for this work.
  • A remote repository (or “remote”) is a copy of the same repository in a different place. Usually it is on a server such as GitHub. Your team shares work through the remote.

Git does not copy your work to a remote automatically. You send and receive changes when you decide to. You learn how in Module 2.

  1. In the terminal, run:

    Terminal window
    git --version
  2. If a window asks you to install the “command line developer tools”, select Install. When the installation is complete, run git --version again.

The output shows a version number. Yours can be different:

Output
git version 2.54.0

Each time you save a change, Git records who made it. Give Git your name and email address once. The --global option means “use this for every repository on this computer”.

  1. Set your name. Replace Your Name with your name, and keep the quotes:

    Terminal window
    git config --global user.name "Your Name"
  2. Set your email address. Replace the example address with yours:

    Terminal window
    git config --global user.email "you@example.com"
  3. Set the name of the first branch in new repositories to main:

    Terminal window
    git config --global init.defaultBranch main

    A branch is a line of work in a repository. You learn about branches in Module 2. Most teams now call the first branch main. Some Git versions use master if you do not set this.

  4. Check your settings:

    Terminal window
    git config --global --list

    The output shows your values:

    user.name=Amira Khan
    user.email=amira@example.com
    init.defaultbranch=main

    Git shows the names in lowercase. This is normal.

If the output has a mistake, run the same git config command again with the correct value. The new value replaces the old one.

  1. Move to your projects folder:

    Terminal window
    cd ~/projects
  2. Make a repository called ticket-notes:

    Terminal window
    git init ticket-notes

    Git makes the folder and the .git folder inside it:

    Initialized empty Git repository in /Users/amira/projects/ticket-notes/.git/
  3. Move into the repository:

    Terminal window
    cd ticket-notes
  4. List all the files, including hidden ones. The -a option means “all”:

    Terminal window
    ls -a
    . .. .git

    The .git folder is there. On some systems, the names are on separate lines. . means “this folder” and .. means “the folder above”.

  5. Ask Git about the state of the repository:

    Terminal window
    git status
    On branch main
    No commits yet
    nothing to commit (create/copy files and use "git add" to track)

    You use git status often. It is the safest command in Git, because it only shows information and changes nothing.

To clone means to make a full copy of a repository, with its history. When you join a team, you usually clone the team’s repository from a server.

You can also clone from a folder on your own computer. This shows how local and remote repositories connect, without an account on a server. Amira clones ticket-notes into a second folder:

Terminal
cd ~/projects
git clone ticket-notes ticket-notes-copy
Output
Cloning into 'ticket-notes-copy'...
warning: You appear to have cloned an empty repository.
done.

The warning is expected. The original repository has no saved changes yet, so the copy is empty too.

Next, she moves into the copy and asks Git where its remote is. The -v option means “verbose”: show more detail.

Terminal
cd ticket-notes-copy
git remote -v
Output
origin /Users/amira/projects/ticket-notes (fetch)
origin /Users/amira/projects/ticket-notes (push)

The copy remembers where it came from. origin is the name that Git gives to the remote that you cloned from. For the copy, ticket-notes is the remote. On a team, origin is usually an address on a server, for example https://github.com/team/ticket-notes.git.

The original ticket-notes has no remote. Run git remote -v in it: the output is empty.

Answer these questions with commands, not from memory:

  1. In ticket-notes-copy, which branch are you on? Use git status.
  2. Does ticket-notes-copy have a .git folder of its own? Use ls -a.
  3. Does the original ticket-notes have a remote? Use git remote -v.

When you finish, delete the copy. You do not use it again, and a new clone is easy to make. Go up to projects, then delete the folder:

Terminal window
cd ~/projects
rm -rf ticket-notes-copy
Show the answers
  1. main. A clone uses the same first branch as the original.
  2. Yes. Every clone is a full repository, with its own .git folder and its own history.
  3. No. git remote -v shows nothing in ticket-notes, because you made it with git init, not with git clone.

Common mistake

Not a git repository

Symptom: you run a Git command, and Git shows this error:

fatal: not a git repository (or any of the parent directories): .git

Cause: the working directory is not in a repository. Git looks for a .git folder in the working directory and in each folder above it. It did not find one. Usually you are in the wrong folder.

Fix:

  1. Run pwd to see where you are.
  2. Move into the repository, for example with cd ~/projects/ticket-notes.
  3. Run the Git command again.

A related mistake: you ran git init in your home folder. Now git status in your home folder lists all your files as untracked. To fix it, go to your home folder and delete only the .git folder there with rm -rf ~/.git. Check the command before you run it. Your files stay, and only the new, empty history is removed. Do not do this in a real project, because it deletes that project’s history.

Check your understanding

Amira runs git status in /Users/amira/projects. Git shows 'fatal: not a git repository'. But ticket-notes is a repository. Why does she get the error?

Show the answer

The projects folder is not a repository. The repository is the ticket-notes folder inside it. Git looks in the working directory and the folders above it, never in the folders below it. She must run cd ticket-notes first.

Check your understanding

Practical check: show that Git knows your name and email address, and that ticket-notes is a repository with no commits.

Show the answer

Run git config --global --list: the output shows your user.name and user.email. Then run cd ~/projects/ticket-notes and git status: the output shows On branch main and No commits yet.

Recap

  • A repository is a folder with a hidden .git folder, which keeps the history.
  • You set your name and email address once with git config --global.
  • git init makes a new repository. git clone copies a repository that exists, with its history.
  • A local repository is on your computer. A remote is a copy in another place. Git connects them only when you tell it to.
  • git status shows the state of a repository and changes nothing.

Next, you save your first change in ticket-notes.

Next: Track a first change