Back
GitoryxGitoryx

Git Init: Start a New Repository From Scratch

git init turns any folder into a Git repository. This tutorial covers creating the repository, naming the first branch, making your first commit, connecting a remote, and the mistakes that create nested repositories.

Beginner6 min readUpdated

On this page

What is git init?

git init turns the current folder into a Git repository by creating a hidden .git directory. It starts empty: no commits, no remote. From there you stage files with git add, save them with git commit, and optionally connect a remote with git remote add.

The short version

  1. 1Go to your project folder: cd my-project
  2. 2Initialise the repository: git init -b main
  3. 3Check the state: git status
  4. 4Stage your files: git add .
  5. 5Create the first commit: git commit -m "Initial commit"
  6. 6Link a remote: git remote add origin <url>

Why it matters

You have a folder of code that is not under version control yet, and every tutorial you open starts with git clone as if a repository already existed. Or you ran git init in the wrong folder and now Git behaves strangely.

git init is the first command of every project that starts on your machine. It creates the hidden .git folder where Git stores your whole history. Until you run it, nothing you do can be tracked, branched, or shared.

Where it usually goes wrong

  • Running git init inside a folder that is already part of another repository, creating a nested repo
  • Running it in your home directory, so Git suddenly tracks everything you own
  • Forgetting that the default branch name may be master instead of main
  • Expecting git init to create a remote: it only creates a local repository

Git init, step by step

  1. Step 1: Create or open your project folder

    Move into the folder that holds your project. Make sure it is not already inside another repository: git rev-parse --show-toplevel prints the repository root if there is one, and fails if there is none.

    Terminal
    mkdir my-projectcd my-project# Check you are not already inside a repositorygit rev-parse --show-toplevel

    Tip: fatal: not a git repository is the answer you want here: it means the folder is free to initialise.

  2. Step 2: Initialise the repository

    Run git init. Use -b to choose the name of the first branch instead of relying on your Git version's default.

    Terminal
    git init -b main# Initialized empty Git repository in /path/to/my-project/.git/

    Tip: Set it once for all future repositories with git config --global init.defaultBranch main.

  3. Step 3: Check the state

    A fresh repository has no commits. git status shows the branch name and lists your files as untracked.

    Terminal
    git status# On branch main# No commits yet# Untracked files: ...
  4. Step 4: Make your first commit

    Stage your files and save the first snapshot. Add a .gitignore first if your project generates files you do not want to track.

    Terminal
    git add .git commit -m "Initial commit"

    Careful: Run git status before git add . to be sure no secrets (.env, keys) or build folders (node_modules) are about to be staged.

    Git Init: Start a New Repository From Scratch: the same workflow in Gitoryx
    In Gitoryx: Open any folder you initialised with git init: Gitoryx recognises an empty repository with no commits yetHow it works
  5. Step 5: Connect a remote and push

    Create an empty repository on GitHub, GitLab, or elsewhere, then link it as origin and push your branch.

    Terminal
    git remote add origin git@github.com:you/my-project.gitgit push -u origin main

    Tip: -u sets the upstream so later git push and git pull need no arguments.

  6. Step 6: Undo an accidental init

    Everything Git knows lives in .git. Removing that folder returns the directory to a plain folder; your files are untouched.

    Terminal
    # Make sure you are in the right folder first!pwdrm -rf .git

    Careful: This deletes the entire history of the repository. Only do it on a repo you just created by mistake.

Mistakes to avoid with git init

Initialising inside an existing repository
A second .git inside a project creates a nested repository. The parent sees it as an opaque folder and your commits end up in the wrong history.
Fix: Run git rev-parse --show-toplevel before git init. If it prints a path, you are already in a repo; remove the nested .git if it was a mistake.
Running git init in your home folder
Git then tries to track every file you own, and git status becomes unusably slow.
Fix: Delete ~/.git and initialise inside the project folder only.
Committing everything with git add . right away
Without a .gitignore, dependencies, build output, and .env files end up in your very first commit, where they are painful to remove later.
Fix: Create a .gitignore before the first git add, and review git status.

Git init in Gitoryx

Open a freshly initialised folder in Gitoryx and it shows an empty-repository screen with an 'Initialize with README' button that creates your first commit for you.

  • Open any folder you initialised with git init: Gitoryx recognises an empty repository with no commits yet
  • Click 'Initialize with README' to create the first commit in one step
  • Watch the graph start with a single node, then stage your own files and commit as usual
  • Add a remote and push from the same window

Free plan · no account required · macOS, Windows and Linux

Git init: frequently asked questions

What does git init do?
It creates a hidden .git folder in the current directory, which stores the history, branches, and configuration of a new local repository. It does not upload anything anywhere.
What is the difference between git init and git clone?
git init creates a brand new empty repository from a local folder. git clone downloads an existing repository, including its full history, and sets up the remote for you.
Can I run git init twice?
Yes. On an existing repository it prints Reinitialized existing Git repository and changes nothing about your history.
How do I rename master to main after git init?
Run git branch -m main. To avoid it in future, set git config --global init.defaultBranch main.
Do I need git init if I clone a repository?
No. git clone already initialises the repository for you.
All Git tutorials →
Gitoryx

Stop fighting Git. Start shipping.

Download the fast, native Git client that developers actually enjoy using.

v1.1.0 · macOS 12+ (Apple Silicon or Intel) · Windows 10+ · Linux x86_64