Understanding Git and Version Control Systems (VCS) in Simple Terms
What is Git?
Imagine you have a magic notebook where every time you make a change, a new page is created to keep track of that change. Git is like that magic notebook for your code. It’s a tool called a Version Control System (VCS) that helps you manage changes to your code over time.
Why is Git Useful?
Let’s say you’re working on a project and you make some changes to your code. If something goes wrong, you might want to go back to an earlier version of your code. Without Git, you’d have to manually save copies of your code and track them yourself, which can be messy and confusing. Git automates this process, allowing you to easily switch between different versions of your code.
File System vs. Git
In a traditional file system, you’re just saving and overwriting files. For example, if you have a file named demo.txt
and you make changes, the old version is gone unless you manually save it somewhere.
In a File System:
You see data, file owner, group, file name, size, permissions, and author details.
No automatic tracking of changes or history.
In Git:
Git keeps track of every change made to the file, storing a history of these changes.
It uses something called a hash ID (a unique string generated for each version of your file) to identify different versions.
Hash ID Explained
A hash ID is like a fingerprint for each version of your file. When you make changes and save them in Git (a process called a "commit"), Git generates a new hash ID to uniquely identify that version.
The History of Git
Git was created by Linus Torvalds, who is also the founder of Linux. One day, after deploying some code, Linus realized he couldn’t go back to an earlier version. This frustration led him to develop Git—a system that saves every version of the code so you can revert to previous states whenever needed.
Common Git Commands
Here are some basic Git commands you’ll use frequently:
git init
: Initializes a new Git repository in your project directory.git --version
: Displays the current version of Git you have installed.ls -a
: Lists all files, including hidden ones..git
is a hidden folder where Git stores version information.git status
: Shows the current status of your project, including which files are changed, staged, or untracked.
Understanding Git Stages: Untracked, Staged, and Tracked
Let’s use a story to explain these concepts.
1. Untracked
Imagine Laila and Majnu are strangers. In Git, new files are like these strangers. They are "untracked" because Git doesn’t know about them yet.
touch Laila.txt
touch Majnu.txt
2. Staged
As Laila and Majnu get to know each other and decide to get engaged, they are "staged" for marriage. In Git, when you add files to the staging area, they are ready to be committed.
git add Laila.txt
git add Majnu.txt
3. Tracked
After their engagement, Laila and Majnu are now "tracked" in their relationship. In Git, once files are committed, they are tracked, and you can see their history and changes.
git commit -m "Added Laila and Majnu files"
4. Untracking Changes
If Laila and Majnu break up and become strangers again, the files can be removed from Git’s tracking.
git rm --cached Laila.txt
5. Restoring Files
If you want to revert a file from the tracked state to the staged state, you can use:
git restore <file>
6. Adding All Files
To add all untracked files to the staging area:
git add .
Summary
Git helps you keep track of every change in your code, so you can always go back to a previous version if needed. By understanding basic Git commands and concepts like untracked, staged, and tracked, you’ll be able to manage your projects more effectively and avoid the frustration of losing valuable work.