GitInitialize a Repository (git init)

Initialize a Repository (git init)

git init is the command that turns a regular folder into a Git repository. It is the very first Git command anyone learns. The moment you run it, Git creates a hidden .git/ subfolder which will hold all your project’s history from that point onward.

The simplest possible use

Turn the current folder into a Git repo

Bash
mkdir my-project
cd my-project
git init
# Initialized empty Git repository in /Users/you/my-project/.git/

That single line did everything that needed to happen:

  • Created the .git/ directory.

  • Inside .git/, created the basic object database (objects/, refs/, etc.).

  • Wrote a default config (.git/config) and HEAD pointer (.git/HEAD).

  • Picked a default branch name — main if you configured init.defaultBranch, else master on older Git.

What is inside .git?

A fresh .git folder

Bash
ls -A .git/
# HEAD             ← which branch you're on
# config           ← repo-level settings
# description      ← only used by GitWeb
# hooks/           ← scripts that run on Git events
# info/            ← extra exclude rules
# objects/         ← every commit, tree, and blob lives here
# refs/            ← branch and tag pointers

You can technically peek inside .git/, but you should never edit these files by hand. Git commands are your interface.

Warning
Never delete the `.git/` folder unless you really want to throw away your project’s entire history. Once it is gone, it is gone.
Common variations of git init

git init in a specific directory

Bash
# Initialize at a given path (creates the folder if it does not exist)
git init my-new-project

# Pick a branch name explicitly
git init --initial-branch=main my-project
# Or short flag
git init -b main my-project
Initialising an existing project

It is perfectly fine to run git init in a folder that already contains code. Git happily picks up any existing files — but it does not start tracking them until you explicitly add them.

Take an existing project under Git control

Bash
cd existing-project
git init
git status
# All your files appear as "untracked"

git add .                    # stage everything
git commit -m "Initial commit"
Re-initialising an existing repo

Running git init inside a folder that is already a Git repo is safe — Git will not destroy your history. It only refreshes missing template files.

Bash
cd already-a-repo
git init
# Reinitialized existing Git repository in /Users/you/already-a-repo/.git/
Bare repositories

A bare repository has no working tree — only the .git contents at the top level. Bare repos are used as central “server” repositories that people push to.

Create a bare repo

Bash
git init --bare my-project.git
  • By convention bare repos end in .git (e.g., my-project.git).

  • You cannot edit files in a bare repo — there is no working tree.

  • You can push to and clone from a bare repo. GitHub, GitLab, etc. host bare repos under the hood.

Customising the template

Each new repo starts from a template directory (defaults to /usr/share/git-core/templates). You can supply your own — useful for shipping pre-installed hooks or a default config.

Bash
git init --template=/path/to/my-template my-new-project
Choosing the default branch name

New Git installs default to main. If you are on a slightly older Git, the default is still master. To make every new repo use main automatically:

Bash
git config --global init.defaultBranch main
git init vs git clone
  • git init — create a brand-new, empty repo right here.

  • git clone <url> — copy an existing remote repo to your machine.

Use git init to start something new from scratch. Use git clone to start working on something that already exists elsewhere.

A complete first-session example

From empty folder to first commit

Bash
mkdir hello-git
cd hello-git
git init
echo "# Hello Git" > README.md
git add README.md
git commit -m "Initial commit"
git log
# commit 1f9a... (HEAD -> main)
# Author: Your Name <you@example.com>
# Date:   Mon Jan  1 10:00:00 2024
#
#     Initial commit
The first commit feels disproportionately good
Once you have made your first commit, you have a real Git repository with a real history. Everything else in this tutorial — branches, remotes, merging, rebasing — is just operating on that same data structure.
Tip
Many editors (VS Code, JetBrains IDEs, GitHub Desktop) have a “Initialize Repository” button. Behind the scenes they run `git init` for you. Use whichever feels comfortable — the result is identical.