What is Version Control?
Version control is a system that records changes to files over time so that you can recall specific versions later. It is essential for collaborative software development and also extremely useful for solo developers to track and manage changes.
Why Use Version Control?
- Track the history of changes in a project
- Restore previous versions of code/files if something breaks
- Collaborate with others without overwriting their work
- Create and test features in isolation (branches)
Types of Version Control
- Local Version Control: Maintains versions on a local machine
- Centralized Version Control (CVCS): All files are stored on a central server
- Distributed Version Control (DVCS): Every user has a local copy of the repository (e.g., Git)
Why Git?
Git is the most popular distributed version control system. It is fast, reliable, and widely used by developers worldwide. Git enables local development, branching, merging, and collaboration on remote repositories.
Installing Git
# For Windows/macOS/Linux
# Visit https://git-scm.com/ and download the appropriate installer
Basic Git Workflow
- Initialize a repository
- Add files to staging
- Commit changes
- View commit history
Step-by-Step Git Example
1. Initialize a Git repository
git init
This creates a hidden .git
folder in your current directory, marking it as a Git repository.
2. Add files to the staging area
git add myfile.txt
This stages the specified file to be committed.
3. Commit the changes
git commit -m "Initial commit with myfile.txt"
This records the snapshot of your staged files.
4. View the commit history
git log
Output:
commit 6a1f9d9c7a... Author: Your Name Date: Fri May 9 10:00:00 2025 +0530 Initial commit with myfile.txt
Understanding the Workflow
START
INIT project with 'git init'
CREATE or MODIFY files
STAGE files with 'git add'
COMMIT with message
REPEAT on changes
END
Common Beginner Questions
Q: What happens if I change a file but forget to commit?
A: The change stays in your working directory. You can use git status
to see uncommitted changes.
Q: Can I undo a mistake?
A: Yes. Git provides commands like git checkout
, git reset
, and git revert
to manage changes.
Q: Do I need an internet connection to use Git?
A: No, Git works locally. You only need internet when working with remote repositories like GitHub.
Using Branches
Branches help you work on new features without affecting the main codebase.
git branch feature-x
git checkout feature-x
Merging Changes
Once your work is complete, merge it into the main branch.
git checkout main
git merge feature-x
Working with Remote Repositories
git remote add origin https://github.com/your/repo.git
git push -u origin main
This pushes your local commits to a remote repository for backup or collaboration.
Summary
- Git is a powerful tool to track and manage code changes.
- It supports both solo development and team collaboration.
- Learn the basic commands:
init
,add
,commit
,log
,branch
,merge
,push
Mini Practice Project
Create a simple pseudocode project (e.g., a calculator), track changes using Git as you evolve your logic from basic operations to advanced ones like recursion.
Tip: Always commit small logical changes with meaningful messages. This makes history cleaner and easier to navigate.