Creating a repository
Overview
A repository is a project folder whose history Git tracks. There are two ways to get one:
- Create a new one from a folder on your computer —
git init(this lesson). - Copy an existing one, history and all —
git clone(Cloning and fetching).
git init
Go to your project folder and run:
cd portfolio
git init -b mainInitialized 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:
git init -b main recipes # creates recipes/ with a repository insideInside .git
ls -a
# . .. .git about.html index.htmlThe .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).
.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
.gitby hand. Use Git commands. - Deleting
.gitdeletes 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 withrm -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.
git statusOn 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
mainbranch. - 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:
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:
cd css
git rev-parse --show-toplevel
# /home/asha/portfolioOutside any repository, Git commands complain:
fatal: not a git repository (or any of the parent directories): .gitThat 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 initin a subfolder of an existing repository, you get a nested repository that the outer one can’t track properly. Rungit rev-parse --show-toplevelfirst if you’re unsure. - Running
git initin 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 inittwice. 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.
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.
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 existsmkdir 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
git init -b main
# Initialized empty Git repository in .../portfolio/.git/
# 2. What does Git think of the files?
git status
# On branch main
# No commits yet
# Untracked files:
# about.html
# index.html
# 3. From inside css/, ask Git where the top of the repository is
cd css
git rev-parse --show-toplevel # → .../portfolio
cd ..
# 4. Confirm the .git folder exists
ls -a # . .. .git about.html css index.htmlSummary
git initturns the current folder into a repository by creating a hidden.gitdirectory..githolds the entire history and settings — delete it and the folder is no longer a repository.git init -b mainnames the first branchmainregardless of your Git version’s default.git statusis 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.