How to Create Git Repository (git init Command with Examples)

How to Create Git Repository (git init Command with Examples)

Create Git Repository (Quick Answer)

Create a new Git repository using git init

Use the git init command to create a new Git repository in your project directory:

text
mkdir my-project
cd my-project
git init

This will initialize an empty Git repository by creating a hidden .git folder.

Create repository and make first commit (copy-paste example)

Follow these steps to create a repository using git init and make your first commit using git commit:

text
mkdir my-project
cd my-project
git init
echo "Hello Git" > file.txt
git add .
git commit -m "Initial commit"

This creates a repository, adds a file, and commits it to version control.

Verify repository creation

To confirm that your repository is created successfully using git status (and inspect changes using git diff):

text
git status

You should see output indicating the current branch and repository state.

You can also verify the .git directory:

text
ls -a

What is Git Repository?

What does a Git repository contain

A Git repository is a storage location where your project files and their version history are tracked using Git. It contains:

  • Project files and directories
  • Commit history
  • Branch information
  • Metadata for version tracking

All this data is managed internally by Git.

Role of .git directory in version control

The .git directory is the core of a Git repository. It stores:

  • Commit objects
  • Branch references
  • Configuration settings
  • Logs and history

Without the .git folder, Git cannot track changes.

Local repository vs remote repository

  • Local repository: Exists on your system and is created using git init
  • Remote repository: Hosted on platforms like GitHub or GitLab and accessed via git clone or git push

git init Command Explained

Syntax of git init command

The git init command is used to initialize a new Git repository.

Basic syntax:

text
git init [directory]

If no directory is provided, Git initializes the current directory.

Initialize repository in current directory

To initialize a repository in the current folder:

text
git init

This creates a .git directory inside the current location.

Initialize repository in a new directory

To initialize a repository in a specific directory:

text
git init my-project

This creates the directory (if it does not exist) and initializes it as a Git repository.

Set default branch during initialization

You can define the default branch name during initialization:

text
git init -b main

This sets main as the initial branch instead of the default master.


How to Create Git Repository (Step-by-Step)

Step 1: Create project directory

Create a new directory for your project:

text
mkdir my-project
cd my-project

This directory will contain your Git repository and project files.

Step 2: Initialize repository using git init

Run the following command:

text
git init

This creates a hidden .git folder and converts your directory into a Git repository.

Step 3: Check repository status

Verify the repository status:

text
git status

This shows the current branch, tracked files, and untracked files.

Step 4: Add files to staging area

Create a file and add it to Git:

text
echo "Hello Git" > file.txt
git add file.txt

You can also add all files using:

text
git add .

Step 5: Create first commit

Commit the staged files:

text
git commit -m "Initial commit"

This creates the first version of your Git repository.


What Happens After git init?

Structure of .git directory

When you run git init, Git creates a .git directory which stores all version control data.

Key directories inside .git:

  • objects/ → Stores all commits and file data
  • refs/ → Stores branch and tag references
  • hooks/ → Contains client-side hooks (scripts)
  • config → Repository-specific configuration

This structure allows Git to track changes efficiently.

HEAD file and branch tracking

The HEAD file points to the current active branch.

Example:

text
ref: refs/heads/main

This tells Git which branch is currently checked out.

Objects and refs directory overview

  • objects/ → Stores content using SHA-1 hashes
  • refs/heads/ → Stores local branch references
  • refs/tags/ → Stores tags

These are core components of Git's internal working.


Create Git Repository in Existing Directory

Initialize repo without deleting existing files

You can initialize Git in an existing project without deleting any files.

Navigate to your project folder and run:

text
git init

All existing files remain unchanged.

Track existing files safely

Add and commit existing files using git add and git commit:

text
git add .
git commit -m "Initial commit from existing project"

This starts version tracking without affecting your data.


Common Issues While Creating Git Repository

git command not found error

If Git is not installed:

text
git: command not found

Install Git using your package manager (e.g., apt, yum, dnf).

Permission issues

If you see permission errors:

  • Ensure you have write access to the directory
  • Avoid running Git commands in restricted system paths

Default branch warning (master vs main)

You may see:

text
hint: Using 'master' as the name for the initial branch

Fix by setting default branch using git config:

text
git config --global init.defaultBranch main

Files not getting tracked

If files are not tracked:

  • Ensure you ran git add
  • Check .gitignore for excluded files

Best Practices for Git Repository Setup

Always configure username and email

Set your identity before committing using git config:

text
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

Use meaningful commit messages

Good commit messages created using git commit help track changes and understand history:

text
git commit -m "Add login feature"

Avoid vague messages like "update" or "fix".

Avoid modifying .git directory manually

Never edit .git files directly.

This can corrupt your repository and break version tracking.


Summary

In this guide, you learned how to create a Git repository using the git init command, which is the foundation of version control in Git. We covered how to initialize a repository, add files, and create your first commit using simple command-line steps.

You also understood how Git internally works with the .git directory, including important components like HEAD, objects, and references. This helps in building a strong conceptual understanding beyond just commands.

Additionally, we explored how to create a repository in an existing project, common errors such as Git not installed or files not being tracked, and how to fix them quickly.

This tutorial gives you a complete beginner-to-intermediate understanding of how to create and manage a Git repository effectively using command line.


Official Documentation

You can learn more about creating Git repositories and similar commands from the official Git documentation:

Deepak Prasad

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.