Skip to content
Tactiverge

Version control concepts (git)

How version control records every change to a project as a dated, described snapshot, and how commits, history, diffs and branches work in git.

Lesson 5 of 6 · 30 min · Self-paced · Free

The folder of "final" files

Most people have seen a folder like this:

controller.py
controller_old.py
controller_v2.py
controller_v2_fixed.py
controller_FINAL.py
controller_FINAL_really.py

Which one is the version that worked at the competition? What changed between v2 and v2_fixed? Nobody knows. Now imagine three teammates each emailing their own copies around. This is the problem version control solves.

A version control system keeps a complete, organised history of a project's files. Instead of copies with hopeful names, you have one set of files plus a record of every saved change: what changed, who changed it, when, and — if you write a good message — why. The most widely used version control system is git, which is free and open-source. This lesson covers the concepts, with git commands as examples.

Repositories and commits

A repository (or repo) is a project folder whose history git is tracking. Git keeps that history in a hidden .git folder inside it.

The basic unit of history is the commit: a snapshot of the tracked files at a moment you choose, together with a short message, the author and the date. Think of a commit as a notebook entry for your files. Each commit gets a unique identifier (a long string of letters and numbers, usually shortened to the first seven characters, like 1c88edf).

Making a commit is a two-step process:

  1. Stage the changes you want to include with git add. This lets you choose exactly what goes into a snapshot.
  2. Commit them with git commit, adding a message.

Here is a short session in a new, hypothetical project. Lines starting with $ are what you type; the rest is git's output (your identifiers will differ).

$ git init -b main
Initialized empty Git repository in /home/sam/paper-bridge/.git/
$ git status --short
?? README.md
$ git add README.md
$ git commit -m "Add README with project goal"
[main (root-commit) 1c88edf] Add README with project goal
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
$ git add measure.py
$ git commit -m "Add first measurement script"
[main 82f52b1] Add first measurement script
 1 file changed, 1 insertion(+)
 create mode 100644 measure.py
$ git log --oneline
82f52b1 Add first measurement script
1c88edf Add README with project goal

git init -b main creates the repository and names its main line of history main (the -b option needs git 2.28 or newer). git status shows what has changed since the last commit; ?? means a file git is not yet tracking. git log --oneline lists the history, newest first.

Before your first commit, git needs to know who you are. You set this once with git config --global user.name "Your Name" and git config --global user.email "you@example.com". The name and email are stored in every commit, so think about what you want to be public if you later share the repository online.

Seeing what changed: diffs

A diff shows exactly which lines changed. Before committing, git diff shows changes you have not staged yet, and git diff --staged shows what is staged and about to be committed:

$ git diff
diff --git a/measure.py b/measure.py
index 5587912..7819e6a 100644
--- a/measure.py
+++ b/measure.py
@@ -1 +1,2 @@
 load_g = 0
+width_mm = 60

A line beginning with + was added; a line beginning with - was removed. Reading your own diff before every commit is an excellent habit: it catches stray debugging lines and accidental edits.

Writing good commit messages

The message is the why of the change. A common convention is a short summary line in the imperative mood, as if giving an instruction to the code:

  • Weak: "stuff", "fixed it", "update"
  • Strong: "Lower steering gain from 0.8 to 0.5 to stop oscillation on the oval track"

A good test: could a teammate understand what changed and why from the message alone, without opening the files? Small commits, each doing one thing, are far easier to understand and undo than one giant commit at the end of the week.

Going back in time

Because every commit is kept, you can recover earlier work:

  • git restore measure.py throws away your unstaged changes to that file and returns it to the last committed version (or to the staged version, if you have already used git add on it). Be careful: those discarded changes are gone.
  • git restore --source=1c88edf measure.py brings back the file as it was in an earlier commit.
  • git show 1c88edf displays a past commit and its changes.
  • git log -- measure.py shows only the commits that touched that file.

Branches: safe experiments

A branch is an independent line of development. Suppose your project works and you want to try a risky idea. Instead of endangering the working version, you create a branch, experiment there, and only bring the changes back if they work:

$ git switch -c try-wider-deck
Switched to a new branch 'try-wider-deck'
$ git commit -am "Try a 60 mm wide deck"
[try-wider-deck acade0b] Try a 60 mm wide deck
 1 file changed, 1 insertion(+)
$ git switch main
Switched to branch 'main'
$ git merge try-wider-deck
Updating 82f52b1..acade0b
Fast-forward
 measure.py | 1 +
 1 file changed, 1 insertion(+)

git switch -c creates a new branch and moves to it (git switch needs git 2.23 or newer; older guides use git checkout -b). git commit -am stages every change to files git already tracks and commits them in one step; it does not include brand-new files, which still need git add. git merge brings the experiment's commits into main. If the idea fails, you simply switch back to main and the working version is untouched. When two branches changed the same lines differently, git reports a merge conflict and asks you to decide which version to keep.

Remotes and collaboration

So far everything lives on one computer. A remote is a copy of the repository somewhere else, often on a hosting service. git clone copies a repository, git push sends your commits to a remote, and git pull fetches and merges other people's commits. This is how teams share one history instead of emailing files around.

What to track, and what not to

Version control works best with text files: code, Markdown documentation, CSV data and BOMs, plain-text notebooks. Git can store any file, including CAD models and images, but for binary files it cannot show a meaningful line-by-line diff, and very large files make repositories slow.

Some things should never be committed: passwords, access keys and other secrets, and files that can be regenerated automatically. List them in a file called .gitignore so git ignores them:

# .gitignore
__pycache__/
secrets.txt

Once a secret is committed and shared, removing it from history is difficult. Treat it as exposed and change it.

Recap

  • Version control replaces piles of copies with one history of dated, described snapshots called commits.
  • Stage with git add, snapshot with git commit, inspect with git status, git diff and git log.
  • Write commit messages that explain why; keep commits small.
  • Branches let you experiment safely; merging brings successful work back.
  • Remotes share history; .gitignore keeps secrets and generated files out.

Project

Software project: give a small project a history

Install git (free, from the official site) and create a new repository for a small project — the README from the earlier lesson, a short Python script, or a CSV of your notebook data.

Make at least five small commits, each with a clear message explaining why. Create a branch, try a change there, and either merge it or abandon it. Read git diff before every commit. Finally, run git log --oneline and copy the output into your notebook.

Deliverables

  • A repository with at least five commits, each with a descriptive message
  • One branch that was either merged or deliberately abandoned, with a note saying which and why
  • A .gitignore file listing at least one thing that should not be tracked
  • The output of git log --oneline copied into your notebook

Knowledge check

Check your understanding

  1. 1. What is a commit in git?
  2. 2. Which commit message is most useful?
  3. 3. Why would you create a branch before trying a risky change?
  4. 4. In a git diff, what does a line starting with + mean?
  5. 5. You accidentally committed a file containing a password and pushed it. What is the right response?

Go further

Version control concepts (git) — Research and Engineering Methodology — Tactiverge