Appendix G: Git for Teams


Goals

Note

This is a quick primer on Git, mostly meant as a refresher/reference.

For learning more Git I recommend:

Initializing a Git Repository

From within a directory that you want to treat as a repo:

intermediate-git/$ git init
Initialized empty Git repository in /Users/james/repos/intermediate-git/.git/

That’s all it takes. The current directory you’re in is now a git repository with 0 commits.

It’s a good idea to create a .gitignore file at this point:

.gitignore

If you create your project via GitHub it’ll create a .gitignore file for you. Otherwise, you’d create one yourself.

This file should be a list of files/patterns that you’d like to exclude.

For example:

*.pyc
.vscode
scraped_data/
.DS_Store

This would avoid checking in .pyc files, your local .vscode settings, and the scraped_data directory. MacOS makes .DS_Store files that you probably don’t want to check in either.

Adding A Remote

If you want to push your repo to GitHub, you’ll need to add a remote:

$ git remote add origin git@github.com:jamesturk/git-workshop-example.git

Data Model

Git is what we call a leaky abstraction. This means that it is sometimes necessary to understand how it works under the hood in order to use it effectively.

If you read about Git or use some of the more advanced features you’ll eventually see references to some key data structures:

  • Blobs
  • Trees
  • Commits
  • Tags

Blobs are essentially the contents of a file at a given point in time. Trees are a collection of blobs in a directory-like hierarchy. We don’t need to worry about these too much for what we’re talking about today but I wanted to mention them.

We do want to talk about commits however.

You’re familiar with making commits, but let’s talk a bit more about what is actually stored:

  • Commit ID (a SHA-1 Hash)
  • Author information (name, email)
  • Committer information (name, email) [can be different from author, we won’t worry about this]
  • Commit message
  • Timestamp
  • A reference to the tree at the time of the commit.
  • Parent(s) (zero or more)

graph BT;

    A --> B;
    B --> C;
    C --> D;
    C --> E;
    A --> W;
    W --> F;
    D --> F;
    E --> F;

(I’ll draw git diagrams with the root at the bottom and the most recent commit at the top, which is what you’ll usually see by convention.)

Commits form a Directed Acyclic Graph (DAG).

A is a root commit, because it has no parent.

(Typically repos only have one root commit.)

F is a merge commit, because it has more than one parent.

Branches

TODO: git branch -c

  • Branches are (mutable) labels that point to (immutable) commits.
  • git commit moves the branch that you’re currently on forward.
  • git switch changes which branch you’re currently on.
  • git branch <branchname> creates a new branch.
  • git branch without a branch name will list all of the branches in your repo.

git branch without a branch name will list all of the branches in your repo.

$ git branch
  main
* new-feature
  pr/11
  pr/12
  experiments

A git commit stores metadata as well as a reference to the status of the files at the time of the commit.

This metadata includes:

  • Commit ID (a SHA-1 Hash)
  • Author information (name, email)
  • Commit message
  • Timestamp
  • Parent(s) (zero or more)

When you are working solo, your git history typically is linear.

In the examples below, history runs from top to bottom, and the arrows point from child to parent.

graph BT;

  A[initial commit];
  B[fixed something] --> A;
  C[added test] --> B;
  D[<b>main</b> <br> added README] --> C;

Commits in git are immutable, once you’ve made a commit, you cannot change it.1

The label main is a branch reference, and works like a bookmark that applies to a given commit. Every time you commit, the active branch reference moves forward onto the latest commit.

We can use the git switch command to change our active branch:

The command git switch -c <branchname> creates a new branch and switches to it.

Once a branch exists, you can use git switch <branchname>.

Warning

Older versions of git do not have git switch.

If you’re using a system that doesn’t support git switch the commands git checkout -b and git checkout can be used to create & switch branches instead.

Each commit only has the changes in its parents, so we could use branches to create experimental work that we didn’t yet want on main.

# switch to a branch, any commits made will not be on main
git switch -c red

# after making edits to files
git commit -am "trying red theme"

graph BT;

  A[initial commit];
  B[fixed something] --> A;
  C[added test] --> B;
  D[<b>main</b> <br> added README] --> C;
  E[<b>red</b> <br> trying red theme] --> D;

The new commit only moved the red label forwards.

Switching between the two will reset the state of the repository to the way things were at the most recent commit.

Warning

You generally only switch branches between working on things. This guide will not cover switching between branches when you have uncommitted changes.

So git switch main would switch back to the status of the repository before adding the red theme.

From here, it’d be possible to make another branch with the same parent:

# switch back to main (from red)
git switch main

# make a new branch for blue theme
git switch -c blue

# again, after edits to files
git commit -am "trying blue theme"

# this time, we realize we needed another change
git commit -am "adjusting font color"

Now our tree would resemble:

graph BT;

  A[initial commit];
  B[fixed something] --> A;
  C[added test] --> B;
  D[<b>main</b> <br> added README] --> C;
  E[<b>red</b> <br> trying red theme] --> D;
  F[trying blue theme] --> D;
  G[<b>blue</b> <br> adjust font color] --> F;

Now we have three branch identifiers, we could continue to allow blue and red to develop independently.

Eventually however, we will want to bring one or both back to main.

Merging

Now that we can create branches, we can work on multiple features at once. Whether we’re working alone or on a large team, we’ll eventually want to combine our work.

A branch created multiple nodes that had the same parent.

A merge typically creates a node that has multiple parents.

If we had a commit history like:

graph BT;

  B --> Initial;
  C[...] --> B[...];
  D[red] --> C;
  E[...] --> C;
  G[blue] --> E;
  F[main] --> C;

graph BT;

    A(initial commit : main)
    A --> B(wireframe UI);
    B --> C(add bootstrap CSS: ui);
    C --> D(add profile page: profile-page);
    C --> E(add login page);
    E --> F(fix login page bug: login-page)
    A --> W(backend prototype, very slow : backend);
    W --> X(add benchmarks);
    X --> Y1(optimized via rpython : try-pypy);
    X --> Y2(wrote C version: try-c);
    X --> Y3(rewritten in Rust: try-rust);

We have a lot of different branches here:

  • main
    • ui
      • profile-page
      • login-page
    • backend
      • try-pypy
      • try-c
      • try-rust

Typically, we’ll see branches merge back to their parent, so we can consider the ui and backend branches separately. Let’s look at UI for now:

graph BT;

    A(initial commit : main)
    A --> B(wireframe UI);
    B --> C(add bootstrap CSS: ui);
    C --> D(add profile page: profile-page);
    C --> E(add login page);
    E --> F(fix login page bug: login-page)

TODO: combine

And we decided we were happy with blue, we would want to merge it onto main.

# ensure we are editing the destination branch, main
$ git switch main

# merge in blue
$ git merge blue

This creates a new commit that is the child of both blue and main:

graph TB;

  G[blue] --> E["merge commit"];
  F[main] --> E;
  X[...] --> G;
  Y[...] --> F;

If the destination branch does not have changes of its own, Git may perform a fast-forward merge, where it does not need to create a commit with two parents. Instead, the label main is moved forward to G.

Clean Merges

Let’s continue, and say that it is now time to merge in the profile page.

git switch ui
git merge profile-page

Let’s say profile-page only touched the profile.html file, and login-page only touched login.html. In this case, Git will be able to automatically merge the two branches together.

Auto-merging profile.html
Merge made by the 'recursive' strategy.
 profile.html | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Git will automatically create a new commit with two parents, one for each branch.

graph BT;

    A(initial commit : main)
    A --> B(wireframe UI);
    B --> C(add bootstrap CSS);
    C --> D(add profile page: profile-page);
    C --> E(add login page);
    E --> F(fix login page bug)
    F --> G(merge commit: ui)
    D --> G

Merge Conflicts

If two branches modify the same file(s) there is a risk of a merge conflict.

Perhaps we try to merge the red branch on top of our newly merged main and blue.

But both modified a file base_template.html.

Auto-merging base_template.html
CONFLICT (content): Merge conflict in base_template.html
Automatic merge failed; fix conflicts and then commit the result.

At this point, your repository will be in a “merge conflict” state. Git will have modified the file to show you the conflicts, in this case two different CSS files were added to the HTML:

<title>My Website</title>
<head>
<<< HEAD
<link rel="stylesheet" href="theme/blue.css">
=======
<link rel="stylesheet" href="theme/red.css">
>>> red
</head>
<body>

The <<< HEAD and >>> red lines show you the two different versions of the file split by ======.

The portion between <<< HEAD and ==== is the version of the file that was on the current branch, in this case main.

The portion between ==== and >>> red is the version of the file that was on the branch we’re merging in, in this case red.

We make a choice here, perhaps we are going to support both:

<title>My Website</title>
<head>
<link rel="stylesheet" href="theme/blue.css">
<link rel="stylesheet" href="theme/red.css">
</head>
<body>

Whatever change we make, we would then make a git commit and git push the same way that we would any change.

Aborting a Merge

Sometimes you attempt a merge and discover the conflict will be hard to resolve.

In this case, you can abort the merge with git merge --abort.

This will rewind your repository to the state it was in before you tried to merge, so you can consider other approaches.

Merging and Testing

Of course, this is a trivial example, and in a real merge conflict it can be necessary to figure out how the changed lines should be combined.

If you’re using VS Code or another editor with Git integration, you can use the editor to resolve the conflict. Otherwise, you’ll need to edit the file manually.

Also, note that merge conflicts only occur when the same section of a file was edited in both branches.

If the edit is in completely different parts of the file, git will merge them automatically by default. That doesn’t mean that the code works, as you may find that a change to a function in a different file (or part of the same file) changes how the code works.

This is another reason that tests are so important, as running the tests after a merge can provide some peace of mind that the code still works as expected if your test suite is comprehensive.

It is a good idea to run your tests after a merge, to ensure that the changes on the two branches weren’t in conflict with one another.

(You have good tests for your code right?)

Deleting Branches

When we’re done with a branch we can delete it:

git branch -d login-page

All that this command does is delete the branch reference, the underlying commits will never be deleted.

If you try to delete a branch that isn’t yet merged into the branch you’re working from, Git will warn you and prevent you from doing this.

Caution

If you want to do it anyway, you can use git branch -D.

This is a git command that can cause you to lose work, be careful with it.

Remote Branches

So far, we’ve been working with branches that only exist on our local machine. To share branches with other developers, we need to push them to a remote repository.

Pushing

To work with remote branches, you’ll need a remote set up, which we saw in Part 1. (If you created/cloned the repo from GitHub a remote already exists).

To push a branch to GitHub:

# push the ui branch to the origin remote
git push origin ui

If you’d like to be able to just type git push to push the current branch, you can set up a default remote branch:

# sets it as default
git push -u origin ui

From then on, you can just type git push to push the ui branch to the remote.

Fetch & Pull

If you want to pull a remote branch that exists on the remote but not locally (e.g. to check out a teammates work), you can use git fetch:

# fetch the login-page branch from the origin remote
git fetch origin login-page

This will create a local branch called origin/login-page that you can check out & work with as usual.

If your intent is to merge all of the changes from the remote branch into your current branch, you can use git pull:

# fetch the login-page branch from the origin remote, and merge it into the current branch
git pull origin login-page

Deleting Remote Branches

If you want to delete a remote branch, you can use git push with the --delete flag:

git push origin --delete login-page   # delete the login-page branch from the origin remote

(You can also do this from GitHub’s web interface, which is handy if you’re using Pull Requests.)

Working in Teams

When working solo you may find branches useful, or you may prefer to stick to a trunk-based workflow without branching.

When working in teams however, branches are close to essential.

Team members can do their work on branches, merging to a central branch (often main) after work is ready.

“GitHub Flow”

A model that works well for solo work or small to mid-sized teams is the “GitHub Flow” model.

In this model there is only one long-lived branch, usually called main.

All work is done on feature branches, which are merged into main when they are ready.

This means you never commit directly to main, the only commits on main are merges from feature branches.

General workflow:

  • Create a feature branch aimed at tackling a specific problem
  • Make commits on the feature branch as needed
  • When the feature is ready, open a pull request. This lets the team review the code and discuss it.
  • Once the pull request is approved, merge it into main.
  • Delete the feature branch.

You can read more about this workflow here: https://docs.github.com/en/get-started/quickstart/github-flow

GitHub Pull Requests

Whether or not you adopt “github flow”, you may still want to use pull requests instead of manually merging to main.

This is a process that allows team members to submit work on its own branch for review, gather feedback & improve their branch, and when ready– it can be merged into main.

GitHub has a guide on using Pull Requests that you should read if you plan to use them.

Branches off Branches?

It is possible to branch off of any branch, not just main. You may do this if exploring multiple options on your own feature branch, for instance.

Especially until you’re comfortable working with branches, I would recommend minimizing the number of branches you have at once.

In general, the longer a branch lives the harder it becomes to merge back to main. Small, short-lived branches are much less likely to have merge conflicts or other issues.

Good Commit Messages

If you are working solo, you may not write commit messages that are particularly descriptive:

  • “made edits”
  • “more edits…”
  • “ughhh not working”
  • “finally done”
  • “clean up”

But, when working with a team, it is important to write a message that will help others understand the change:

A good commit message should:

  • Have a first line that is a summary of the change (<50 characters)
  • Have a blank line after the summary
  • Have a more detailed description of the change as needed
  • Be written in the imperative (e.g. “Add” instead of “Added”)
  • Explain why the change was made, not what the change was (since that will be in the diff)

Tagging

Tags are a way to mark a specific commit as important. A common use is to tag releases. (e.g. v0.6.2 or 2023-04-05)

Tags are distinct from branches in that they do not move when new commits are added, but are similar in that they are just a pointer to a commit.

https://git-scm.com/book/en/v2/Git-Basics-Tagging

Further Reading


  1. There are commands that will “replace” a commit, but this creates a new commit ID and thus a new commit.↩︎