Version Control Basics

Tracking changes with Git: repositories, commits, branches, merges, remotes and good commit messages.

What is it?

Version control records the history of your project's files so you can see what changed, when, why and by whom, go back to any earlier state, and work with other people without overwriting each other's work.

Git is by far the most widely used version control system. Its core ideas:

- Repository (repo): a project folder plus its complete history, stored in a hidden .git directory. - Commit: a saved snapshot of the project at a moment, with a message, an author, a timestamp and a link to the previous commit (its parent). Each commit has a unique ID (a hash, like 3f2a9c1). - Staging area: where you choose which changes go into the next commit (git add). - Branch: a movable label pointing at a commit, letting you develop something in parallel without disturbing the main line (main). - Merge: combine the work from one branch into another. If both changed the same lines, you get a merge conflict to resolve by hand. - Remote: a copy of the repo elsewhere, usually on a host like GitHub or GitLab (origin). You push your commits to it and pull others'.

Git is distributed: every clone holds the full history, so you can commit, branch and view history offline.

Explain like I'm 10

Git is like save points in a video game, with labels describing each save. Branches are like trying a risky path in a separate save slot: if it works out, you merge it into your main game; if not, you delete the slot and nothing is lost.

Examples

Starting a repo and making commits

git init                      # create a new repository here
git status                    # see what's changed / untracked

git add hello.py              # stage a file for the next commit
git commit -m "Add hello script"

# edit hello.py ...
git diff                      # see unstaged changes line by line
git add hello.py
git commit -m "Greet the user by name"

git log --oneline             # view history
# 9c1e4b2 Greet the user by name
# 3f2a9c1 Add hello script

The everyday loop: change files, check status/diff, add what belongs together, commit with a clear message.

A basic branch-and-merge workflow

git clone https://github.com/example/project.git
cd project

git switch -c add-login       # create and switch to a new branch
# ...edit files...
git add .
git commit -m "Add login form"

git push -u origin add-login  # publish the branch to the remote
# open a pull request on GitHub, get a review, then merge it

git switch main
git pull                      # fetch + merge the latest main
git branch -d add-login       # delete the merged branch locally

Work on a short-lived branch, push it, open a pull request for review, merge into main, then update your local main. git switch is the modern form of git checkout for changing branches.

Resolving a merge conflict

$ git merge feature
CONFLICT (content): Merge conflict in greeting.py

# greeting.py now contains:
<<<<<<< HEAD
message = "Hello, world"
=======
message = "Hi there, world"
>>>>>>> feature

# 1. Edit the file to the version you want (remove the markers):
message = "Hi there, world"

# 2. Mark it resolved and finish the merge:
$ git add greeting.py
$ git commit

Git shows both versions between markers: yours (HEAD) above =======, theirs below. You decide the final content, stage it, and commit.

How it works

Git stores snapshots, not just lists of changes. Each commit records the full state of the tracked files (unchanged files are shared with the previous commit, so this stays cheap) and points to its parent commit(s). Together, commits form a chain (more precisely, a graph) of history.

A commit's ID is a hash of its contents, metadata and parent. Change anything and the hash changes, which is how Git detects corruption and why rewriting history produces new IDs.

Three places your changes live:

1. Working directory: the files you edit. 2. Staging area (index): what git add has prepared for the next commit. 3. Repository: committed history.

A branch is just a small file containing a commit ID. Committing on a branch moves its label forward. HEAD points to the branch (or commit) you currently have checked out.

Merging: if main hasn't moved since you branched, Git just moves the label forward (fast-forward). Otherwise it creates a merge commit with two parents. git pull is git fetch (download new commits) followed by a merge (or a rebase, if configured).

working dir ──git add──► staging area ──git commit──► local repo ──git push──► remote
       ▲                                                    │  ▲                    │
       └──────────────────── git switch / restore ──────────┘  └──── git pull ──────┘

  History with a branch and a merge:

  main:      A ─── B ─────────── E  (merge commit, parents B and D)
                    \           /
  feature:           C ─────── D

Why does it exist?

Before version control, people kept copies like report_final_v2_REAL.doc and emailed files around, losing work and overwriting each other. Version control gives a reliable history, a safe way to experiment, a record of why each change was made, and a structured way for many people to work on the same code at the same time.

When to use it

Use it for practically every project, even solo ones:

- Code of any size, configuration files, documentation, infrastructure scripts. - Before risky changes: commit first, so you can always get back. - For collaboration: branches and pull requests let teammates review changes before they reach main.

Writing good commit messages:

- A short summary line (around 50 characters, imperative mood): "Add login form", "Fix crash on empty cart". - A blank line, then a body explaining why the change was needed, if it's not obvious. - One logical change per commit, so history is easy to read and revert.

When not to use it

- Don't commit secrets (passwords, API keys, .env files); history is hard to scrub and remotes may be public. List such files in .gitignore. - Don't commit generated or downloaded files (node_modules/, build output, __pycache__/); regenerate them instead. - Plain Git is a poor fit for large binary files (videos, datasets); use Git LFS or separate storage.

Common mistakes

  • Giant commits mixing unrelated changes with messages like "stuff" or "fix", which make history useless.

  • Committing secrets or .env files. Removing them in a later commit leaves them in history; rotate the secret immediately.

  • Working directly on main for everything instead of using short-lived branches.

  • Panicking at a merge conflict. It's just Git asking you to choose between two edits to the same lines.

  • Using git push --force on a shared branch, overwriting teammates' commits. Prefer --force-with-lease, and only on your own branches.

Practice exercises

  1. Easy:

    Create a new repo, add a file, and make three commits with clear messages. Use git log --oneline and git diff HEAD~1 to view the history and the last change.

  2. Medium:

    Create a branch, change the same line of a file on both the branch and main, merge the branch into main, and resolve the resulting conflict.

  3. Hard:

    In a repo with 20+ commits, introduce a bug in one of the middle commits. Then use git bisect to find it, writing a small script so git bisect run finds it automatically.

Interview questions

What is a commit?

A snapshot of the tracked files at a point in time, plus metadata (author, date, message) and a pointer to its parent commit(s). It's identified by a hash of its contents.

What is a branch in Git?

A lightweight, movable pointer to a commit. Creating one is nearly free; committing on it moves the pointer forward, leaving other branches untouched.

What's the difference between `git fetch` and `git pull`?

fetch downloads new commits from the remote but doesn't change your working branch. pull runs a fetch and then integrates the changes into your current branch by merging (or rebasing, if configured).

What's the purpose of the staging area?

It lets you choose exactly which changes go into the next commit, so one commit can contain one logical change even if you've edited several things.

What is a merge conflict and how do you resolve it?

It happens when two branches change the same part of a file differently and Git can't decide which to keep. Edit the file to the correct final content, remove the conflict markers, git add it, and complete the merge with a commit.

What's the difference between Git and GitHub?

Git is the version control tool that runs locally. GitHub is a hosting service for Git repositories that adds collaboration features: pull requests, code review, issues and CI.

What is a fast-forward merge?

When the target branch has no new commits since the feature branch split off, Git simply moves the target's pointer forward to the feature's latest commit. No merge commit is needed.

What makes a good commit message?

A concise summary in the imperative mood ("Fix crash on empty cart"), optionally followed by a blank line and a body explaining why the change was made. Each commit should contain one logical change.

What is `.gitignore` for?

It lists file patterns Git should not track, such as dependencies, build output, editor files and local secrets. It doesn't affect files already committed; those must be removed with git rm --cached.

Scenario: you accidentally committed an API key and pushed it. What do you do?

Treat it as leaked: revoke and rotate the key immediately. Then remove it from the code and, if needed, rewrite history (for example with git filter-repo) and force-push, coordinating with the team. Rewriting alone isn't enough, because clones and caches may already have it.