Sub topic 3
Creating a repository with git init
This chapter assumes you can use a terminal (cd, mkdir, ls), can edit plain text files, and have Git installed. Check the install with git --version.
What git init does
A Git repository is a project directory plus a history of its changes. git init creates that history store. It makes a hidden directory named .git inside the current directory. Git keeps everything it records in .git, including commits, settings, and the pointer to your current branch. Your own files stay where they are.
mkdir hello-git
cd hello-git
git initGit prints a message like this:
Initialized empty Git repository in /home/ada/hello-git/.git/The word "empty" means there are no commits yet. The directory holding .git is called the working tree. Git tracks files below that directory.
Looking inside
List the hidden directory to see what Git created.
ls -a
ls .gitYou will see entries such as HEAD, config, objects, and refs. You do not edit these by hand. Two are worth knowing now. HEAD names the branch you are on, and objects will hold your commits and file contents once you start committing.
Deleting .git removes the history and leaves the working files alone. The directory becomes an ordinary folder again.
Choosing the first branch name
A new repository starts on a default branch. Older Git versions call it master. Newer setups often use main, depending on the init.defaultBranch setting. To pick the name explicitly, use -b (Git 2.28 or later):
git init -b mainCheck which branch you are on with git branch --show-current. A brand-new repository shows its branch name here even before the first commit.
Initializing an existing project
You can run git init in a directory that already contains files. Git does not track them automatically. They show up as untracked until you stage them, which the next chapters cover.
You can also create the directory and repository in one step:
git init hello-gitCommon errors
- Initializing in the wrong place. Running
git initin your home directory makes Git treat everything beneath it as one project. Runpwdfirst. If you did it by mistake, remove the stray.gitdirectory in that location. - Nesting repositories. If you run
git initinside a directory that is already part of a repository, you create a second, separate repository. The outer one then treats the inner directory in a confusing way. Check withgit rev-parse --show-toplevelbefore initializing. If it prints a path, you are already inside a repository. - Running it twice. This is safe. Git reports
Reinitialized existing Git repositoryand keeps your history.
Exercise
You run git init in ~/projects/app, and later notice that git status also works in ~/projects/app/docs. You then run git init inside docs. What have you created, and how could you have avoided it?
Show answer
You created a second, separate repository nested inside the first, with its own .git directory and history. You could have avoided it by running git rev-parse --show-toplevel in docs first. That command prints the root of the repository you are already in, which shows that docs did not need its own.