git init
Beginner Git CLI Safe
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
$ cd ~/git-practice/trailguide
$ git initInitialized empty Git repository in /Users/you/git-practice/trailguide/.git/$ git statusOn branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)A folder that does not exist yet:
$ git init ~/git-practice/glossaryThen the first commit:
$ 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. Noinit.defaultBranchconfigured.git branch -m mainrenames it now;git config --global init.defaultBranch mainfixes the future. - Running it in the wrong folder. Check
pwdfirst. If you initialized your home folder by mistake, delete the.gitfolder that was just created there (the only time deleting.gitis right). - Expecting a remote.
git initknows nothing about GitLab; add one withgit 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.
Related commands
git clone gets an existing repository · git add and git commit make the first snapshot · git remote connects the new repository to a server.