Git Version Control — Branches, Commits, and Collaborative Development

Version Control: Never Lose Your Work Again

The System That Tracks Every Change, by Every Person, Forever

In 2005, the Linux kernel -- 25 million lines of code contributed by 15,000+ developers across every timezone -- needed a way to track every change, by every person, without conflicts destroying anyone's work. Linus Torvalds, who created Linux, built Git in 10 days. Today, 100 million developers use Git. It is the foundation of all software collaboration -- and its core concept is so useful that writers, lawyers, and designers have adopted it too.

Before Git, the standard approach was naming files report_v2_final_ACTUAL_final_fixed.docx. Everyone has been there. You make a copy "just in case," then another copy, then the copies multiply, and within a week nobody knows which version is correct. Version control solves this permanently. It tracks every change, who made it, when they made it, and why -- and lets you undo any of them. It is unlimited Ctrl+Z for your entire project, with a complete audit trail attached.

This is not optional knowledge. Every software company, every open-source project, every development team on Earth uses version control. If you plan to write code professionally -- or even collaborate with anyone on a shared project -- version control is as fundamental as knowing how to save a file.

100M+
Developers on GitHub, the largest Git hosting platform
330M+
Public repositories on GitHub -- 330 million open-source projects
10 days
Time Linus Torvalds spent building the first version of Git
25M
Lines of code in the Linux kernel -- all tracked by Git

The Problem Git Solves

Without version control, collaboration on a shared codebase is chaos. Imagine three developers working on the same project folder on a shared drive. Developer A changes the login page. Developer B changes the login page at the same time but in a different way. Developer C accidentally deletes a file that A was depending on. Nobody knows what changed, when, or why. Rolling back to yesterday's version means overwriting everything that happened today -- including changes you wanted to keep.

This is not a hypothetical scenario. It is what happened at every software company before version control became standard. Files were lost. Work was overwritten. Bugs were introduced and nobody could figure out which change caused them. Hours of work vanished because someone saved over the wrong file.

Git solves all of this with one core idea: every save is a snapshot of the entire project, and every snapshot is permanent, identified, and reversible. You never lose work. You always know who changed what. You can jump to any point in your project's history instantly. And multiple people can work on the same files simultaneously without overwriting each other.

Without Version Control

Recovering from mistakes: Hope you have a recent backup. Manually compare files to find what changed. Lose hours of work regularly.

Collaboration: "Don't edit that file, I'm working on it." Email files back and forth. Merge changes by hand.

History: report_v1.doc, report_v2_final.doc, report_v2_final_REAL.doc. No idea who changed what or when.

With Git

Recovering from mistakes: git revert undoes any commit instantly. git log shows exactly when the bug was introduced. Zero work lost.

Collaboration: Everyone works on their own branch. Changes merge automatically. Conflicts are detected and resolved explicitly.

History: Every change has a message, author, timestamp, and unique ID. Full audit trail forever.

Git Basics: Add, Commit, Push

Git's workflow revolves around three areas and three commands. Understanding these is understanding 80% of daily Git usage.

Your working directory is the folder on your computer where you edit files. This is where you write code, save changes, and do your work. Git watches this folder but does not automatically track anything.

The staging area (also called the index) is a holding zone. When you are ready to save a snapshot, you choose which changes to include by staging them. This lets you make 10 changes but only commit 3 of them -- keeping the other 7 for a separate commit later.

The repository is Git's database of all snapshots. Every commit you make is stored permanently in the repository with a unique identifier, a message, an author, and a timestamp.

Git Staging Workflow Working Directory Your files as you edit them modified files git add Staging Area Changes ready for next snapshot staged changes git commit Local Repository Permanent snapshots on your machine committed history git push Remote (GitHub) shared with team git add selects which changed files to include in the next snapshot. git commit saves a permanent snapshot with a descriptive message. git push uploads your commits to a remote server (GitHub) so others can access them.
The Git staging workflow. Files move from your working directory to the staging area (via git add), then to the local repository (via git commit), and finally to the remote repository (via git push). Each step is deliberate -- you choose what to stage, what to commit, and when to share. This gives you precise control over your project's history.

Here is what a typical workflow looks like in practice:

# You edited login.js and styles.css
git add login.js styles.css        # Stage both files
git commit -m "Fix login button alignment and color"  # Save snapshot
git push origin main               # Upload to GitHub

The -m flag adds a commit message -- a short description of what you changed and why. Good commit messages are one of the most underrated skills in software development. "Fixed stuff" is useless. "Fix login button misalignment on mobile caused by missing flexbox property" tells your future self (and your teammates) exactly what happened.

Real-World Example

The Linux kernel receives approximately 10,000 commits per release cycle (roughly every 9-10 weeks). Each commit is a discrete, documented change by a specific developer. When a bug appears, the team can use git bisect to binary-search through thousands of commits and find the exact one that introduced the problem -- often within minutes. Without version control, finding one bad change among 10,000 would be like searching for a specific grain of sand on a beach.

Branches: Parallel Universes for Your Code

Branches are Git's most powerful feature. A branch is a separate line of development -- a parallel universe where you can experiment, build features, fix bugs, or try wild ideas without affecting the original code. If the experiment works, you merge the branch back. If it fails, you delete the branch. Zero risk to the main codebase.

The main branch (previously called master) is the primary branch -- the canonical version of your project. In a professional setting, the main branch represents production code: the version that users see. Nobody commits directly to main. Instead, you create a branch, do your work there, and merge it back after review.

git branch feature-dark-mode      # Create a new branch
git checkout feature-dark-mode    # Switch to it
# ... make changes, test them ...
git add .
git commit -m "Add dark mode toggle with CSS custom properties"
git checkout main                 # Switch back to main
git merge feature-dark-mode       # Bring dark mode changes into main
Git Timeline: Branching and Merging main C1 Initial commit C2 Add homepage C3 Fix typo feature-dark-mode C4 Add toggle C5 Style dark M Merge! Dark mode is now in main C6 Bug fix (other dev) Main branch commits -- production code Feature branch commits -- isolated experimental work Merge commit -- feature integrated into main
A Git timeline showing branching and merging. The main branch progresses with commits C1-C3 and C6. A feature branch splits off at C3 for dark mode work (C4, C5). Meanwhile, another developer makes a bug fix on main (C6). When the feature is ready, the branch merges back into main. Git combines both lines of history into the merge commit. No work is lost from either branch.

The power of branches becomes clear in teams. Five developers can each work on their own branch simultaneously. Developer A builds a new feature. Developer B fixes a bug. Developer C refactors the database layer. Developer D updates the documentation. Developer E experiments with a new algorithm. All five work in parallel, on the same codebase, without ever touching each other's work. When each is ready, they merge their branch into main one at a time, resolving any conflicts along the way.

GitHub: Git's Social Network

Git is the version control system. GitHub is the platform built on top of Git that adds collaboration features. The distinction matters: Git works locally on your computer. GitHub is the remote server where teams share their repositories and coordinate work.

GitHub's killer feature is the pull request (PR). When you finish work on a branch, instead of merging it yourself, you open a pull request. This notifies the team: "I have changes ready for review." Other developers read your code, leave comments, suggest improvements, and approve or request changes. Only after approval does the code merge into main. This process -- called code review -- catches bugs, spreads knowledge across the team, and maintains code quality.

Create a branch
Make changes and commit
Push branch to GitHub
Open a pull request
Team reviews code
Approved and merged

Beyond pull requests, GitHub hosts 330+ million public repositories, making it the world's largest collection of open-source code. Every major programming language, every popular framework, every tool you use -- its source code is likely on GitHub. You can read it, learn from it, and contribute to it. The Issues tab tracks bugs and feature requests. GitHub Actions automates testing and deployment. GitHub Pages hosts static websites for free.

Merge Conflicts: When Two People Edit the Same Line

Merge conflicts are the part of Git that scares beginners. They sound catastrophic. They are not. A merge conflict happens when two branches change the same line of the same file, and Git cannot automatically decide which version to keep. So it asks you.

Here is what a conflict looks like in the file:

<<<<<<< HEAD
  background-color: #3b82f6;
=======
  background-color: #10b981;
>>>>>>> feature-dark-mode

Git is saying: "The main branch has blue (#3b82f6). The feature branch has green (#10b981). Which one do you want?" You decide. You edit the file to keep one version, the other, or a combination. Then you save, stage, and commit. Conflict resolved.

Conflicts are not errors. They are decisions. Git handles 95% of merges automatically. It only stops to ask when it genuinely cannot determine the right answer. The fear of merge conflicts is almost always worse than the conflicts themselves.

Key Insight

Merge conflicts are a feature, not a bug. They mean Git detected that two developers changed the same code and is asking a human to make the judgment call. The alternative -- silently choosing one version and discarding the other -- would cause far worse problems. A conflict takes 30 seconds to resolve. A silently overwritten change can take days to discover.

Real-World Git Workflow: How Teams Actually Work

Solo developers use Git as a time machine -- they commit regularly, branch for experiments, and push to GitHub as a backup. Teams use a more structured workflow.

1
Main branch is sacred

The main branch always represents working, deployable code. Nobody commits directly to it. All changes go through pull requests. Some teams add automated tests that must pass before a PR can be merged -- this is called CI/CD (Continuous Integration/Continuous Deployment).

2
Feature branches for each task

Every new feature, bug fix, or improvement gets its own branch. Naming conventions help: feature/dark-mode, fix/login-crash, refactor/database-queries. Each branch is short-lived -- ideally merged within a few days, not weeks.

3
Pull requests for review

When a feature is ready, the developer opens a PR. At least one other team member reviews the code: reading for bugs, checking for edge cases, suggesting improvements, verifying the approach. Code review is where teams learn from each other and catch problems before they reach users.

4
Automated testing before merge

GitHub Actions or a similar CI tool runs automated tests every time a PR is opened or updated. If any test fails, the PR cannot be merged. This catches regressions -- changes that accidentally break existing features -- before they reach production.

5
Merge and deploy

After review and passing tests, the PR is merged into main. In many teams, merging to main automatically triggers deployment -- the code is live on the production server within minutes. This is the "Continuous Deployment" part of CI/CD.

This workflow scales from two-person startups to companies with thousands of engineers. Google, Microsoft, Amazon, and Meta all use variations of this process. The tools differ (Google uses an internal system called Piper instead of Git), but the pattern is identical: isolated work on branches, review before merging, automated testing, and a protected main branch.

Beyond Code: Who Else Uses Version Control

Git was built for code, but the concept applies far more broadly. Writers use Git to track drafts and revisions without losing earlier versions. Lawyers use version control for contract revisions -- tracking exactly what changed between draft 3 and draft 4. Data scientists version their datasets and models to reproduce experiments. Designers version Figma files and design tokens. Governments have published legislation on GitHub (the German federal law repository is on GitHub) to track amendments and changes with full transparency.

The underlying principle is universal: any work that changes over time benefits from a system that tracks every change, attributes it to a person, and allows reversal. That principle predates Git by centuries -- librarians, archivists, and accountants have always maintained audit trails. Git just made it automatic, distributed, and free.

Real-World Example

The entire open-source software ecosystem runs on Git. When a developer in Brazil finds a bug in a library maintained by a developer in Finland, they fork the repository (create a personal copy on GitHub), fix the bug in their fork, and submit a pull request. The Finnish maintainer reviews the fix, approves it, and merges it. The fix is now available to every developer who uses that library. This process happens millions of times per day across GitHub. It is the largest collaborative effort in human history, and it is built entirely on Git.

Answers to Questions Students Actually Ask

Git seems complicated. Is it worth learning? The core workflow (add, commit, push, branch, merge) takes a day to learn and a week of practice to internalize. Advanced features (rebasing, cherry-picking, reflog) take longer but are rarely needed. The payoff is immediate: you will never lose work again, you can experiment fearlessly, and you meet the baseline requirement for every development job. There is no professional development environment that does not use Git.

What is the difference between Git and GitHub? Git is the tool -- it runs locally on your computer and tracks changes to files. GitHub is a web platform that hosts Git repositories remotely, adds collaboration features (pull requests, issues, code review), and serves as the social network for open-source development. GitLab and Bitbucket are alternatives to GitHub that serve the same purpose. You can use Git without GitHub, but most teams use both.

Should I use the command line or a visual Git tool? Start with the command line. It forces you to understand what each command does rather than clicking buttons. Once you are comfortable, a visual tool like GitKraken, Sourcetree, or the Git integration in VS Code can speed up common operations and make branch visualization easier. But when something goes wrong -- and it will -- the command line is where you fix it. Every Git tutorial, every Stack Overflow answer, every troubleshooting guide uses the command line.

How often should I commit? Commit when you complete a logical unit of work. Fixed a bug? Commit. Added a feature? Commit. Refactored a function? Commit. The rule of thumb: each commit should be a single, coherent change that you could describe in one sentence. Do not commit half-finished work to the main branch. Do not batch 20 unrelated changes into one commit. Think of commits as chapters in a book -- each one should tell a complete, focused story.

What if I accidentally commit something I should not have (a password, a large file, a secret)? This is a genuine risk. If you push a file with credentials to a public GitHub repository, bots will find it within minutes. Prevention: add a .gitignore file to your project that lists files and patterns Git should never track (.env, node_modules/, *.secret). If credentials do leak, rotate them immediately (change the password, revoke the API key) -- do not just delete the commit, because Git history is permanent and anyone who pulled the repository already has it.

Where Version Control Takes You Next

Version control is the connective tissue of software development. It connects to every other topic. CI/CD (Continuous Integration/Continuous Deployment) automates testing and deployment every time code is merged -- it is built on top of Git events. DevOps treats infrastructure as code, storing server configurations in Git repositories and deploying them through the same branch-review-merge process as application code. Open source runs entirely on Git and GitHub -- contributing to open source is contributing through pull requests. Code review is a skill in itself, built on the ability to read diffs (the changes between two commits) and provide constructive feedback.

Every concept in this page -- commits, branches, merges, pull requests, code review -- is something you will use daily if you write code for a living. It is not an exaggeration to say that Git is the single most universally required tool in software development. Editors, languages, frameworks, and platforms all vary by company and team. Git does not. Learn it well and it pays dividends in every project you ever touch.

The takeaway: Git tracks every change to your project with permanent, identified, reversible snapshots. The core workflow -- add, commit, push -- captures your work. Branches let you experiment without risk. Pull requests enable code review and collaboration. GitHub hosts 330 million repositories and 100 million developers. Merge conflicts are decisions, not disasters. Whether you are working solo or on a team of thousands, version control means never losing work, always knowing what changed, and being able to undo anything. It is the first tool every developer learns and the last one they would give up.