1. Introduction to Git

What is Git?

Git is a Distributed Version Control System (DVCS) that tracks changes in source code and allows multiple developers to collaborate efficiently.

Key Concepts:
  • Snapshots, Not Diffs: Git stores complete snapshots of your project at each commit, not just file differences
  • SHA-1 Hashes: Each commit is identified by a unique 40-character hexadecimal hash (e.g., `3a7d8f2`)
  • Distributed: Every developer has a complete copy of the repository history
  • Branching: Create isolated development branches for features without affecting the main code
  • Staging Area: Control exactly what goes into each commit

Git Workflow (Working Directory → Staging → Repository)

Working Directory

(Local files)

Staging Area

(git add)

Repository

(git commit)

Pro Tip: The staging area lets you choose exactly which changes to commit, enabling atomic commits with related changes only.

BeginnerInstallation

Windows:
Download Git from https://git-scm.com/download/win Run installer with default settings
macOS:
brew install git
Linux (Ubuntu/Debian):
sudo apt-get update sudo apt-get install git
Verify Installation:
git --version # Should show git version 2.x.x or higher

2. Git Setup & Configuration

BeginnerInitial Setup

Global Configuration:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com" git config --global core.editor "code"
View Configuration:
git config --list git config --global --list git config user.name

IntermediateUseful Aliases

git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.amend 'commit --amend --no-edit' git config --global alias.visual 'log --graph --oneline --all'

3. Basic Git Commands

BeginnerInitialize & Clone

Create a New Repository:
git init git init project-name git init --bare
Clone an Existing Repository:
git clone https://github.com/user/repo.git git clone --depth 1 https://github.com/user/repo.git

BeginnerStage & Commit

Check Status:
git status git status -s git diff git diff --staged
Stage Changes:
git add file.txt git add . git add -A git add -p

Key Difference:
git add . - Stages changes in current dir (not deletions)
git add -A - Stages ALL changes (additions, modifications, deletions)

Commit Changes:
git commit -m "message" git commit --amend git commit --amend --no-edit git commit -am "message"

4. Branching & Merging

BeginnerBranch Operations

List Branches:
git branch git branch -a git branch -r
Create & Switch:
git branch feature/login git checkout feature/login git checkout -b feature/login git switch feature/login
Delete & Rename:
git branch -d feature/login git branch -D feature/login git branch -m old-name new-name

IntermediateMerging & Rebasing

Merge Strategies:
git merge feature/login git merge --no-ff feature/login git merge --squash feature/login git rebase main git rebase -i HEAD~3

Merge vs Rebase:
Merge: Preserves history, creates merge commit
Rebase: Linear history, rewrites history (use only on local branches)

5. Remote Repositories & GitHub

BeginnerRemote Management

Remote Commands:
git remote git remote -v git remote add origin https://github.com/user/repo.git git remote set-url origin https://github.com/user/repo.git git remote show origin
Push, Pull & Fetch:
git fetch origin git pull origin main git push origin main git push -u origin feature/login git push --all git push --tags

Key Differences:
fetch: Downloads changes (no merge)
pull: fetch + merge
push: Upload local changes

IntermediateSSH Setup

Generate SSH Key:
ssh-keygen -t ed25519 -C "your.email@example.com" cat ~/.ssh/id_ed25519.pub

Then add to GitHub: Settings → SSH Keys → New SSH Key

6. Pull Requests & Code Review

BeginnerPR Workflow

Create a Pull Request:
git checkout -b feature/user-auth # Make changes git add . git commit -m "feat: add authentication" git push -u origin feature/user-auth # Go to GitHub and click "Create Pull Request"

PR Best Practices:
• Clear title describing change
• Detailed description of what and why
• Link related issues: "Closes #123"
• Keep PRs small and focused (200-400 lines)
• Include screenshots for UI changes

7. Stashing

BeginnerStash Operations

git stash git stash push -m "wip: message" git stash list git stash apply git stash pop git stash drop stash@{0}

💡 Use Case: Urgent branch switch needed?
git stash → switch branch → git stash pop

8. Reset, Revert & Restore

IntermediateUndo Operations

⚠️ Soft Reset: Keeps staged & working directory

git reset --soft HEAD~1

⚠️ Mixed Reset (default): Keeps working directory

git reset HEAD~1

🔴 Hard Reset: DESTRUCTIVE - discards all changes

git reset --hard HEAD~1
Revert vs Reset:
git revert abc1234 # Safe for shared branches git reset --hard abc1234 # Only for local branches git restore file.txt # Discard changes (modern) git restore --staged file.txt # Unstage file

9. Logs & History

BeginnerView History

git log git log --oneline git log -n 10 git log --graph --oneline --all git log --author="Name" git log --since="2 weeks ago" git blame file.txt

AdvancedReflog (Recovery)

Reflog shows where HEAD has been. Critical for recovery!

git reflog git checkout -b recovered-branch abc1234

Recovery Window: 90 days by default

10. Advanced Git Techniques

AdvancedAdvanced Operations

Cherry-Pick:
git cherry-pick abc1234 git cherry-pick abc1234 def5678 git cherry-pick abc1234..def5678
Bisect (Find Bug):
git bisect start git bisect bad HEAD git bisect good v1.0 # Test and answer 'git bisect bad' or 'git bisect good' # Repeat until bug found git bisect reset
Worktrees:
git worktree add ../project-feature feature/new-api git worktree list git worktree remove ../project-feature

11. .gitignore & Clean

Beginner.gitignore Basics

Common Patterns:

# IDE & Editors .vscode/ .idea/ *.swp # Dependencies node_modules/ /vendor # Environment .env .env.local # Build dist/ build/ # OS .DS_Store Thumbs.db

Clean Working Directory:
git clean -n # Preview (safe) git clean -f # Delete untracked files git clean -fd # Delete files & directories

Warning: Always use -n first to preview!

12. Common Workflows

BeginnerFeature Branch Workflow

1. Start from main git checkout main && git pull 2. Create feature branch git checkout -b feature/user-auth 3. Make commits git add . && git commit -m "feat: add login" 4. Push and create PR git push -u origin feature/user-auth 5. After approval, merge git checkout main git merge --no-ff feature/user-auth git push 6. Cleanup git branch -d feature/user-auth

IntermediateVersioning & Releases

Semantic Versioning (MAJOR.MINOR.PATCH):
git tag v1.2.3 git tag -a v1.2.3 -m "Release 1.2.3" git push origin v1.2.3 git push origin --tags

13. Troubleshooting

IntermediateCommon Issues

Detached HEAD:
git checkout -b temp-branch git checkout main
Merge Conflicts:
# 1. Edit conflicted files # 2. Remove conflict markers # 3. Stage and commit git add . git commit -m "Resolve conflicts"
Recover Deleted Branch:
git reflog git checkout -b recovered abc1234
Wrong Commit Message:
git commit --amend -m "New message" git rebase -i HEAD~3 # For older commits

14. Quick Command Reference

aaadddd

aaaaaaddd

15. Interview Questions (50+)

aaaa

dddddddddddddddddddddddddd

16. Pro Tips & Best Practices

✨ Commit Message Format

  1. Use imperative mood: "Add" not "Added"
  2. Capitalize first letter
  3. No period at end
  4. Keep subject under 50 chars
  5. Separate subject from body with blank line
  6. Explain WHY, not WHAT
  7. Format: type: description
  8. Reference issues: "Closes #123"
Good Example:
feat: add user authentication Implement JWT-based auth for API endpoints. Closes #456

🚀 Productivity Aliases

git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.amend \ 'commit --amend --no-edit' git config --global alias.visual \ 'log --graph --oneline --all' git config --global alias.unstage \ 'reset HEAD --'

Usage: git st, git co feature

🌳 Branch Best Practices

✓ DO:

  • Create branch per feature
  • Keep branches focused
  • Delete merged branches
  • Pull latest before work
  • Push regularly (backup)

✗ DON'T:

  • Long-lived branches
  • Work on main/develop
  • Force push shared branches
  • Rebase shared branches

🔒 Security Best Practices

  • ✓ Use SSH keys (not HTTPS passwords)
  • ✓ Never commit secrets (.env files)
  • ✓ Use .gitignore for sensitive files
  • ✓ Sign commits with GPG
  • ✓ Enable branch protection on main
  • ✓ Require code reviews before merge
  • ✓ Rotate SSH keys regularly