Git Version Control
How Git Version Control Works?
Git is the most widely used distributed version control system in the software development world. Whether you are a beginner or an experienced developer, understanding how Git works internally is essential for managing projects efficiently.
In this guide, we will take an in-depth look at how Git version control works, covering its internal structure, workflow, and key commands. By the end of this article, you will have a clear understanding of how Git tracks changes, manages commits, and handles branches.
Table of Contents
- Introduction to Git
- Why Git is Essential for Developers?
- Understanding Git’s Workflow
- Git States: Modified, Staged, and Committed
- How Git Stores Data Internally
- Git File Locations: Working Directory, Staging Area, and Git Directory
- Understanding Git Internals
- Key Git Commands and Their Purpose
- Conclusion
1. Introduction to Git
Git is an open-source version control system that allows developers to track changes, collaborate, and manage different versions of their codebase. Unlike traditional version control systems, Git operates in a distributed manner, meaning that every developer has a complete copy of the repository.
🔗 Official Git Documentation
2. Why Git is Essential for Developers?
Before Git, developers often used manual backups of their code files by renaming them (project_backup_v1, project_backup_v2). This approach led to confusion, duplication of files, and difficulty tracking changes.
With Git, developers can:
✔ Keep track of changes to the source code
✔ Collaborate seamlessly with teams
✔ Revert to previous versions in case of errors
✔ Maintain a clean and structured project history
✔ Work on multiple features simultaneously using branches
3. Understanding Git Version Control Workflow
Git works by tracking changes in files and storing different versions efficiently. The workflow in Git follows a step-by-step process that involves:
- Creating or modifying files in the working directory
- Adding changes to the staging area (
git add) - Committing changes to the local repository (
git commit) - Pushing changes to a remote repository (
git push) - Pulling updates from the remote repository (
git pull)
4. Git States: Modified, Staged, and Committed
Git organizes files into three states:
1. Modified State
- The file has been changed but not yet saved in Git.
- The file is still in the working directory.
2. Staged State
- The file is added to the staging area using
git add. - Git is now tracking this version, but it’s not permanently saved yet.
3. Committed State
- The file is saved permanently in the repository using
git commit. - The commit is given a unique SHA-1 hash for identification.
Example Git Commands for Each State:
# Check the status of files
git status
# Add modified files to staging
git add <file-name>
# Commit the staged files
git commit -m "Added new feature"
5. How Git Stores Data Internally
Unlike other version control systems that store full copies of files at each version, Git stores snapshots of files efficiently. Each commit in Git is not a new copy of the entire repository but a reference to the previous state with only changes recorded.
🔗 Git Internals – Official Docs
6. Git File Locations: Working Directory, Staging Area, and Git Directory
Each file in Git is stored in a different location based on its state:
- Working Directory → The folder where you work with project files.
- Staging Area → A temporary space where Git tracks files before committing.
- Git Directory (.git folder) → The actual repository where all committed files are stored.
Diagram Representing Git Workflow:
[ Working Directory ] → git add → [ Staging Area ] → git commit → [ Git Directory ]
7. Understanding Git Internals
The .git directory is where Git stores all data, including commit history, branches, and objects.
Inside .git, the most important subdirectories are:
objects/→ Stores all commits, trees, and blobs.refs/→ Contains references (pointers) to commits.HEAD→ Points to the current branch.config→ Stores repository settings.
8. Key Git Commands and Their Purpose
| Command | Purpose |
|---|---|
git init |
Initializes a new Git repository |
git clone |
Clones a repository |
git status |
Checks the status of files |
git add |
Moves files to the staging area |
git commit -m "" |
Saves changes to the repository |
git log |
Shows commit history |
git branch |
Lists all branches |
git checkout |
Switches between branches |
git merge |
Merges branches |
git fetch |
Fetches updates from a remote repository |
git pull |
Fetches and merges updates |
git push |
Uploads commits to a remote repository |
🔗 Complete List of Git Commands
9. Conclusion
Git revolutionized version control by making it easy to track changes, collaborate, and manage multiple versions of a project efficiently. By understanding the core workflow, file states, internal structure, and key commands, developers can use Git effectively in any development project.
Key Takeaways:
✔ Git works by tracking snapshots of files, not copies.
✔ Files move between Modified → Staged → Committed states.
✔ The .git directory stores all Git history and metadata.
✔ Git is highly efficient, secure, and used by millions of developers worldwide.
Learn More:
🔗Git Official Documentation
🔗 GitHub Guide to Git
Explore more about how to revert a commit in git bitbucket​
