Git Course 0%

git init

Beginner Git CLI Safe ≈ 5 min

Summary: git init creates an empty repository in the current folder (or a named one) by adding a hidden .git directory. The existing files are untouched and untracked until you add and commit them.

Safe — it creates .git and nothing else; running it twice in the same folder is harmless ("Reinitialized existing Git repository").

What it does

Creates .git/ with an empty object store, an empty staging area, a HEAD pointing at the default branch (main if init.defaultBranch is set, otherwise master), and no commits. The folder becomes the working tree of the new repository.

Why it exists

Every repository starts somewhere. git init is the "somewhere" for a project that does not yet exist on a remote; everything else in Git assumes .git is there.

When to use it

  • Starting a brand-new project on your computer.
  • Turning an existing folder of files (documents, scripts, the playground) into a repository.
  • Creating a bare repository on a server (--bare), rarely done by hand.

When not to use it

  • To get a project that already exists on GitLab or GitHub: use git clone, which also sets up the remote.
  • Inside another repository (nested repositories confuse tools), or in your home folder (everything you own becomes untracked).

Syntax

Form Meaning
git init Initialize in the current folder
git init <folder> Create the folder if needed and initialize inside it
git init -b main Choose the initial branch name (Git 2.28+); unnecessary if init.defaultBranch is set
git init --bare A repository without a working tree, for servers

Examples

Terminal
$ cd ~/git-practice/trailguide
$ git init
Initialized empty Git repository in /Users/you/git-practice/trailguide/.git/
Terminal
$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

A folder that does not exist yet:

Terminal
$ git init ~/git-practice/glossary

Then the first commit:

Terminal
$ git add .
$ git commit -m "Initial import of trailguide"
[main (root-commit) 216f956] Initial import of trailguide
 12 files changed, 324 insertions(+)

Expected result

A .git folder (visible with ls -a), git status reporting No commits yet, and every existing file listed as untracked. No file content changes.

Common mistakes

  • The branch is called master. No init.defaultBranch configured. git branch -m main renames it now; git config --global init.defaultBranch main fixes the future.
  • Running it in the wrong folder. Check pwd first. If you initialized your home folder by mistake, delete the .git folder that was just created there (the only time deleting .git is right).
  • Expecting a remote. git init knows nothing about GitLab; add one with git remote add origin <url> (Lab 2).

How to undo or recover

Delete the .git folder that git init created, if there are no commits you care about: rm -rf .git. Your files remain exactly as they were.

In VS Code

Source Control → Initialize Repository (shown when the open folder is not a repository). See Set up a safe playground.

In IntelliJ IDEA

VCS → Create Git Repository… (older versions: Enable Version Control Integration → Git). See Install, Git executable, enable VCS.

git clone gets an existing repository · git add and git commit make the first snapshot · git remote connects the new repository to a server.

Lessons that use this command

init vs clone · Set up a safe playground · Lab 1.

Key terms

Repository (repo) Working tree main