Creating a repository

Topic 1 of 5 in Your first repository6 min readBeginnerFree course

Overview

A repository is a project folder whose history Git tracks. There are two ways to get one:

  1. Create a new one from a folder on your computer — git init (this lesson).
  2. Copy an existing one, history and all — git clone (Cloning and fetching).

git init

Go to your project folder and run:

Terminal
cd portfolio
git init -b main
Text
Initialized empty Git repository in /home/asha/portfolio/.git/

That’s it: the folder is now a repository. Nothing about your files has changed — Git has only created one hidden folder, .git.

-b main (short for --initial-branch) names the first branch main. If you set init.defaultBranch main in the previous lesson, a plain git init does the same.

To create the folder and the repository in one go:

Terminal
git init -b main recipes     # creates recipes/ with a repository inside

Inside .git

Terminal
ls -a
# .  ..  .git  about.html  index.html

The .git directory is the repository: every commit, every branch, the staging area and the repository’s settings. The rest of the folder — the files you edit — is called the working directory (or working tree).

Text
.git/
├── HEAD          which branch you're on
├── config        settings for this repository (git config --local)
├── objects/      every version of every file, compressed
├── refs/         branches and tags
└── index         the staging area (appears after your first git add)

You’ll explore objects/ and refs/ in How Git works. For now, two practical rules:

  • Don’t edit files inside .git by hand. Use Git commands.
  • Deleting .git deletes the history. The folder becomes ordinary files again. Occasionally that’s what you want (to start over); mostly it’s a disaster, so be careful with rm -rf.

git status: your dashboard

git status tells you where you are and what has changed. Run it constantly — before and after every other command while you’re learning.

Terminal
git status
Text
On branch main

No commits yet

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

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

Read it line by line:

  • On branch main: you’re on the main branch.
  • No commits yet: the history is empty.
  • Untracked files: files in the folder that Git isn’t tracking yet. Git has noticed them but won’t record them until you say so.

Git’s hints in brackets tell you the command for the next step. They’re genuinely useful — read them.

For a compact version:

Terminal
git status --short     # or: git status -s
# ?? about.html
# ?? index.html

?? means untracked. You’ll meet the other codes in the next lesson.

Where is the top of the repository?

Git commands work from any subfolder of a repository — Git walks up the folder tree until it finds .git. To ask where the top is:

Terminal
cd css
git rev-parse --show-toplevel
# /home/asha/portfolio

Outside any repository, Git commands complain:

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

That message means “you’re not in a repository” — usually you’re in the wrong folder.

Common mistakes

  • A repository inside a repository. If you run git init in a subfolder of an existing repository, you get a nested repository that the outer one can’t track properly. Run git rev-parse --show-toplevel first if you’re unsure.
  • Running git init in your home folder. Every file on your account becomes “untracked”, every folder below is inside the repository, and tools get very confused. If you’ve done it, delete ~/.git (only that folder!) to undo it.
  • Running git init twice. Harmless — Git just says “Reinitialized existing Git repository” and changes nothing important.

Next: the files are untracked. To start tracking them you’ll use the staging area — Git’s most important idea for beginners.

Exercise

Start tracking a website project

Create a folder called portfolio with an index.html and an about.html, then turn it into a Git repository whose first branch is main. Use git status to see that both files are untracked, find out which folder is the top of the repository from inside a subfolder, and confirm the hidden .git folder exists. Don’t commit anything yet.

Terminal
mkdir portfolio && cd portfolio
echo "<h1>Hi, I'm Asha</h1>" > index.html
echo "<h1>About me</h1>" > about.html
mkdir css

# 1. Make this folder a Git repository on a branch called main

# 2. What does Git think of the files?

# 3. From inside css/, ask Git where the top of the repository is

# 4. Confirm the .git folder exists

Summary

  • git init turns the current folder into a repository by creating a hidden .git directory.
  • .git holds the entire history and settings — delete it and the folder is no longer a repository.
  • git init -b main names the first branch main regardless of your Git version’s default.
  • git status is the command you’ll run most: it shows the branch and the state of every changed file.
  • Never create a repository inside another repository, and avoid making your home folder a repository.