habib's blog

Git - cause i keep on forgetting this piece of shit

Git Basics: The Complete Guide with Analogies

Think of Git Like This

Imagine your code is a massive document. Git is like a version control system at Google Docs where:


1. How to Create a GitHub Repo

The Setup:

First, go to GitHub.com and create an account if you don't have one.

Steps:

  1. Click "+" in the top right corner
  2. Select "New repository"
  3. Name your repo (e.g., "my-awesome-project")
  4. Add a description (optional)
  5. Choose Public or Private
  6. Click "Create repository"

GitHub will show you some commands. Copy them. You'll need them next.


2. Push Your Local Code to GitHub for the First Time

The Analogy:

You've been writing code on your laptop (local machine). It's like having a bunch of papers on your desk. Now you want to send them to GitHub (the cloud). Here's the process:

Step 1: Initialize Git in Your Project

Open your terminal/command line in your project folder and run:

git init

This is like saying "Hey Git, start tracking this folder from now on."

Step 2: Add Your Files

git add .

The dot (.) means "add everything." It's like gathering all your papers and putting them in an envelope.

Step 3: Create Your First Commit

git commit -m "Initial commit"

This is like sealing the envelope and writing "Version 1" on it. The -m flag lets you add a message describing what you did.

Step 4: Connect to GitHub

git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO-NAME.git

This tells Git "when I say push, send it to THIS GitHub repo." Origin is just the default name for your GitHub repo.

Step 5: Push to GitHub

git branch -M main
git push -u origin main

The first line renames your branch to "main" (we'll explain this next). The second line uploads everything to GitHub. The -u flag means "set this as the default."

Boom. Your code is now on GitHub.


3. Main vs Master: What's the Difference?

The Analogy:

Imagine you're managing a restaurant:

There's literally NO difference in how they work. GitHub just decided in 2020 to call it "main" instead of "master" to be more inclusive. It's just a naming thing.

What is a Branch?

A branch is like a timeline split. You have the main timeline (main/master), and you can create alternate timelines (branches) to experiment.

main branch: Version 1 → Version 2 → Version 3 (stable)
             ↓
             └─→ v2 branch: Experiment 1 → Experiment 2 (testing)

Quick Summary:


4. How to Push Code to Main/Master

Scenario: You've made changes to your files. Now you need to save them to GitHub.

Step 1: Check What You Changed

git status

This shows you which files changed (they'll be red if not staged, green if staged).

Step 2: Add Files to Staging Area

git add .

Or add specific files:

git add filename.js

The Analogy: You're putting files in a box before shipping. Staging = putting in the box. Committing = sealing the box and labeling it.

Step 3: Commit Your Changes

git commit -m "Fixed the login bug"

Always write a clear message. Future you will thank you.

Step 4: Push to GitHub

git push origin main

This uploads your commit to the main branch on GitHub.

Done. Your code is now on GitHub under the main branch.


5. How to Create a New Branch (e.g., v2)

The Analogy:

You don't want to mess with the main code yet. So you create a parallel universe where you can experiment. Main stays clean, v2 gets messy.

Create and Switch to the Branch:

git checkout -b v2

This creates a new branch called "v2" and switches to it in one command.

Verify You're on the Right Branch:

git branch

You'll see something like:

  main
* v2

The asterisk (*) shows you're currently on v2.

What Just Happened?

Git created a copy of all code from main and called it v2. You're now working in this copy. Main is untouched.


6. How to Push Code to the New v2 Branch

This is exactly like pushing to main, but you're in a different branch.

Step 1: Make Your Changes

Edit your files in the v2 branch.

Step 2: Stage and Commit

git add .
git commit -m "Added new feature for v2"

Step 3: Push to the v2 Branch

git push origin v2

Same as pushing to main, just replace "main" with "v2."

On GitHub, you'll now see two branches. Click the branch dropdown to see both main and v2.


7. How to Merge and See Differences

The Analogy:

You experimented in v2 and it worked great. Now you want to bring those changes back to main. Merging is like combining two timelines back into one.

Step 1: Switch to Main

git checkout main

This takes you back to the main branch.

Step 2: See What Changed in v2

Before merging, let's see what's different:

git diff main v2

You'll see:

Step 3: Merge v2 into Main

git merge v2

This pulls all the changes from v2 into main.

What If There's a Conflict?

If you edited the same line in both branches, Git gets confused and asks you to choose. The conflicted file will look like:

<<<<<<< HEAD
This is from main
=======
This is from v2
>>>>>>> v2

You manually pick which version you want, remove the markers, then:

git add .
git commit -m "Resolved merge conflict"

Step 4: Push the Merged Code

git push origin main

Now main has all the changes from v2, and v2 still exists.


8. How to Delete a Branch

After merging v2 into main, you don't need v2 anymore.

Delete Locally:

git branch -d v2

The -d flag means "delete if everything is merged."

Delete on GitHub:

git push origin --delete v2

Now v2 is gone from both your computer and GitHub.


9. Super Important Git Commands You Need to Know

git status

git status

Shows which files changed. Run this constantly. It's your friend.

git log

git log

Shows your commit history. You'll see who committed what, when, and with what message.

To exit log, press 'q'.

git checkout (Switching Branches)

git checkout main

Switch to main branch.

git checkout v2

Switch to v2 branch.

git checkout -b new-feature

Create and switch to a new branch in one command.

git pull (Get Latest Code)

git pull origin main

Downloads the latest changes from GitHub. Always do this before starting work.

git add (Staging Files)

git add .

Stage all files.

git add filename.js

Stage specific file.

git commit (Save Changes)

git commit -m "Your message here"

Always write a descriptive message.

git push (Upload to GitHub)

git push origin main

Upload commits to main.

git push origin v2

Upload commits to v2.

git fetch (Check What's New Without Merging)

git fetch origin

Sees what's new on GitHub but doesn't merge. Useful for checking before pulling.

git reset (Undo Things)

git reset HEAD~1

Undoes your last commit but keeps the changes. HEAD~1 means "go back 1 commit."

git reset --hard HEAD~1

Undoes your last commit AND deletes the changes. Dangerous. Use only if you're sure.


The Typical Daily Workflow

You Start Your Day:

git pull origin main

Get the latest code.

You Work on v2:

git checkout -b v2
git add .
git commit -m "Added feature X"
git push origin v2

Everything Works, Time to Merge:

git checkout main
git pull origin main
git merge v2
git push origin main
git branch -d v2
git push origin --delete v2

Done.


Cheat Sheet

What You Want Command
Create repo On GitHub.com manually
First push git init → git add . → git commit -m "msg" → git remote add origin URL → git push -u origin main
Check status git status
Create branch git checkout -b branch-name
Switch branch git checkout branch-name
Push to branch git push origin branch-name
See differences git diff main branch-name
Merge branch git checkout main → git merge branch-name → git push origin main
Delete branch git branch -d branch-name → git push origin --delete branch-name
Get latest code git pull origin main
View commits git log

Final Thoughts

Git is simple when you remember: everything is just moving files between your computer and GitHub. You're not doing magic. You're just organizing snapshots of your code and sharing them.

Once this becomes muscle memory, you'll be fast as fuck. Seriously. Just practice these commands daily for a week, and you'll never forget them.