Foundations to Frontier
Tests
C. Programming, Tools and HPC › C2 Software Engineering for Research

Git and GitHub: Branching and Pull Requests

Git records a project's history as a graph of immutable, content-addressed snapshots; branches are movable names for commits; merging combines lines of work; GitHub adds collaboration around pull requests: review, CI checks and discussion. This topic explains the object model (blobs, trees, commits, SHA hashes), the everyday commands, branching strategies, merge vs rebase, resolving conflicts, and good PR practice for research code. In the exercises you reproduce Git's object hashing and graph algorithms (ancestors, merge base, fast-forward, topological log), then implement a three-way merge and measure how conflict risk explodes with branch lifetime.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Save points you can share

Writing code is like writing a long report with a team. You want save points you can go back to, a way to try an idea without wrecking the working version, and a way to combine everyone’s changes. Git gives you all three: each commit is a save point of the whole project, a branch lets you experiment on a copy of the timeline, and a merge brings the timelines back together. GitHub hosts the project so a team can review each other’s changes in pull requests before they’re merged.

Branch, commit, merge, rebase
mainfeatc0↑main↑feature
merge base c0 · main is 0 ahead · feature is 0 ahead
git init; git commit -m "initial"
Every commit points to its parent(s); a branch is just a movable name for one commit. Merging diverged branches creates a commit with two parents; if one branch is an ancestor of the other, Git simply moves the name (fast-forward). Rebasing rewrites the feature commits as new commits on top of main: a linear history, but new commit ids, so never rebase commits others have pulled. The longer both sides keep committing past the merge base, the more likely a conflict.
Why engineers care

Every result in a paper or a design review should trace back to an exact version of the code. Git provides that traceability (tag the commit that produced the figures), and pull requests with review and automated tests catch bugs before they reach a mission-critical analysis or a robot.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A commit is…
  2. 2. A branch lets you…
  3. 3. A pull request is…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Everyday commands

git clone URL; cd repo
git switch -c fix-eps-telemetry        # new branch
# edit files …
git status; git diff                   # what changed?
git add src/eps.py tests/test_eps.py   # stage
git commit -m "fix(eps): clamp SoC estimate to [0, 1]"
git push -u origin fix-eps-telemetry   # then open a pull request on GitHub
git switch main; git pull              # update after the merge
git log --oneline --graph --all        # see the history

.gitignore keeps generated files, data and secrets out of the repository. Never commit credentials; use environment variables or a secrets manager.

The three areas

Working tree (your files) → git add → staging area (the next commit’s contents) → git commit → repository (history). git restore undoes working-tree changes; git restore --staged unstages.

📝 Quick check (Level 1)
0/3 answered
  1. 1. git add moves changes into…
  2. 2. Which command creates and switches to a branch?
  3. 3. Where should data files and secrets go?

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

How Git stores history

Every object is named by the SHA-1 hash of its content (SHA-256 in newer repositories):

  • blob: file contents, hashed as "blob <size>\0" + content.
  • tree: a directory listing (names → blob/tree ids).
  • commit: a tree id, parent commit id(s), author, message.

Change one byte and every id above it changes, so history is tamper-evident. A branch is a file containing one commit id; HEAD says which branch you’re on.

Merge vs rebase

  • Merge: if main is an ancestor of feature, main fast-forwards (just moves). Otherwise Git makes a merge commit with two parents, combining changes since the merge base (best common ancestor) by a three-way merge.
  • Rebase: replays your commits on top of the new main as new commits, giving a linear history. Never rebase commits others have already pulled.

Pull-request practice

Small, focused PRs (one idea, a few hundred lines at most), a clear description (what, why, how tested), CI green before review, reviewers check logic and tests, squash or merge per team convention. Short-lived branches merged often (trunk-based development) avoid painful conflicts.

🐍 Exercise 1: Git's object model and history graph
Compute Git blob ids (SHA-1 over 'blob <size>\0' + content) and check them against real git values; then, on a small commit graph with a merge, implement ancestors, the merge base, a fast-forward test and a topological log (children before parents).
📝 Quick check (Level 2)
0/3 answered
  1. 1. A fast-forward merge happens when…
  2. 2. Rebasing published commits is risky because…
  3. 3. A three-way merge compares ours and theirs with…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Three-way merge logic

For each region, with base B, ours O and theirs T:

  • O = T: take it.
  • O = B: only they changed it, take T.
  • T = B: only we changed it, take O.
  • Otherwise it is a conflict, marked <<<<<<<, =======, >>>>>>> for a human to resolve.

Real Git aligns regions with a diff algorithm (Myers, histogram) and handles insertions and deletions, but the decision rule is the same.

Why long-lived branches hurt

If a file has N regions and each branch independently changes k of them, the probability that at least one region was changed by both is

P(conflict)=1−(N−kk)/(Nk)≈1−e−k2/N,P(\text{conflict}) = 1 - \binom{N - k}k\Big/\binom Nk\approx1 - e^{-k^2/N},

a birthday-problem effect: it grows with the square of the work done on each side. Merging small changes often keeps k small.

Bisecting

git bisect binary-searches the history for the commit that introduced a bug: ⌈log⁡2n⌉\lceil\log_2 n\rceil test runs for n commits (see C2.6).

📝 Quick check (Level 3)
0/3 answered
  1. 1. Ours changed a line, theirs left it as in base. Result:
  2. 2. Conflict probability grows roughly like…
  3. 3. Bisecting 1000 commits needs about…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Merging, measured

Exercise 1. Your blob_hash reproduces Git exactly: “hello world\n” → 3b18e512… and the empty file → e69de29b…. On the sample graph, merge-base(G, H) = E, C can fast-forward to G, and the topological log is G F E D C B A.

Exercise 2: conflict risk vs branch lifetime (a 500-line file, each branch edits 3 random lines per day, 400 simulated merges per point):

branch lifetime simulated P(conflict) exact
1 day 0.01 0.018
3 days 0.16 0.152
1 week 0.60 0.601
2 weeks 0.97 0.979
30 days 1.00 1.000

A one-week branch is more likely than not to conflict. Real edits cluster in “hot” regions, which makes this worse.

🐍 Exercise 2 (research): why merge early and often
Implement a line-wise three-way merge with conflict markers, simulate two branches editing random lines of a shared file for different numbers of days, measure the conflict rate, and compare it with the exact overlap probability.

Git in AI, robotics and aerospace

  • Traceability: tag the commit that produced each figure, model or flight-software build.
  • Pull requests with CI: tests, linters and formatting checks before merge (C2.2, C2.3).
  • Large files: Git LFS or DVC for datasets and model weights; keep them out of normal history.
  • Monorepos and ROS workspaces: submodules and workspace tools manage many packages.

References

  1. S. Chacon, B. Straub (2014). Pro Git, 2nd ed. Apress (free at git-scm.com/book).
  2. E. W. Myers (1986). “An O(ND) difference algorithm and its variations.” Algorithmica, 1.
  3. G. Wilson et al. (2017). “Good enough practices in scientific computing.” PLOS Computational Biology, 13(6).
  4. Conventional Commits specification (conventionalcommits.org).
  5. GitHub Docs: About pull requests, About protected branches.