Creating a Repository

A repository is an ordinary project folder that Git is keeping a history for. You get one in two ways: turn an existing folder into a repository with git init, or download a copy of an existing one with git clone. This lesson covers the first; cloning comes in lesson 33.

git init

Sam already has a folder of recipes and wants to start tracking it:

$
cd recipes
ls
Output
omelette.txt  pancakes.txt
$
git init
Output
Initialized empty Git repository in /home/sam/recipes/.git/

That's it — the folder is now a repository. The files themselves haven't changed; Git simply created a hidden .git folder next to them:

$
ls -a
Output
.  ..  .git  omelette.txt  pancakes.txt

The .git folder

Everything Git knows about the project lives in .git: every commit, every branch, your local settings. The files you edit — everything outside .git — are called the working tree.

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

You'll never need to edit these files by hand (lesson 42 takes a look inside). Two things are worth knowing now:

  • Deleting .git deletes the history. The current files stay, but every commit is gone. Moving or copying the project folder is fine — the history travels with it.
  • One repository per project. Create the repository in the project's top folder. Git commands work from any subfolder inside it; Git walks up the folder tree until it finds .git.

Checking the state with git status

git status is the command you'll run most often. It describes where you are and what has changed:

$
git status
Output
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	omelette.txt
	pancakes.txt

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

The repository has no commits yet, and Git sees two files it isn't tracking. Nothing is saved until you add the files and commit them — the topic of the next few lessons.

Outside a repository

Git commands that need a repository fail politely when you're not in one:

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

"Not a git repository" almost always means you're in the wrong folder. Use pwd to check, and cd into the project.

Starting a project from scratch

git init can also create the folder for you:

$
git init shopping-list
cd shopping-list
git status
Output
Initialized empty Git repository in /home/sam/shopping-list/.git/
On branch main

No commits yet

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

Don't nest repositories

Running git init inside a folder that is already part of a repository creates a second, separate repository inside the first. The outer one then sees the inner folder as a mystery and won't track its files properly. If that happens by accident, delete the inner .git folder. (When you genuinely want one repository inside another, lesson 46 shows submodules.)

Likewise, never run git init in your home folder — Git would try to track everything you own.

Bare repositories

You may see git init --bare in server documentation. A bare repository has the history but no working tree: no one edits files in it directly. Hosting services like GitHub store bare repositories, and you exchange commits with them.

Try it yourself

Create a folder called journal with one text file in it, turn it into a repository, and run git status. Then look at the hidden files with ls -a and confirm the .git folder is there.