Skybinary

View Categories

2. Understanding Git

3 min read

What is Git, and how does it work? #

Git is a distributed version control system that allows developers to track changes in their codebase, collaborate efficiently, and manage different versions of a project.
It works by recording snapshots of your files and storing them in a repository. Each snapshot (called a commit) represents a specific version of the project. Git saves only the changes (deltas) between versions, not the entire file every time, which makes it fast and efficient.

Git enables multiple developers to work on the same project simultaneously without interfering with each other’s work. It achieves this by allowing branching and merging, where developers can create separate branches for new features or fixes and later merge them back into the main codebase.


Difference between Git and GitHub #

  • Git is the actual tool used for version control on your local machine. It helps you manage your source code history and track changes.
  • GitHub is a cloud-based hosting service that stores Git repositories online. It provides collaboration tools like pull requests, issues, discussions, and CI/CD integrations.

In simple terms:

  • Git = local version control system (works offline)
  • GitHub = online platform for sharing and collaborating using Git

Installing Git (Windows, macOS, Linux) #

Windows:

  1. Visit https://git-scm.com
  2. Download the installer for Windows
  3. Run the setup and follow the instructions (keep default settings)
  4. Verify installation: git --version

macOS:

  1. Install using Homebrew: brew install git or install Xcode Command Line Tools using: xcode-select --install

Linux (Ubuntu/Debian):

sudo apt update
sudo apt install git

Verify installation using:

git --version

Git Configuration #

After installing Git, you need to configure your identity, which will be used in your commits.

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

To verify your configuration:

git config --list

These settings are stored in the ~/.gitconfig file and are applied globally across all repositories on your system.


Git Architecture #

Working Directory #

This is where your project files live. You make changes, edit, delete, and create files here.

Staging Area (Index) #

Before committing, Git lets you stage your changes. The staging area acts as a preview of what will go into your next commit.

Repository (Local & Remote) #

  • Local Repository: Stored on your system. It contains all commits, branches, and project history.
  • Remote Repository: Stored on platforms like GitHub or GitLab. It allows sharing and collaboration between developers.

Core Git Concepts #

Commits and Snapshots #

A commit is a snapshot of your project at a given point in time. Each commit records:

  • The changes made
  • Author information
  • A unique ID (hash)
  • A commit message

You can view commits using:

git log

Branches and Merging #

  • A branch allows you to create an independent line of development.
  • Merging integrates the changes from one branch into another (usually into the main branch).

Example:

git branch feature-1
git checkout feature-1
# make changes
git commit -m "Added new feature"
git checkout main
git merge feature-1

HEAD and References #

HEAD represents your current position in the repository.

  • When you checkout a branch, HEAD points to the latest commit in that branch.
  • When you make a new commit, HEAD moves forward to point to it.

You can see where HEAD is pointing with:

cat .git/HEAD

Hashing (SHA-1) #

Every commit, file, and object in Git is identified using a SHA-1 hash, a 40-character string (e.g., 3a5b9f4d2e9f7c...).
It ensures data integrity — even a small change in a file will result in a completely different hash.

Git uses this hash-based system to detect changes, prevent data corruption, and manage versions securely.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top