CH 07 / 75Part 2: Git Basics — Working Locally

Creating a Repository (git init)

What you will learn on this page

  • You can turn a folder into a repository with git init
  • You'll understand what the .git folder is and why you must not delete it
  • You'll know where you should not run git init
  • You can check whether you're currently inside a repository
On this page10 items

In Part 2, #06, you created a practice folder, ~/git-practice. At this point, it’s still just a folder. On this page, you’ll turn it into a repository where Git can record history.

In a plain folder, Git does nothing#

First, check the state before the change.

text5 lines
cd ~/git-practice
git status

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

Git says “not a repository (no .git found even when following the parent folders).” Git treats only places that contain a folder named .git as repositories. It doesn’t start recording in just any folder on its own.

Turn it into a repository with git init#

text4 lines
git init

# Output:
# Initialized empty Git repository in /Users/taro/git-practice/.git/

This message means “initialized an empty Git repository.” The path shown will be the location of your own folder.

Run git status again, and this time you get an answer.

text8 lines
git status

# Output:
# On branch main
#
# No commits yet
#
# nothing to commit (create/copy files and use "git add" to track)

The first line, On branch main, shows that the default branch name you set in Part 2, #06 is being used. No commits yet means “there are no records yet.” The repository has been created, but its history is empty.

What was added: the .git folder#

All git init actually did was create a single folder named .git. Folders whose names start with . are hidden, so check with ls -a (-a means show hidden files too).

text6 lines
ls -a

# Output:
# .
# ..
# .git

. stands for the current folder and .. for the folder one level up; every folder has them. The only thing added is .git.

text7 lines
git-practice/          ← The folder you work in. Put your own files here
└── .git/              ← The repository itself. All history and settings live here
    ├── HEAD
    ├── config
    ├── objects/
    ├── refs/
    └── …

This is an important point for understanding Git. The “repository” is really the .git folder; the git-practice/ around it is just a workspace for editing files. Every time you commit from now on, the records pile up inside .git.

You never edit the files inside .git by hand. You’ll open each of them and see what it does in Part 3, #20 (a tour of the .git folder). For now, it’s enough to remember that “all the history is in here.”

Where you shouldn’t run git init#

git init turns the folder you’re currently in into a repository. Always check where you are before running it. You can display your current location with pwd.

text4 lines
pwd

# Output:
# /Users/taro/git-practice

Don’t run it in your home folder#

The most common accident is running git init in your home folder (~), right after opening Terminal. Your entire home folder becomes a repository, and every folder under it is treated as “inside some repository.” Every time you run git status, a huge list of the contents of your Desktop and Documents folders shows up.

Don’t create another one inside a repository#

If there’s no .git in the current folder, Git searches upward through the parent folders. That’s why you can use Git in a subfolder of a repository without running git init.

text7 lines
mkdir notes
cd notes
git status

# Output:
# On branch main
# …

If you run git init again here, you create a separate repository inside the repository, and the outer repository stops recording its contents. The rule is: one project, one .git, in the topmost folder only.

You can check which folder is the top of the repository with the following command.

text4 lines
git rev-parse --show-toplevel

# Output:
# /Users/taro/git-practice

It works on existing folders too#

git init isn’t only for empty folders. Even if you run it in a folder that already has files, not a single file is changed or moved. Only .git is added.

However, nothing has been recorded yet at this point. Files enter the history only once you choose which ones to record. That procedure is covered in Part 2, #09.

FAQ#

Q. I ran git init in my home folder.
A. Deleting the .git created in your home folder puts things back. Run ls -a in your home folder, confirm that .git is there and that it’s the one you just created, then delete it with rm -rf ~/.git.

Q. What happens if I run git init twice in the same folder?
A. It displays Reinitialized existing Git repository, but the history isn’t erased. It only fills in any missing files.

Summary#

  • git init creates a .git folder in the current folder, making it a repository
  • The repository is really the .git folder. Delete it and all history is lost
  • Git looks for .git starting from the current folder and working up through the parents
  • Don’t run git init in your home folder or inside an existing repository
  • git init changes nothing about existing files, and doesn’t record them yet either

Next page#

08. Git’s three areas — By what route do files go from “where you work” into “the record”? We’ll look at the single most important diagram for using Git.