Git Course 0%

init vs clone; the .git folder

Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI ≈ 9 min

What you will learn

  • When to init and when to clone
  • What clone sets up for you that init does not (origin, tracking)
  • What is inside .git and why it must stay untouched

After this lesson you can

  • I can start a repository from a folder and get an existing one from GitLab or GitHub
  • I know that deleting .git deletes the history and nothing else

Why this matters

Every repository on your computer got there in one of two ways. Choosing the right one decides whether Git already knows where the remote is, and understanding the hidden folder both create prevents the one mistake that actually destroys history.

Two ways in

git init git clone <url>
Use when You have a folder of files and no repository yet The repository already exists on GitLab or GitHub
Creates An empty .git in the current folder; no commits A new folder with the full history and the files of the default branch
Remote None; you add one later (git remote add origin <url>) origin set to the URL; main tracks origin/main
Typical for A brand-new project, a personal notes folder Joining any existing project (by far the most common)

You did git init on the playground; you will git clone almost everything else in your working life.

Start a new repository in an existing folder:

Terminal
$ cd ~/git-practice/trailguide
$ git init
Initialized empty Git repository in /Users/you/git-practice/trailguide/.git/

Nothing is committed yet; the files are untracked until you git add and git commit them.

Get an existing repository (the URL comes from the project's page; the Code or Clone button shows it):

Terminal
$ cd ~/git-practice
$ git clone https://gitlab.com/northwind-trails/trailguide.git
Cloning into 'trailguide'...
done.

Real clones also print progress lines (remote: Enumerating objects…, Receiving objects: 100%). A folder named after the repository appears, with the files and a .git inside, and the remote is already configured:

Terminal
$ cd trailguide
$ git remote -v
origin	https://gitlab.com/northwind-trails/trailguide.git (fetch)
origin	https://gitlab.com/northwind-trails/trailguide.git (push)

To clone into a folder with a different name, add it: git clone <url> my-folder.

Inside .git

Both commands create the hidden .git folder. It is the repository:

Terminal
$ ls .git
COMMIT_EDITMSG
HEAD
config
description
hooks
index
info
logs
objects
refs
Item What it holds
objects/ Every commit and every version of every file, compressed
refs/ Branches (heads/), tags (tags/), remote-tracking branches (remotes/)
HEAD Which branch you are on
index The staging area
config The repository's local configuration, including origin's URL
logs/ The reflog: a diary of where HEAD and branches pointed (lesson 12.1)
hooks/ Optional scripts Git runs at certain moments (lesson 18.4)

The working tree is everything outside .git. Deleting the working tree while keeping .git loses nothing: git restore . rebuilds the files from the last commit.

Common mistakes

  • Cloning inside another repository. A repository inside a repository confuses both. Clone into a plain folder such as git-practice.
  • Downloading the ZIP and wondering why git status fails. The ZIP has no .git. Clone.
  • git init in your home folder by accident. Every file you own becomes untracked in one giant repository. Fix: delete the .git folder that git init just created in your home folder (this one time, that is the right move, and it is the only .git you should ever delete), then cd into the intended folder.
  • Cloning with a URL that is not yours to push to. You can clone any public repository, but pushing needs permission; Section 7 covers forks for that case.

Try it yourself

Goal: see the difference between a clone and a ZIP download, without leaving your computer.

  1. Clone your own playground into a second folder: cd ~/git-practice && git clone trailguide trailguide-copy (Git can clone from a local path).
  2. In trailguide-copy, run git remote -v and git log --oneline.
  3. Delete the copy when done: rm -rf ~/git-practice/trailguide-copy.

Expected result: the copy has the full history and an origin pointing at your original folder; a ZIP would have had neither.

Show solution

Terminal
$ git clone trailguide trailguide-copy
Cloning into 'trailguide-copy'...
done.
git remote -v shows origin as the path to your original trailguide; git log --oneline shows the same commits. That is what a clone is: a complete, independent copy that remembers where it came from.

Check yourself

1. You want to work on a project that already exists on GitLab. What do you run?
2. What happens if you delete the .git folder?
3. After git clone, which of these is already set up?

Key terms

Repository (repo) Clone origin Working tree