Ad – 728×90
🚀 Getting Started

git init & git clone – Starting a Repository

Every Git story begins in one of two ways: you either start fresh with git init, or you copy an existing repository with git clone. This lesson covers both commands in depth — including what the hidden .git/ folder contains and how to avoid the most common setup mistakes.

⏱️ 15 min read 🎯 Beginner 📅 Updated 2026

git init – Creating a New Repository

The git init command turns any ordinary directory on your machine into a Git-tracked repository. It creates the hidden .git/ folder that stores all version history, configuration, and metadata. Nothing else in your directory is changed — your files stay exactly as they are.

Initialize the current directory

Bash
# Navigate to your project folder first
cd my-project

# Initialize a new Git repository here
git init
Output
Initialized empty Git repository in /home/user/my-project/.git/

Git confirms that a new empty repository was created. The .git/ folder now exists inside my-project/. Your existing files are not staged or committed yet — that's the next step.

Create and initialize a named directory in one step

If you pass a name to git init, Git creates that directory and initializes the repository inside it — saving you a separate mkdir step.

Bash
# Creates the folder "myproject" and initializes Git inside it
git init myproject

# Then enter the directory
cd myproject
ℹ️
Default branch name

On modern Git (2.28+) you can configure the initial branch name. Many teams use main instead of the legacy master. Run git init -b main or set a global default with git config --global init.defaultBranch main.

git clone – Copying an Existing Repository

git clone downloads a complete copy of an existing repository — all commits, all branches, all history — to your local machine. You typically use it to get a project from GitHub, GitLab, Bitbucket, or any other remote host.

Clone using HTTPS (easiest to start with)

Bash
# General form
git clone https://github.com/username/repository.git

# Real example
git clone https://github.com/torvalds/linux.git

Git creates a folder named after the repository (e.g. linux/), downloads everything, and automatically sets up the remote named origin pointing back to the URL you cloned from.

Clone using SSH (recommended once you've set up SSH keys)

Bash
# SSH form — no password prompts after keys are configured
git clone git@github.com:username/repository.git

# Real example
git clone git@github.com:torvalds/linux.git
ℹ️
HTTPS vs SSH

HTTPS is simpler to start with — it works immediately with a username and password (or personal access token). SSH requires a one-time key setup but is more convenient for daily use because it does not require re-entering credentials. The SSH Keys lesson in the Remotes section explains the setup.

Clone into a custom folder name

By default, Git names the local folder after the repository. Pass a second argument to use a different name:

Bash
# Clone into a folder called "my-linux" instead of "linux"
git clone https://github.com/torvalds/linux.git my-linux

# Clone into the current directory (note the trailing dot)
git clone https://github.com/username/repo.git .
Ad – 336×280

What a Repository Looks Like After Cloning

After cloning a typical project, your directory structure will look like this:

Directory structure
my-project/
  .git/          ← Git's internal database (hidden)
  src/
    main.py
    utils.py
  tests/
    test_main.py
  README.md
  .gitignore

Your project files are visible and editable as normal. The .git/ folder is hidden (its name starts with a dot) and you should almost never need to touch it directly.

What's Inside the .git/ Folder

The .git/ directory is the entire repository — it is the reason Git is a "distributed" version control system. Every developer who clones a project gets this complete database. Here are the key items inside:

ItemWhat it contains
HEAD A pointer to the currently checked-out branch (e.g. ref: refs/heads/main). This tells Git "you are on the main branch right now."
config Repository-level configuration: remote URLs, branch tracking, user overrides. Edited by git config or manually.
objects/ Git's content-addressable storage. Every file snapshot, commit, and directory tree is stored here as a compressed binary object, named by its SHA-1 hash.
refs/ Pointers (references) to commits. refs/heads/ stores branch tips; refs/tags/ stores tags; refs/remotes/ stores last-known remote branch positions.
index The staging area. A binary file that records which changes are staged for the next commit.
COMMIT_EDITMSG The message from the most recent commit. Used when you run git commit --amend.
⚠️
Never manually edit the .git/ folder

Everything inside .git/ is managed by Git. Manually deleting or editing files inside it can corrupt your repository beyond recovery. If you want to delete the entire repository and start over, delete the whole project folder — or just the .git/ folder to "un-Git" the directory while keeping your files.

Verify your repository is set up correctly

Bash
# Check the status of your new or cloned repo
git status

# See where the remote "origin" points (only for cloned repos)
git remote -v
Output (cloned repo)
origin  https://github.com/username/repository.git (fetch)
origin  https://github.com/username/repository.git (push)

Common errors and how to fix them

Error messageCauseFix
fatal: not a git repository You ran a git command outside of an initialized repo Run git init in the right directory, or cd into your project folder
Reinitialized existing Git repository You ran git init in a folder that was already a repo This is harmless — Git just resets config to defaults. No history is lost.
Repository not found Wrong URL, typo in username/repo, or the repo is private Double-check the URL; for private repos you need correct credentials or SSH key access
destination path already exists A folder with the clone's target name already exists Delete the folder first, or clone into a different name with a second argument

📋 Summary

  • git init — creates a new empty repository in the current directory; a .git/ folder is created and nothing else changes.
  • git init myproject — creates the folder and initializes the repo in one step.
  • git clone <url> — downloads a full copy of an existing repository including all history; sets up origin remote automatically.
  • SSH vs HTTPS — HTTPS works immediately; SSH is more convenient after a one-time key setup.
  • The .git/ folder is Git's entire database — HEAD, config, objects/, refs/, and the staging index. Never edit it manually.
  • Common errors: not a git repository means you forgot init; Reinitialized is harmless.

FAQ

What is the difference between git init and git clone? +

git init creates a brand-new, empty repository from scratch — there is no prior history. git clone copies an existing repository (from a remote URL) to your machine, including all commits, branches, and tags. Use git init when starting a project from zero; use git clone when joining an existing project or working with a repository hosted on GitHub/GitLab.

Can I clone a private repository? +

Yes — you just need the right credentials. For HTTPS cloning of a private repo, GitHub and GitLab require a Personal Access Token (PAT) instead of your password (password authentication was deprecated in 2021). For SSH cloning, you need an SSH key added to your account. If you have collaborator access to the repository, both methods work once authentication is set up.

What happens if I delete the .git folder? +

Deleting .git/ removes all Git history, branches, remotes, and configuration from that directory. Your working files remain intact, but the directory is no longer a Git repository — it becomes a plain folder. This is sometimes done intentionally to "detach" a project from Git. To restore version control, you would need to run git init again and recommit everything (or re-clone from the remote if one exists).

Can I have nested Git repositories? +

Yes, but it is generally discouraged for beginners — it leads to confusion. Git will treat the inner repository as an "untracked directory" from the outer repo's perspective. The proper tool for managing a project that contains other Git repositories is git submodules or git subtree, both of which are advanced topics covered later in this course.