Menu
Coddy logo textTech

Git Init

Last updated

git init turns the current folder into a Git repository by creating the hidden .git directory that stores all history. It's the first command you run when starting version control on a new project. After initializing, you add files, make your first commit, and optionally connect the repo to a remote.

Try these in the terminal playground - a real shell in your browser, nothing to install.

git init turns a plain folder into a repository: the new .git directory will hold every commit that follows.

Syntax

CommandWhat it does
git initInitialize a repo in the current folder
git init myprojectCreate a folder and initialize it
git init -b mainInitialize with main as the first branch
git init --bareCreate a bare repo (for hosting, no working tree)

Starting a project from scratch

Init, first commit, and connect to a remote.

StepCommandResult
1git init -b mainNew repo with a main branch
2git add .Stage all files
3git commit -m "Initial commit"First commit
4git remote add origin <url>Connect to a remote like GitHub

Git init FAQ

What does git init do?
It creates a new, empty Git repository in the current directory by adding a hidden .git folder where Git stores all commits, branches, and configuration. From that point on, Git tracks the folder's contents. Nothing is committed yet - you still stage files and make your first commit.
How do I set the default branch to main?
Run git init -b main to initialize with main as the first branch. To make it the default for all new repos, set it globally once: git config --global init.defaultBranch main. Older Git versions default the first branch to master.
How do I connect a new repo to GitHub?
After git init and your first commit, run git remote add origin <url> with your repository's URL, then git push -u origin main to push and set the upstream. Create the empty repository on GitHub first so the URL exists.
What's the difference between git init and git clone?
git init starts a brand-new empty repository from local files. git clone <url> copies an existing remote repository - including all its history - to your machine and sets up the remote automatically. Use init for a new project, clone to work on an existing one.
Can I practice this online?
Yes. Open the terminal playground to run git init in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers starting a repository step by step.
Coddy programming languages illustration

Learn Git with Coddy

GET STARTED