Having established that Git operates as a distributed version control system, allowing you to work offline and maintain a full history locally, let's examine the fundamental building blocks that make this possible. Understanding these core concepts is essential before you start using Git commands. They are the repository, the commit, and the branch.
At the heart of every Git-managed project lies the repository, often abbreviated as "repo". Think of the repository as Git's private database for your project. It stores the complete history of all changes, metadata about those changes (like who made them and when), and the pointers (branches) that help organize this history.
When you initialize Git in a project directory (using a command we'll cover soon, git init
), Git creates a special hidden subdirectory named .git
.
A typical project directory structure after initializing Git, showing the hidden
.git
subdirectory alongside your project files.
This .git
directory contains everything Git needs to track your project. It includes configuration files, logs, the actual history data (stored efficiently), and information about branches and tags. Because the entire repository is stored locally within your project folder, operations like viewing history, comparing versions, or creating branches are incredibly fast, as they don't require network access. This self-contained nature is a direct benefit of Git's distributed design.
If the repository is the database, then commits are the individual records that make up the project's history. A commit represents a snapshot of your entire project's tracked files at a specific point in time. It's not just a record of what changed since the last commit; it's a complete picture of the project state as Git knew it when the commit was made.
Each commit is identified by a unique cryptographic hash (specifically, an SHA-1 hash, which looks like a1b2c3d4e5f6...
). This hash is generated based on the content of the files in the snapshot and the metadata associated with the commit.
Key components stored within each commit include:
A simplified view of commit history. Each commit points back to its parent, forming a chain representing the project's timeline.
Think of making a commit like taking a photograph of your work desk just before you start a new task. You capture the state of everything relevant so you can always refer back to exactly how things were at that moment.
A branch in Git is simply a lightweight, movable pointer to a specific commit. Imagine it as a label or a bookmark attached to one of the snapshots (commits) in your history. Branches are fundamental to the Git workflow, allowing you to diverge from the main line of development and work on different features or fixes independently without affecting each other.
By default, the main line of development in a Git repository is usually called main
(though older repositories might use the name master
). When you start making commits, the main
branch pointer automatically moves forward to point to the latest commit you've made on that line.
The
main
branch points to the latest commit (C3).HEAD
indicates thatmain
is the currently active branch.
If you want to work on a new feature, you can create a new branch, let's say called new-feature
. Initially, this new branch pointer will point to the same commit as main
. However, as you make new commits while "on" the new-feature
branch, only the new-feature
pointer will move forward. The main
branch remains unchanged until you decide to merge your new feature back into it.
A new branch
new-feature
created from commit C2. Subsequent commit C4 is only on this branch.HEAD
now points tonew-feature
, indicating it's the active branch.
Git also uses a special pointer called HEAD
. HEAD
typically points to the local branch you are currently working on. When you switch branches, HEAD
updates to point to the new branch. This tells Git where the next commit should be added in the history graph.
Because branches in Git are just simple pointers (stored as files containing the SHA-1 hash of the commit they point to), creating, switching between, and deleting branches is extremely fast and efficient. This lightweight branching model is a significant advantage of Git, encouraging workflows where branches are used frequently for tasks of any size.
These three concepts, repository, commit, and branch, form the foundation upon which all Git operations are built. As you progress through this course, you'll see how commands like git add
, git commit
, git branch
, git checkout
, and git merge
manipulate these core components to manage your project's history effectively.
© 2025 ApX Machine Learning