Session 4 · Initialize a repository#
The big idea#
git init turns any folder into a Git repository by creating a hidden .git folder inside it. From then
on Git can watch that folder. Nothing is saved yet: a new repository is empty until you make your first commit.
Everyday example: opening a new notebook. The notebook exists and has a cover (the .git folder), but
there is nothing written in it. git status is you flipping through it to see what is inside and what is
not recorded yet.
Create MyProject#
mkdir MyProject
cd MyProject
git init
echo '--- is it a repository now?'
git status
echo '--- what did init create?'
ls -A
ls .git
echo '--- the default branch name is a setting'
git config --global init.defaultBranch main
git init -q ../Another
git -C ../Another branch --show-current
echo '--- running git in a folder that is not a repository'
cd /
git status 2>&1 | head -1
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint: git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint: git branch -m <name>
Initialized empty Git repository in ~/work/MyProject/.git/
--- is it a repository now?
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
--- what did init create?
.git
branches
config
description
HEAD
hooks
info
objects
refs
--- the default branch name is a setting
main
--- running git in a folder that is not a repository
fatal: not a git repository (or any of the parent directories): .git
What happened:
The long
hint:block appears when you have not chosen a default branch name. Git usedmaster, as you can see inOn branch master. The hint tells you how to change it. Rungit config --global init.defaultBranch mainonce and every new repository starts onmain.Initialized empty Git repositoryconfirms.gitwas created.git statussaysNo commits yetandnothing to commit. The repository is valid, just empty.ls -Ashows that the only thinginitadded is.git. Inside it liveHEAD(which branch you are on),config(this repository’s settings),objects(the stored snapshots) andrefs(branch names).After the setting, the second repository’s branch is
main.Outside a repository,
git statusfails withfatal: not a git repository. That message simply means you are in the wrong folder.
What lives inside .git#
You rarely need to open .git, but knowing what is in it removes the mystery. objects holds every
snapshot you will ever commit, stored compactly and addressed by hash. refs holds the names (branches and
tags) that point at those snapshots. HEAD is a one-line file saying which branch you are on. config is
the local settings file from Session 2. Because all of this sits in one folder, you can copy a project with its
whole history just by copying the folder, and you can delete the history by deleting .git.
The files next to .git (your actual project files) are called the working folder. Git compares the working
folder with what is stored in .git every time you run git status.
Where to run git init#
Run it once, in the top folder of the project, not in every subfolder and never in your whole home
directory. A repository inside another repository confuses Git, and an accidental git init in ~ makes
git status list everything you own.
Common mistakes#
git initinside an existing repository (or a subfolder of one). You create a nested repository. Check withgit statusfirst.Running
git initin the wrong folder, such as your home or Downloads folder. Delete the stray.gitfolder.Forgetting the first commit. Until you commit,
git logfails with “does not have any commits yet”.Confusing
git initwithgit clone. Init starts a new empty history; clone copies an existing one.Ignoring the branch-name hint.
masterandmainmixed across projects confuses scripts. Pick one default.
Hands-on exercises#
Exercise 1. Create MyProject, initialise it and confirm with git status.
Solution
mkdir MyProject
cd MyProject
git init
git status
Expected: Initialized empty Git repository ... then On branch main (or master), No commits yet.
Exercise 2. Make a second repository Other and a project-specific identity ProjectUser just for it. Prove that MyProject is unaffected.
Solution
git init Other && cd Other
git config user.name "ProjectUser"
git config user.email "projectuser@example.com"
cd ../MyProject
git config user.name # prints your global name, not ProjectUser
Local settings live in each repository’s own .git/config.
Exercise 3. Find out which branch a new repository starts on and change the default for all future ones.
Solution
git init try1 && git -C try1 branch --show-current
git config --global init.defaultBranch main
git init try2 && git -C try2 branch --show-current
Before the setting an older Git prints master (as in the output above); after it, main.
Practice#
Take the Session 4 quiz. The original Q1 Initialize a Repository and Basic Configurations page is in the reference section.