Git and GitHub · Part 1 · Git basics

Your first repository

What a repository is

A repository (or repo) is a project folder that Git tracks: the same folder, with the save system turned on. The files don't change. Git only adds one hidden folder, .git, that holds every save point.

Creating a repository

1.Make a practice folder

Terminal
cd ~/Desktop
mkdir git-practice
cd git-practice

This makes a folder on the Desktop and moves into it. For now, it's a normal folder, and Git says so:

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

2.Turn Git on

Terminal
git init
Output
Initialized empty Git repository in /Users/you/Desktop/git-practice/.git/

The folder is now a repository. ls still shows nothing, because names starting with a dot are hidden. ls -la shows hidden files too:

Terminal
ls -la
Output
drwxr-xr-x  .
drwxr-xr-x  ..
drwxr-xr-x  .git

In Finder, Cmd + Shift + . shows hidden files.

3.Ask Git what's going on

Terminal
git status
Output
On branch main

No commits yet

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

The project is on the main branch (the setting from Lesson 2), with no commits and no files yet.

What's inside .git

Terminal
ls .git
Output
config  description  HEAD  hooks  info  objects  refs

This folder is the repository. The parts that matter:

ItemWhat it holds
objects/Every snapshot: file contents and commits
refs/Pointers to commits, such as branches and tags
HEADWhere the project is right now
configSettings for this repository only
hooks/Scripts that can run automatically, such as before a commit

They're plain files. HEAD, for example, is one line of text:

Terminal
cat .git/HEAD
Output
ref: refs/heads/main

That means “on the main branch.” Git manages all of this, so there's no need to edit anything in .git. And since the whole history is in this one folder, copying the project folder to another computer copies its history too.

Adding a first file

Terminal
echo "Hello Git" > notes.txt
git status
Output
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        notes.txt

Untracked means Git can see the file but isn't tracking it yet. The hint says how to start: git add.

See these steps in a real terminal
A Mac terminal running git status before and after git init, ls -la showing the .git folder, ls .git, cat .git/HEAD, cat .git/config, then a nested my-new-project repository and an untracked notes.txt, and rm -rf my-new-project.
This lesson, run for real, including the nested repository mistake below and its fix.

Mistakes to avoid

  • Deleting .git. The files stay, but the whole history is gone for good.
  • Running git init in the home folder or on the Desktop. Git then tracks everything inside it. Always cd into the project folder first. To undo it, delete the .git folder that was created by mistake, for example rm -rf ~/.git, and nowhere else.
  • A repository inside a repository. git init my-new-project creates a new folder with its own .git. Run inside git-practice, it makes a nested repository, which causes confusing problems. Delete it with rm -rf my-new-project, and only run git init outside other repositories.