GIT Version Control System
β Core Essence of Git
At its heart, Git is a tool to track and manage changes to files over time, especially source code, and it enables multiple developers to collaborate without overwriting each otherβs work.
Think of it like a time machine + collaboration manager for your code.
π What Makes Git Special?
1. Distributed
- Each user has a complete copy of the repository.
- You can work offline.
- Full access to history and no single point of failure.
2. Version Tracking
Each "commit" in Git saves:
- What files changed
- What was added/removed
- Who made the change and when
- A unique ID (SHA-1 hash) for the commit
This enables you to:
- Go back to any previous version
- Compare versions
- Branch and experiment safely
3. Branches & Merging
Git makes it easy to:
- Create a branch to work independently
- Merge branches back into the main branch
This supports parallel development, feature testing, and bug fixes.
π§ Think of Git Like This
git init
: Start your project diary.git add
: Tell Git which files to track.git commit
: Take a snapshot of changes.git branch
: Start a new timeline of work.git merge
: Combine changes from different timelines.git push/pull
: Share and fetch changes with others.
π€ Collaboration
Git enables teamwork via remote repositories like GitHub, GitLab, and Bitbucket. Collaborators can:
- Push their changes to a shared hub
- Pull updates from others
- Review and approve changes (pull requests)
- Resolve merge conflicts
π Git Push, Pull Requests, and Forks
β git push
git push
uploads your local commits to a remote repository (like GitHub).
Example:
git push origin main
This pushes your changes to the main
branch on the remote named origin
.
β Pull Request (PR)
A Pull Request is a request to merge your changes into someone elseβs branch or repository. It's used in collaboration workflows for:
- Code review
- Discussion and feedback
- CI checks (automated tests)
- Approval and controlled merging
Workflow:
- You push changes to your branch
- Create a pull request on GitHub
- Reviewers approve or suggest changes
- The branch is merged after review
β Fork
A fork is a personal copy of someone else's repository on GitHub. It allows you to:
- Experiment freely
- Work independently without affecting the original
- Submit a Pull Request later if needed
Typical Open Source Flow:
- Fork the repo on GitHub
- Clone it to your machine
- Make changes and push to your fork
- Create a pull request to the original repo
π§© Other Git Concepts
β git status
Displays the current state of the working directory and staging area.
git status
β git log
Shows the project's commit history.
git log
Optional flags:
git log --oneline --graph --all
β git diff
Shows differences between files, commits, or branches.
git diff
git diff --staged
git diff HEAD~1
β git clone
Creates a local copy of a remote Git repository.
git clone https://github.com/user/repo.git
β git rebase
Rewrites the base of a branch by reapplying commits on a new base.
git rebase main
Note: Avoid using on public branches to prevent history conflicts.
β git stash
Temporarily saves changes that are not ready to be committed.
git stash
git stash apply
git stash pop
git stash list
β .gitignore
Used to tell Git to ignore specific files or directories.
Example:
*.log
build/
.env
__pycache__/
π§ Summary Table
Command/Concept | Purpose |
---|---|
git status | Shows current working/staging state |
git log | Shows commit history |
git diff | Compares file changes |
git clone | Copies a remote repo to your system |
git rebase | Rewrites history to apply changes on a new base |
git stash | Saves uncommitted work temporarily |
.gitignore | Excludes files from being tracked |
π Summary
Concept | Description |
---|---|
Distributed | Everyone has the full project and history |
Versioning | Every commit saves a state of the project |
Branching | Parallel development is easy and safe |
Merging | Combine changes from multiple branches |
Collaboration | Coordinate changes with multiple developers |
π Git vs GitHub
Aspect | Git | GitHub |
---|---|---|
What is it? | Command-line tool | Web-based platform |
Role | Version control system | Hosting and collaboration |
Works offline? | Yes | No |
Functionality | Tracking changes, branching, merging | PRs, Issues, CI/CD, team work |
Installation needed? | Yes | No (browser-based) |
Examples | git commit , git merge | GitHub PRs, Actions, Issues |
Analogy: Git is the engine of your car. GitHub is the garage and road system to drive and collaborate with others.