Skip to main content

Git Init: How to Initialize and Set Up a Git Repository

Learn how to set up a Git repository with git init, connect to a remote, configure settings, and troubleshoot common issues—all in one beginner-friendly guide.
Mar 17, 2025  · 15 min read

Ever deleted a file by mistake or overwritten your code with no way to recover it? Without version control, such errors can be catastrophic. That’s where git init comes in—it transforms your project folder into a Git repository, ensuring that every change is trackable and recoverable. 

In this tutorial, I’ll show you how to set up Git, share personal tips from my journey, and help you create a robust workflow right from the start.

What Is Git Init?

The git init command kick-starts your version control journey. When you run it, Git creates a hidden .git folder inside your project directory that stores configuration data, commit history, and all the metadata needed to track your changes. 

With every modification recorded, you can branch, merge, and collaborate confidently—knowing that your project’s complete history is always just a command away.

The Git initialization process

Image by Author

How to Use Git Init

Before getting into advanced topics, it’s essential to understand how to use git init in practice.

Basic usage of git init

Follow these steps to create a new Git repository from scratch:  

  • Open your terminal: Launch your preferred command-line interface.
  • Navigate to your project directory:
cd /path/to/your/project

This changes your current folder to the one you want to track.

  • Initialize the repository: 
git init

After executing this command, a hidden .git folder is created. That folder stores every bit of Git’s tracking information.

  • Verify the repository status:
git status

At this stage, Git is set up but not tracking any files.

Initializing a repository with an existing project

For projects that aren’t yet under version control, you can initialize Git and start tracking changes as follows:

  • Navigate to your project directory:
cd /path/to/your/project
  • Convert the project to a Git repository:
git init

As mentioned before, the above command creates the hidden .git folder that enables Git tracking.

  • Stage your files: Since git init does not automatically track files, add them with:
git add .
  • Commit your files: Save the initial snapshot by running:  
git commit -m "Initial commit"

This process converts an unmanaged project into one with a complete version history—ready for new development and collaboration.

Configuring Git (if required)

If you encounter the error "Author Identity Unknown," it means Git doesn’t know who you are. To fix it, configure your user identity:

  • Set up Git user information: 
git config --global user.email "your_email@example.com"
git config --global user.name "Your Name"

Using --global applies these settings across all repositories on your machine. Omitting it will set the identity for just the current repository.

  • Verify your configuration:  
git config --global user.email
git config --global user.name
  • Retry the commit:  
git commit -m "First Commit"

I encountered this error when I first began using Git—setting my identity resolved it.

Successful first Git commit in PowerShell after staging files with git add . and committing with git commit -m "first commit".

The initial Git commit ran successfully in PowerShell.

Connecting to a remote repository

Once you’ve initialized a Git repository locally, you’ll likely want to back it up or collaborate with others by connecting it to a remote repository (e.g., GitHub, GitLab, Bitbucket).

  • Add a remote repository: To link your local repository to a remote one, use:
git remote add origin <repository-url>

Replace <repository-url> with the URL of your remote repository. origin is the default alias for the remote repository, but you can use any name.

  • You can verify the connection with:
git remote -v
  • Push your changes to the remote repository:
# Push the first commit and track the remote branch
git push -u origin $(git branch --show-current)  # Works for 'main' or 'master' 

The above command pushes the current branch and sets it to track the remote branch.

If you’re using an older Git version that doesn’t support $(git branch --show-current), use:

git push -u origin main  # Or 'master' depending on your default branch  

As you continue working on your project, you'll need to merge changes from different branches when collaborating with a team. Learn how to handle merges effectively with this Git merge tutorial.

Authentication and access

If pushing to GitHub, GitLab, or Bitbucket, you might need to authenticate. Depending on your setup, you can:

  • Use SSH keys instead of HTTPS authentication (recommended for security):
git remote set-url origin git@github.com:your-username/your-repo.git
  • Use a Personal Access Token (PAT) instead of a password when using HTTPS. For GitHub, replace the password with a token:
git push https://your-username@github.com/your-repo.git

Connecting to a remote repository was a turning point for me—it validated that my local work was safely backed up and ready for collaboration. Plus, it gave me confidence in version control, knowing my history was preserved even if my local machine failed.

Learn Git Fundamentals Today

For beginners: Master version control using Git.
Start Learning For Free

Advanced Options with Git Init

For those wishing to tailor their Git setups, advanced options provide additional flexibility.

Initializing a bare repository

If you're setting up a Git server where developers will push their changes but not edit files directly, you'll need a bare repository. A bare repository omits a working directory and contains only version control data. It’s typically used for remote repositories:

git init --bare <repository-name>

Bare repositories are often used in GitOps workflows, where infrastructure and application deployment are managed through Git. If you’re interested in learning how GitOps automates and streamlines software delivery, check out this guide to GitOps.

Initializing with specific configuration

If you need custom Git hooks or configuration files from the start, initialize with a custom template:

git init --template=<template-directory>

I once used a custom template to load essential hooks automatically, which streamlined our team’s deployment process.

Best Practices for Using Git Init

Starting on the right foot with Git can save you time and troubles down the line.

Organizing your project structure

Before running git init, organize your files into a logical folder structure—separate code, documentation, and assets. 

In one project I inherited, files from multiple subprojects were mixed up, leading to merge conflicts and confusion. By reorganizing everything into clearly defined folders, reinitializing the repository, and setting up a proper .gitignore, the workflow improved dramatically.

Adding a .gitignore file early on

Immediately after initializing your repository, create a .gitignore file to prevent unnecessary files from being tracked:

touch .gitignore

Then, edit it to include rules such as:

# Ignore dependency folders and log files
node_modules/
*.log
.DS_Store  # macOS system file
Thumbs.db  # Windows system file

This step saved me from bulky commit histories and potential future conflicts.

For a detailed guide on configuring a .gitignore file, check out this gitignore tutorial.

Committing frequently

Making small, frequent commits with clear messages helps build a detailed project history and simplifies debugging and collaboration.

Troubleshooting Common Git Init Issues

Even with proper planning, issues can arise. Here are some common problems and remedies.

Mistakenly initializing in the wrong directory

Problem: You ran git init in the wrong folder and now have an unwanted Git repository.

Solution:  

  • Remove the Git repository:
rm -rf .git

> Warning: This deletes all version history for that repository. Be sure you're in the correct folder before running this command!

Then, navigate to the correct directory and run git init again. 

I’ve made this mistake a few times, so double-check your working directory to avoid it.

Dealing with untracked files

Problem: After running git init, Git isn’t tracking your files. Running git status shows:

No commits yet  
Untracked files:
  (use "git add <file>..." to include in what will be committed)

Solution: 

  • Check if the files are truly untracked:
git status

If your files appear under Untracked files, Git sees them but isn’t tracking them yet.

  • Then, stage the necessary files with:
git add <file-name>
  • Commit the files:
git commit -m "Initial commit"

Regular status checks ensure nothing important is overlooked.

If git status shows no files, check if your project contains a .gitignore file that might be excluding important files.

Conclusion

Mastering git init is the crucial first step toward building a reliable version control system that empowers collaboration and efficient project management. Follow this guide—from initializing and organizing your repository to troubleshooting common issues—to set up a smooth, efficient workflow.

Start small—initialize a test project using git init and experiment with staging, committing, and pushing changes. The sooner you master Git’s fundamentals, the more efficient your workflow will be! And if you’re excited to deepen your Git skills even further, check out these DataCamp courses:

  • Intermediate Git – Enhance your skills with advanced branching, merging, and conflict resolution techniques.  
  • GitHub Foundations – Learn how to seamlessly integrate Git with GitHub for collaborative projects.

Happy coding—and here’s to building reliable, innovative projects using Git!

Learn Git Fundamentals Today

For beginners: Master version control using Git.

FAQs

How do I undo git init if I initialized the wrong folder?

Remove the hidden .git folder by running: rm -rf .git(on Unix-based systems) or delete it manually on Windows. Then, navigate to the correct directory and re-run git init.

What’s the difference between git init and git clone?

git init creates a new, empty repository (or adds version control to an existing project), whereas git clone copies an existing remote repository along with its entire history.

How do I check if a directory is already a Git repository?

Open your terminal in that directory and run: git rev-parse --is-inside-work-tree

If it returns “true,” your directory is under Git version control.

Why is the .git folder hidden, and how can I view it?

Git hides the .git folder to protect your project’s version history. To view it, enable hidden files in your file explorer or run:  

ls -a  # on Mac/Linux
dir /A:H  # on Windows

What are common mistakes to avoid immediately after using git init?

Common pitfalls include neglecting to add a proper .gitignore, skipping the staging step with git add, and committing without clear messages. Always verify your repository’s state with git status before proceeding.

How do I initialize Git in a subdirectory of an existing repository?

If you need to track a subproject separately inside a Git repository, use Git submodules instead of running git init in a subdirectory.

git submodule add <repository-url> subdirectory-name

This allows the subdirectory to be managed separately while keeping it connected to the parent repo.


Khalid Abdelaty's photo
Author
Khalid Abdelaty
LinkedIn

Data Engineer with Python and Azure cloud technologies expertise, specializing in building scalable data pipelines and ETL processes. Currently pursuing a B.S. in Computer Science at Tanta University. Certified DataCamp Data Engineer with demonstrated experience in data management and programming. Former Microsoft Data Engineer Intern at Digital Egypt Pioneers Initiative and Microsoft Beta Student Ambassador leading technical workshops and organizing hackathons.

Topics

Learn more about Git with these courses!

Course

Introduction to Git

2 hr
16.9K
Discover the fundamentals of Git for version control in your software and data projects.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

blog

How to Learn Git in 2025: A Complete Guide for Beginners

Learn everything about Git, including its applications, demand in the job market, learning resources, and a step-by-step learning plan to get started.
Laiba Siddiqui's photo

Laiba Siddiqui

Tutorial

Git Setup: The Definitive Guide

In this tutorial, you'll learn how to set up Git on your computer in different operating systems.

Olivia Smith

7 min

Tutorial

Git Install Tutorial

Learn about Git initial setup, Git LFS, and user-friendly Git GUI applications in this in-depth tutorial.
Abid Ali Awan's photo

Abid Ali Awan

9 min

Tutorial

Git Reset and Revert Tutorial for Beginners

Discover how to use Git reset and revert to manage your project history. Practice with detailed examples using soft, mixed, and hard resets. Learn the difference between Git reset and revert.
Zoumana Keita 's photo

Zoumana Keita

10 min

Tutorial

Git Merge Tutorial: A Comprehensive Guide with Examples

Learn how to merge branches efficiently in Git with this step-by-step tutorial. Explore different merge strategies, resolve conflicts, and apply best practices to keep your Git history clean.
Srujana Maddula's photo

Srujana Maddula

15 min

Tutorial

Git Rebase: A Beginner’s Guide to Streamlined Version Control

Learn how to use Git rebase to keep your commit history clean and improve collaboration. This guide covers step-by-step instructions, best practices, and common pitfalls to avoid when rebasing branches in Git.
Derrick Mwiti's photo

Derrick Mwiti

8 min

See MoreSee More