Yeswanth Madasu

Day 04/13 of Git 101 Series

Concept explanation

A Git repository has two visible parts: your working directory (the files you edit) and a hidden .git directory (the repo brain). The .git folder contains the complete history, configuration, and pointers that make version control possible. When you run git init you create that part for a new project. When you run git clone you copy someone else's brain plus a working copy of the latest files.

Key concepts to keep in mind
• Working directory: the files you see and edit.
• Index (staging area): a small table that records exactly what will go into the next commit. Staging is explicit. Commits record only what the index contains.
• .git: the object store and refs. This is the repo itself. Your working directory is just a checkout of one commit.
• Remote: a name (like origin) that points to another repository URL so you can fetch from and push to it.


Commands

git init
Creates a new .git folder in the current directory and makes the current directory a repository. After this you can stage and commit.

git clone
Downloads (fetches) the remote objects and refs, creates a local .git, sets a remote named origin, creates local branches tracking remote branches, and checks out the default branch into a working directory.

git remote -v
Shows configured remotes and their fetch/push URLs. Quick check to confirm origin is set.

git remote add
Adds a new remote reference to .git/config; useful when you started with git init and later want to link a remote.

git push -u origin
Pushes a branch to origin and sets upstream so future git push and git pull work without specifying origin and branch.


example

Start a new project and push it to GitHub
mkdir myapp
cd myapp
git init

Create files and commit
git add .
git commit -m "first commit"
git remote add origin https://github.com/you/myapp.git
git push -u origin main

Clone an existing project and start working
git clone https://github.com/you/myapp.git
cd myapp

create feature, commit, push
git checkout -b feature-x
Edit files
git add .
git commit -m "feature work"
git push -u origin feature-x


Real world use case

Team onboarding: one developer creates the repo and pushes it to a remote host. The rest of the team uses git clone to get the full history and a ready-to-edit working tree. Every developer keeps a local copy of the entire project history so they can branch, inspect past commits, run bisect, and work offline.

Migrating an existing project: you can turn any folder into a tracked project by running git init, committing, creating a remote, and pushing. This replaces the old zip-and-email or shared-drive workflows where history is lost.


Under-the-hood explanation

What git init actually does

Creates a .git directory with a base structure: objects/, refs/, hooks/, info/, and files like HEAD and config.
Writes HEAD pointing to a branch name (by default refs/heads/main or refs/heads/master depending on Git config). HEAD is a pointer that says what commit your working directory should reflect.
Prepares an empty object database. No commits exist yet until you create them.

What git clone does step by step (simplified)

Create a new directory and run a local git init inside it.
Create a remote named origin and record the URL in .git/config.
Fetch objects and refs from the remote. Objects may arrive as loose objects or in packed packfiles.
Create local branches that track the remote branches. If the remote has HEAD pointing at some branch, git sets up local HEAD accordingly.
Checkout the chosen branch into the working directory, writing files from the object store into the filesystem.
Leave you with origin set and the working tree ready.

you can read the series of @blogs in here. and i do write a lot in twitter @yswnth


Tomorrow : Day 05/13 of Git 101 Series Add, Commit, Amend