Lesson 7 of 6815 min read

git init: Initializing a New Repository and the .git Folder Explained

Learn how git init turns an ordinary folder into a Git repository, and what actually lives inside the hidden .git directory.

Author: CodersNexus

git init: Initializing a New Repository and the .git Folder Explained

Every Git repository begins with a single command: `git init`. This lesson demystifies exactly what this command does — it doesn't copy your files anywhere or send anything over a network; it simply creates a hidden `.git` folder inside your project directory, which is where all of Git's tracking, history, and configuration for that project will live from that point forward.

Learning Objectives

  • Use git init to turn any existing folder into a Git repository.
  • Understand that git init only creates the .git folder — it doesn't touch your existing files.
  • Identify the key contents of the .git folder: HEAD, objects, refs, config.
  • Distinguish git init from git clone.

Key Terms to Know Before Learning git init

  • git init: The command that initializes a new, empty Git repository by creating a .git subdirectory in the current folder.
  • .git folder: A hidden directory created by git init that stores all of a repository's metadata, configuration, and complete commit history.
  • HEAD: A reference inside the .git folder that points to the commit currently checked out — essentially 'where you are' in the project's history.
  • objects directory: The part of .git where Git stores the actual content of every file, commit, and tree as compressed, hash-identified objects.
  • refs directory: The part of .git that stores pointers to commits, primarily used to track the tips of branches and tags.

How git init Actually Works

Running `git init` inside any folder — new or already containing files — instructs Git to start tracking that folder as a repository. Concretely, this command does exactly one thing: it creates a hidden subdirectory named `.git` at the root of that folder. Nothing else happens. Your existing files are completely untouched; they simply become eligible to be tracked by Git going forward (though they won't actually be tracked until you `git add` them, as covered in the next lesson).

Inside the `.git` folder, several key items are created immediately:

- **HEAD**: A simple text file that acts as a pointer to whatever commit or branch you currently have 'checked out'. In a freshly initialized repository, HEAD typically points to a branch reference that doesn't have any commits yet (like `refs/heads/main`), since nothing has been committed.
- **objects/**: An initially empty directory that will store every piece of content ever committed to this repository — files, directory trees, and commits — each identified by a unique SHA hash. This is effectively Git's core database.
- **refs/**: A directory holding pointers (references) to commits, primarily organized into `refs/heads/` for branches and `refs/tags/` for tags.
- **config**: The local-level configuration file for this specific repository (as discussed in Lesson 5), where repository-specific settings like a custom `user.email` can be stored.
- **description**: A file used by some Git tools (largely irrelevant for most users' day-to-day workflows).

A critical distinction for beginners: `git init` creates a brand-new, completely empty repository with no history. This is different from `git clone`, which both creates a new local repository *and* copies (clones) the complete existing history from a remote source into it. You use `git init` when starting a brand-new project from scratch, and `git clone` when you want to obtain a working copy of a project that already exists elsewhere (e.g., on GitHub) — a topic covered in the next module.

It's also worth knowing that `git init` is safe to run in a folder more than once — running it again simply reports 'Reinitialized existing Git repository' and does not delete your existing history or files.

git init: Visual Walkthrough

Show a folder icon labeled 'my-project/' containing a few visible files (index.html, style.css). Draw an arrow labeled 'git init' pointing into the folder, after which a new hidden folder appears inside it labeled '.git/' with a dotted-line box expanding to show its contents: 'HEAD', 'objects/ (empty)', 'refs/heads/, refs/tags/', 'config', 'description'. Emphasize with a note: 'Only .git/ is created — index.html and style.css remain untouched.'

git init: Quick Reference Table

.git ItemTypePurpose
HEADFilePoints to the currently checked-out branch or commit
objects/DirectoryStores all committed file content, trees, and commits as hashed objects
refs/DirectoryStores pointers to commits — organized into heads/ (branches) and tags/
configFileRepository-specific (local-level) configuration settings
indexFile (created after first git add)The staging area's internal representation

git init: Command Syntax and Examples

# Create a new project folder and initialize it as a Git repository
mkdir my-portfolio
cd my-portfolio
git init
# Output: Initialized empty Git repository in /path/to/my-portfolio/.git/

# Confirm the .git folder now exists
ls -la
# Shows a hidden .git directory alongside your regular files

# Peek inside .git to see its structure (read-only exploration, don't edit these manually)
ls .git
# HEAD  config  description  hooks  info  objects  refs

cat .git/HEAD
# ref: refs/heads/main   <- points to the 'main' branch, which has no commits yet

Breaking Down the git init Example

After running `git init`, `ls -la` reveals the new hidden `.git` folder sitting alongside any existing files — nothing else in the project changed. Looking inside `.git` with `ls .git` shows the core structural pieces described above: `HEAD`, `objects`, `refs`, and `config` among others. Reading `.git/HEAD` shows it currently points to `refs/heads/main`, meaning Git considers you to be 'on' the main branch — even though that branch has no commits yet, since nothing has been added or committed at this point.

How git init Is Used on Real Engineering Teams

  • Every new software project — from a solo student assignment to a large company's new microservice — begins its version-controlled life with a single git init command run in an empty or existing project folder.
  • Project scaffolding tools (like create-react-app or Vue CLI) automatically run git init as part of generating a new project, so developers get version control set up without a manual step.
  • DevOps teams sometimes need to retroactively add version control to an old, un-tracked legacy codebase — running git init directly inside that existing folder without disturbing any files is the standard starting point.
  • Educational bootcamps teach git init as the literal first hands-on Git command students run, since it's the gateway to every subsequent add/commit/branch operation.

git init Interview Questions and Answers

Q1. What does the git init command actually do?

git init creates a new, empty Git repository by adding a hidden .git subdirectory to the current folder. It does not modify, move, or touch any existing files in that folder — it simply sets up the internal structure (HEAD, objects, refs, config) that Git needs to begin tracking changes.

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

git init creates a brand-new, empty repository with no history, typically used when starting a project from scratch. git clone creates a new local repository by copying the complete existing history from a remote repository, typically used to obtain a working copy of a project that already exists elsewhere, such as on GitHub.

Q3. What is stored inside the .git folder?

The .git folder contains everything Git needs to manage a repository: HEAD (a pointer to the current branch/commit), the objects directory (the actual content of every committed file, tree, and commit, stored as hashed objects), the refs directory (pointers to branch tips and tags), and a local config file for repository-specific settings.

git init Quiz: Test Your Understanding

1. What does running git init inside a folder create?

  1. A backup copy of all files
  2. A hidden .git directory
  3. A new remote repository on GitHub
  4. A compressed zip of the project

Answer: B. A hidden .git directory

Explanation: git init's sole action is creating a hidden .git subdirectory in the current folder, which will hold all of Git's tracking data for that project going forward.

2. What does the HEAD file inside .git typically point to right after git init, before any commits?

  1. The most recent commit hash
  2. A branch reference like refs/heads/main, which has no commits yet
  3. The remote repository URL
  4. The last file that was edited

Answer: B. A branch reference like refs/heads/main, which has no commits yet

Explanation: Immediately after git init, HEAD points to a branch reference (commonly main), but since nothing has been committed yet, that branch doesn't yet resolve to any actual commit.

3. Which of these best describes the difference between git init and git clone?

  1. They are identical commands
  2. git init starts an empty repository; git clone copies an existing repository's full history
  3. git init only works on GitHub; git clone works locally
  4. git clone deletes existing history; git init preserves it

Answer: B. git init starts an empty repository; git clone copies an existing repository's full history

Explanation: git init is used to start version-controlling a project from scratch with no prior history, while git clone copies an entire existing repository, including its full commit history, from a remote source.

git init: Common Mistakes Beginners Make

  • Running git init inside the wrong folder (e.g., the parent Home directory) and accidentally turning far more than intended into a tracked repository.
  • Manually editing files inside .git directly, which can corrupt the repository — nearly everything inside .git should only be modified through Git commands.
  • Believing git init adds or commits any existing files automatically — it only sets up the tracking infrastructure; files must still be explicitly added and committed.
  • Running git init inside an already-cloned repository, creating a nested, confusing second .git folder rather than working within the existing one.

git init: Exam-Ready Quick Notes

  • git init creates only the hidden .git folder — existing files are untouched.
  • Key .git contents: HEAD (pointer to current branch/commit), objects/ (content storage), refs/ (branch and tag pointers), config (local settings).
  • git init = start fresh, no history. git clone = copy an existing repository including its full history.

git init: Key Takeaways

  • git init is the single command that transforms any ordinary folder into a Git-tracked repository.
  • The .git folder is the entire 'brain' of a repository — deleting it removes all version control history for that project.
  • Understanding what's inside .git (HEAD, objects, refs, config) demystifies what Git is actually doing behind the scenes.

Frequently Asked Questions About git init

Q1. What does git init do to my existing files?

Nothing at all. git init only creates a new hidden .git folder in the current directory to set up Git's internal tracking structure. Your existing files remain completely unchanged and are not automatically added or committed.

Q2. Is it safe to delete the .git folder?

Deleting .git permanently removes all version control history for that project — your current files on disk will remain, but you'll lose every past commit, branch, and the ability to track future changes as part of that repository's history. This should only be done deliberately.

Q3. Can I run git init in a folder that already has files in it?

Yes. git init works the same way whether the folder is empty or already contains files — it simply creates the .git tracking folder. You can then use git add to start tracking the existing files.

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

git init creates a brand-new, empty repository with no history — used when starting a project from scratch. git clone copies an entire existing repository, including all of its commit history, from a remote location like GitHub onto your machine.

Q5. What does HEAD mean inside the .git folder?

HEAD is a reference that points to whatever commit or branch you currently have checked out — essentially representing your current position within the project's history. Right after git init, it points to a branch (like main) that has no commits yet.

Summary

`git init` initializes a new Git repository by creating a hidden `.git` folder in the current directory — it does not touch, move, or modify any existing project files. Inside `.git`, Git sets up its core structure: `HEAD` (a pointer to the currently checked-out branch or commit), the `objects` directory (where all committed content is stored as hashed objects), the `refs` directory (pointers to branch tips and tags), and a local `config` file. This is distinct from `git clone`, which both creates a repository and copies an existing project's full history into it. Understanding the contents of `.git` demystifies exactly what Git is tracking and where.

Frequently Asked Questions

Nothing at all. git init only creates a new hidden .git folder in the current directory to set up Git's internal tracking structure. Your existing files remain completely unchanged and are not automatically added or committed.

Deleting .git permanently removes all version control history for that project — your current files on disk will remain, but you'll lose every past commit, branch, and the ability to track future changes as part of that repository's history. This should only be done deliberately.

Yes. git init works the same way whether the folder is empty or already contains files — it simply creates the .git tracking folder. You can then use git add to start tracking the existing files.

git init creates a brand-new, empty repository with no history — used when starting a project from scratch. git clone copies an entire existing repository, including all of its commit history, from a remote location like GitHub onto your machine.

HEAD is a reference that points to whatever commit or branch you currently have checked out — essentially representing your current position within the project's history. Right after git init, it points to a branch (like main) that has no commits yet.