Getting Started with Git¶
Every project needs version control. Git is that version control. There are others - Mercurial, Subversion, Visual Source Safe. But git won. It's free, open source, distributed, can work offline and large brands like GitHub, GitLab, BitBucket, or Azure DevOps use git. This section will focus on some important Git concepts you need to know.
First Step - Clone a repository¶
On a fresh machine, the first thing you always do (apart from git setup etc) is clone a repository. Your team will already have a repository on some git system (Github/Azure Devops/Gitlab). What this cloning does is simply copies files from the cloud to your machine.
Basic Clone Syntax¶
Pro Tip
The .git
extension is optional. Modern systems automatically infer it:
Different Ways to Clone¶
repo-belgium
instead of repo-ontario
. Useful when the repo name is generic.
development
branch instead of main
. Saves you from doing git checkout development
after cloning.
--branch
flag works for tags too.
What Really Happens When You Run git clone
?¶
Behind the Scenes: The Complete Clone Process
You type git clone https://github.com/someone/repo-ontario.git
and boom—a whole project appears. But what's actually happening?
Phase 1: The Setup
flowchart LR
A["git clone"] --> B["Create folder"]
B --> C["Initialize .git/"]
C --> D["Add remote 'origin'"]
style A fill:#e1f5ff
style D fill:#e8f5e9
Git creates a folder and initializes a hidden .git
folder inside it. This .git
folder is the brain—it contains everything Git knows about your project.
Inside the .git Folder
- config → Repository settings
- refs/ → Branch and tag references
- objects/ → All the data (commits, files, everything)
- HEAD → Points to your current branch
- hooks/ → Automation scripts
Git then adds a remote called origin
to .git/config
:
[remote "origin"]
url = https://github.com/someone/repo-ontario.git
fetch = +refs/heads/*:refs/remotes/origin/*
What does that fetch line mean?
"For every branch on the remote (refs/heads/*
), create a local tracking reference (refs/remotes/origin/*
)."
So main
→ origin/main
, dev
→ origin/dev
, and so on.
Phase 2: The Download
flowchart LR
A["GitHub Server"] -->|"Download everything"| B[".git/objects/"]
B --> C["Create origin/main<br/>origin/dev<br/>origin/feature"]
style A fill:#fff3e0
style C fill:#e8f5e9
Git downloads everything: every commit ever made, every branch, every tag, every file version. You're getting a time machine, not just the latest code.
How Git Stores Data
It compresses all this using SHA-1 hashing and stores it in .git/objects/
—like a library where every book has a unique barcode. Want a specific commit? Git looks up its hash and pulls it instantly.
Git creates read-only snapshots for all remote branches:
origin/main
→ Whatmain
looks like on GitHuborigin/dev
→ Whatdev
looks like on GitHuborigin/feature
→ Whatfeature
looks like on GitHub
These are Read-Only
You can view them but can't edit them. They update only when you run git fetch
or git pull
.
Exception: Shallow Clones
If you used --depth 1
, Git only downloads that one commit. No history, no other branches (unless you specified them).
Phase 3: The Checkout
flowchart LR
A["Create local main"] --> B["Link main → origin/main"]
B --> C["Extract files to<br/>working directory"]
style A fill:#e1f5ff
style C fill:#e8f5e9
Git creates your local branch (either main
or whatever you specified with -b
) and links it to its remote counterpart. This is called tracking.
Why Tracking Matters
It means git pull
and git push
know where to sync without you specifying every time. It's like speed dial—one command, it knows where to go.
Finally, Git extracts the latest commit's files into your working directory. Now you see actual code, README files, everything.
Advanced Clone Scenarios¶
Bare Repository (No Working Files)
Creates just the.git
folder without any working files. Used for setting up mirrors or servers.