Initializing a Repository
Initializing a Repository
After this page you have an empty Git repository on main and you know what git init created.
A directory becomes a repository
mkdir -p ~/desk/desk-api
cd ~/desk/desk-api
git initOutput looks like:
Initialized empty Git repository in /home/ada/desk/desk-api/.git/
If you set init.defaultBranch main (chapter 2), the empty branch is named main. Confirm:
git statusOn branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
~/desk/desk-api is the working tree. ~/desk/desk-api/.git/ is the repository (objects, refs, config). You edit files next to .git, not inside it.
~/desk/desk-api/ working tree (you edit these)
├── README.md
├── src/
└── .git/ repository
├── HEAD ref: refs/heads/main
├── config
├── objects/ empty until first commit
└── refs/heads/ main appears after first commit
This book’s recurring desk is three repos: desk-web, desk-api, desk-infra. Part I uses desk-api as the tiny project. Create the other two the same way when a later chapter asks.
What git init wrote
ls -a .gitYou will see at least config, HEAD, objects/, refs/. HEAD currently says ref: refs/heads/main even though refs/heads/main does not exist yet. The first commit creates that file.
cat .git/HEAD
git rev-parse --git-dir
# .git--git-dir is useful when you are in a subdirectory later.
init vs clone
| Command | When |
|---|---|
git init |
You are starting a project that has no Git history yet |
git clone <url> |
You are copying an existing repository, including its history |
clone is chapter 11. For a brand-new desk project, init is the right verb.
Already have files?
git init in a directory that already has source files is fine. Git does not add them until you git add. The next page is that loop.
init in the wrong place
If you run git init in your home directory, every git status from a subdirectory without its own .git will try to use ~/.git. Remove a mistaken repo with:
# only if you meant to throw it away and there are no commits you need
rm -rf .gitThat deletes Git’s data for that directory, not your files. The first-commit hands-on page builds desk-api from scratch so you can see a clean init.