Introduction
Git is a distributed version control system that lets teams track changes to source code over time. Unlike centralized systems, every developer's local clone contains the full history of the project, which makes Git fast, resilient, and well suited to collaborative software engineering.
Cricket analogy: Every franchise in the IPL keeps its own complete scorebook of every match ever played, not just a shared central ledger, so even if headquarters loses records, each team still has the full history.
Explanation
Git organizes work using a three-tree model: the working directory (the files you edit on disk), the staging area or 'index' (a snapshot of changes you intend to include in the next commit), and the repository (the committed history stored in the .git directory). git add moves changes from the working directory into the staging area, and git commit takes what is staged and permanently records it as a new commit in the repository, along with a message describing the change. git status shows the current state of all three trees — which files are modified, staged, or untracked — and git log shows the commit history so you can see who changed what and when.
Cricket analogy: A batsman practices shots in the nets (working directory), the selectors shortlist certain net performances for the final XI (staging), and the official scorecard permanently records the chosen team's innings (repository), with the scoreboard showing all three states at once.
Example
# Check current state of working directory and staging area
git status
# Stage a specific file, then all changes
git add src/app.py
git add .
# Commit staged changes with a message
git commit -m "Add input validation to signup form"
# View commit history
git log --oneline --graph
# Merge feature branch into main (creates a merge commit, preserves history)
git checkout main
git merge feature/signup-validation
# Rebase feature branch onto latest main (rewrites commits, linear history)
git checkout feature/signup-validation
git rebase mainAnalysis
git merge and git rebase both integrate changes from one branch into another, but they behave very differently. git merge creates a new merge commit that ties together the histories of both branches, preserving exactly what happened and when, which is safe for shared branches but can leave a cluttered history. git rebase instead replays your branch's commits on top of the target branch's latest tip, producing a clean, linear history — but because it rewrites commit hashes, it should never be used on commits that others have already pulled, since it can cause confusing conflicts for collaborators.
Cricket analogy: A merge is like combining two separate warm-up net sessions into one official record showing both happened in parallel, while a rebase replays a substitute's practice reps as if they happened right after the main squad's latest session, giving a clean single timeline.
Key Takeaways
- Git tracks changes across three trees: working directory, staging area, and repository.
git addstages changes;git commitpermanently records staged changes with a message.git statusandgit logare your primary tools for inspecting current state and history.git mergepreserves full history via a merge commit;git rebaserewrites history for a linear log.- Never rebase commits that have already been pushed and pulled by others.
Practice what you learned
1. Which Git area holds changes that have been marked with `git add` but not yet committed?
2. What is the main difference between `git merge` and `git rebase`?
3. Which command shows which files are modified, staged, or untracked in your repository?
4. Why should you avoid rebasing commits that have already been pushed and pulled by teammates?
Was this page helpful?
You May Also Like
Branching Strategies
Compare Git Flow, GitHub Flow, and trunk-based development to choose the right branching model for your team.
Code Review Best Practices
Practical techniques for writing reviewable pull requests and giving constructive, effective code review feedback.
Software Engineering Quick Reference
A condensed reference sheet of core software engineering facts: SOLID, GoF patterns, SDLC models, and testing terms.
Related Reading
Related Study Notes in Software Engineering
Browse all study notesMicroservices Study Notes
Software Architecture · 30 topics
Software EngineeringTesting & TDD Study Notes
Software Testing · 30 topics
Software EngineeringDesign Patterns Study Notes
Software Design · 30 topics
Software EngineeringGit & Version Control Study Notes
Bash · 40 topics
Software EngineeringSystem Design Study Notes
Architecture · 40 topics