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.
Syntax
| Command | What it does |
|---|---|
git init | Initialize a repo in the current folder |
git init myproject | Create a folder and initialize it |
git init -b main | Initialize with main as the first branch |
git init --bare | Create a bare repo (for hosting, no working tree) |
Starting a project from scratch
Init, first commit, and connect to a remote.
| Step | Command | Result |
|---|---|---|
| 1 | git init -b main | New repo with a main branch |
| 2 | git add . | Stage all files |
| 3 | git commit -m "Initial commit" | First commit |
| 4 | git remote add origin <url> | Connect to a remote like GitHub |
Git init FAQ
What does git init do?
.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?
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?
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?
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.