Git Init Explained: What It Does, Examples, and When to Use It

Deepak Prasad

Git init is a fundamental Git command used to initialize a new repository and start tracking files. If you're wondering what git init does, it creates a hidden .git directory that enables version control in your project. In this guide, you'll learn how to use git init, real examples, and what to do after git init as part of a complete Git workflow.


Git init cheat sheet (Quick Reference)

Task Command
Initialize a new repository git init
Initialize in a named directory git init my-project
Initialize with main as first branch git init -b main
Initialize bare repository (for server) git init --bare
Reinitialize existing repository git init
Check repository status git status
Add all files to staging git add .
Commit changes git commit -m "message"
Rename default branch to main git branch -M main
Add remote repository git remote add origin <url>
Push to remote repository git push -u origin main

What is git init (Simple Explanation)

git init is a core Git command used to initialize a new repository and enable version control in a project. When you run git init, Git creates a hidden .git directory that stores all metadata required to track changes. In simple terms, git init is the first step to start using Git for managing your code.

What does git init do

The git init command creates an empty Git repository in your current directory. It sets up the internal structure required for version control, including configuration files, object storage, and references. Once initialized, Git can track file changes, manage commits, and support branching and merging.

Why git init is required

You must run git init before using any Git commands like git add or git commit. Without initializing a repository, Git cannot track files or maintain history. It acts as the foundation of the Git workflow, enabling all version control operations in both local and remote repositories.


What happens when you run git init

When you execute git init, Git prepares your directory to become a repository by creating internal structures and default configurations.

Creation of .git directory explained

The most important outcome of git init is the creation of a hidden .git folder. This directory contains all repository data, including commit history, branches, configuration, and object database. It is the core of Git and should not be modified manually.

Inside .git you will find the pieces Git relies on to track your project:

  • objects/ — stores every commit and file version, addressed by hash
  • refs/heads/ — one file per local branch, holding the commit it points at
  • refs/tags/ — the same idea for tags
  • hooks/ — sample client-side scripts you can enable
  • config — settings that apply to this repository only
  • HEAD — a single line naming the branch you are currently on, such as ref: refs/heads/main

Reading HEAD is the quickest way to confirm which branch a fresh repository started on.

How Git starts tracking files

After running git init, Git does not automatically track files. Instead, it prepares the environment so you can start tracking files using git add. Once files are added and committed using git commit, Git begins maintaining their history inside the .git directory.


Initialize a new Git repository locally

Step-by-step git init example

Create a new directory and navigate into it:

text
mkdir my-project
cd my-project

Initialize the Git repository:

text
git init

You will see a message confirming that an empty Git repository has been initialized.

Create some files:

text
touch file1.txt file2.txt

At this point, the files exist but are not yet tracked by Git.

Verify repository using git status

Run the following command to check repository status:

text
git status

This command confirms that (you can further inspect changes using git diff):

  • The directory is now a Git repository
  • Files are untracked and ready to be added

What to do after git init

After running git init, you need to follow the standard Git workflow to start tracking and managing your project.

Add files using git add

Stage files for tracking using:

text
git add .

This command tells Git to start tracking all files in the directory using git add.

Commit changes using git commit

Save the staged files into Git history:

text
git commit -m "Initial commit"

This creates the first commit in your repository using git commit.

Connect to remote repository

To push your local repository to a remote server like GitHub:

text
git remote add origin <repository-url>
git branch -M main
git push -u origin main

This step links your local repository to a remote repository using git remote add and uploads your code using git push.


git init vs git clone (Key Differences)

git init and git clone are two commonly used Git commands, but they serve different purposes. Understanding the difference between git init vs git clone helps you choose the right workflow when starting a project.

  • git init creates a new empty repository in your current directory.
  • git clone copies an existing repository along with its history from a remote source.

When to use git init

Use git init when:

  • You are starting a new project from scratch
  • You want to convert an existing directory into a Git repository
  • You need full control over repository setup before connecting to a remote

Example:

text
git init

When to use git clone

Use git clone when:

  • You want to copy an existing project from GitHub, GitLab, or another remote server
  • You need the complete commit history and branches
  • You are contributing to an existing repository

Example:

text
git clone https://github.com/user/repo.git

Use git init with remote repository

You can use git init to create a local repository and then connect it to a remote repository like GitHub using git remote add.

Initialize repo and push to GitHub

Initialize repository:

text
git init

Add files and commit:

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

Connect to remote repository:

text
git remote add origin https://github.com/user/repo.git

Push code to remote using git push:

text
git branch -M main
git push -u origin main

Fix common remote origin issues

If you see error:

text
error: remote origin already exists

Fix it by removing and re-adding remote using git remote remove and git remote add:

text
git remote remove origin
git remote add origin https://github.com/user/repo.git

Verify remote using git remote:

text
git remote -v

Reinitialize an existing repository

Run git init again safely

If you run:

text
git init

inside an existing repository, Git will reinitialize it without deleting commits or history.

You may see:

text
Reinitialized existing Git repository in /path/.git/

When reinitialization is useful

  • Fix corrupted repository configuration
  • Restore missing Git settings
  • Recreate .git directory metadata (in some recovery cases)

Customize repository using git init options

Git init provides several options to customize how a repository is created.

Use --bare for remote repositories

Create a bare repository (no working directory), commonly used for servers:

text
git init --bare

This is useful for shared repositories where multiple users push and pull changes.

Use -b to name the initial branch

Set the starting branch name as you initialize, instead of renaming it afterwards:

text
git init -b main

This creates the repository with main as the first branch, so you never see the master default at all.

Git also accepts a directory name, which it creates if it does not already exist:

text
git init my-project

If you would rather not pass -b on every new repository, set it once globally with git config:

text
git config --global init.defaultBranch main

That also silences the hint: Using 'master' as the name for the initial branch warning on older Git versions.

Use --template for predefined structure

Initialize repository with predefined templates:

text
git init --template=/path/to/template

Templates can include hooks, configuration files, and directory structure.

Use --separate-git-dir for advanced setups

Store .git directory separately from the working directory:

text
git init --separate-git-dir=/path/to/git-dir

This is useful for advanced setups like shared storage or custom repository layouts.


Frequently Asked Questions

1. What is git init?

git init is a command used to create a new Git repository. It initializes a .git directory that enables Git to start tracking files.

2. What does git init do?

git init creates a hidden .git directory that stores all repository metadata and allows Git to track changes in your project.

3. What to do after git init?

After git init, you should add files using git add, commit them using git commit, and optionally connect to a remote repository using git remote add.

4. What is the difference between git init and git clone?

git init creates a new empty repository, while git clone copies an existing repository along with its history.

5. Can I run git init multiple times?

Yes, running git init again reinitializes the repository without affecting existing files or commits.

6. Where does git init store data?

git init stores all repository data inside a hidden .git directory in your project folder.

7. What is inside the .git folder?

The .git folder holds objects for every commit and file version, refs for branches and tags, a hooks directory of sample scripts, a repository-specific config file, and a HEAD file naming the branch you are currently on.

8. How do I create a Git repository with main as the default branch?

Run git init -b main to name the first branch as you initialize. To make it the default for every new repository, run git config --global init.defaultBranch main.

9. Is git init required before git add?

Yes, you must run git init before using git add because Git needs a repository to track files.

10. How to initialize a remote Git repository?

You typically initialize locally using git init and then connect to a remote repository using git remote add and push changes.

Summary

git init is used to create a new Git repository, while git clone is used to copy an existing repository. Understanding when to use git init vs git clone helps you build efficient Git workflows. After running git init, you can add files, commit changes, and connect to a remote repository to start managing your code effectively.


Official documentation

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly.

  • Git
  • JavaScript
  • Laravel
  • Python (programming language)
  • Machine Learning