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

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 "[email protected]" 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 [email protected]: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://[email protected]/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.

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!

Source:
https://www.datacamp.com/tutorial/git-init