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-projectcd my-projectgit init
Option B: Clone an existing project
git clone https://github.com/user/repo.gitcd 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 changedgit add file.txt # Stage a filegit add . # Stage everythinggit 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 branchgit 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 maingit 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 historygit 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
| Action | Command |
|---|---|
| Check status | git status |
| Add files | git add . |
| Commit | git commit -m "msg" |
| Push | git push |
| Pull | git pull |
| New branch | git checkout -b name |