Git

What is Git?

Git is a version control system that tracks changes in your code, lets you collaborate, and helps you go back to earlier versions if needed.

1. Install Git

Download and install from:
- Git

Then verify:

git --version

2. Set Up Your Identity

Tell Git who you are (this shows in commits):

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

3. Start a Repository

Option A: Create a new project

mkdir my-project
cd my-project
git init

Option B: Clone an existing project

git clone https://github.com/user/repo.git
cd repo

4. Basic Workflow (Core Git Cycle)

Git works in 3 main areas:

  • Working Directory (your files)
  • Staging Area (files ready to commit)
  • Repository (saved history)

Step-by-step:

git status          # See what's changed
git add file.txt    # Stage a file
git add .           # Stage everything
git commit -m "Add a message"  # Save snapshot

5. Branching (Work Safely)

Branches let you work on features without breaking main code.

git branch feature-name      # Create branch
git checkout feature-name   # Switch to it

Or in one command:

git checkout -b feature-name

6. Merge Changes

Bring your branch into main:

git checkout main
git merge feature-name

7. Connect to Remote (GitHub, etc.)

A “remote” is an online repo (like GitHub).

Add a remote:

git remote add origin https://github.com/user/repo.git

Push your code:

git push -u origin main

Pull updates:

git pull

8. View History

git log           # Full history
git log --oneline # Compact view

9. Undo Mistakes

Unstage a file:

git reset file.txt

Discard changes:

git checkout -- file.txt

Undo last commit (keep changes):

git reset --soft HEAD~1

-  Key Concepts to Remember

  • Commit often with clear messages
  • Use branches for new features
  • Pull before pushing to avoid conflicts
  • Don’t panic—Git almost always lets you recover work

Quick Cheat Sheet

ActionCommand
Check statusgit status
Add filesgit add .
Commitgit commit -m "msg"
Pushgit push
Pullgit pull
New branchgit checkout -b name