This lesson is adapted from materials from a previous course, ITMG 533. Gemini Code Assist was also active during the development of these materials, and some text was generated with its help.
Learing Objectives
By the end of this chapter, you will be able to:
Explain when and why different branching strategies should be used
Use Git and GitHub to collaborate on a codebase
Collaborate with AI agents on coding tasks
Perform a code review of an AI agent’s changes to a codebase
Review: Git basics
Start by creating and/or updating a local copy of the repository
git init: Initializes a new git repository
git clone: Copies a repository from the cloud
git pull: Pull changes from the cloud to an existing local copy
Make changes and commit
git add: Adds changes to the staging area - ready for commit
git commit: Create a commit comprising all changes in the staging area
Share commits
git push: Push all commits to the cloud
Code
graph TD E[Clone/Pull from cloud] --> A[Make changes] F[Initialize repo] --> A A --> B[Stage changes] B --> C[Commit changes] C --> D[Push changes to cloud]
graph TD
E[Clone/Pull from cloud] --> A[Make changes]
F[Initialize repo] --> A
A --> B[Stage changes]
B --> C[Commit changes]
C --> D[Push changes to cloud]
TipGit needs to be told what to do
Git will not guess what you want, and git will not automatically do things for you. You have to tell it what to do.
For anyone who is annoyed when your text editor automagically knows what you want better than you do and reformats your entire document, this is a feature.
Branching Strategies
Why use branches? To enable parallel development by isolating the main codebase from
New features
Bug fixes
Experiments
A number branching strategies have been proposed, each with different use cases (Haddad 2022).
GitFlow
GitFlow is a heavy, structured branching strategy proposed by Driessen (2010) that has received a lot of attention over the years.
Main branches:
master or main: Stable, production-ready code
dev or develop: Latest integrated code for the next release
Supporting branches:
feature/*: New features development
release/*: New production release preparation
hotfix/*: Quick patches to production issues
Pros:
Allows for parallel development on a large scale
Excellent for managing multiple versions of a product.
Cons:
Can be overly complex and cumbersome for simple projects.
Slows down the CI/CD process.
GitFlow diagram
GitHub Flow
GitHub Flow is a simpler, continuous-delivery-focused strategy (“GitHub Flow” n.d.), where the main branch is always in a deployable state.
Workflow:
Create a descriptive feature branch from main.
Commit changes to the feature branch.
Open a Pull Request (PR) to discuss and review changes.
Merge the feature branch into main.
Deploy to production.
Pros:
Simple, easy to understand, and fast.
Ideal for teams with continuous integration and deployment.
Cons:
Not suitable for managing multiple versions or environments.
Requires a high level of testing and discipline to keep main stable.
GitHub Flow diagram
GitLab Flow
GitLab Flow is a hybrid strategy that combines elements of GitFlow and GitHub Flow (“What Is GitLabFlow?” n.d.).
Key Feature: Uses environment branches (e.g., production, staging) in addition to the main branch.
Workflow:
Start a feature branch from main.
Open a Merge Request (MR) to merge into main.
main branch is automatically deployed to a testing environment.
Merge main into a staging branch, which is deployed to the staging environment.
Merge staging into a production branch, which is deployed to the production environment.
Pros:
Provides more structure and isolation than GitHub Flow.
Works well for projects with multiple deployment environments.
Cons:
Doesn’t include extra branches for management of multiple versions
Parallel development for large projects may be challenging
GitLab Flow diagram
Trunk-Based Development
Trunk-based development is a strategy where developers merge small, frequent changes directly into a single, shared “trunk” or main branch.
Core principle: All developers work on the same branch.
Key practices:
Commit small, isolated changes multiple times a day.
Use feature flags to hide incomplete work from users.
The main branch is always in a releasable state.
Pros:
Enables rapid and continuous integration and delivery.
Minimizes merge conflicts and “merge hell.”
Cons:
Requires a mature, disciplined team and strong automated testing.
Can be challenging for new developers.
Choosing a Strategy
Simplicity vs. Complexity: Choose a strategy that matches your team’s size and project’s needs.
Release cycle: A strategy should align with your release cadence (e.g. frequent, continuous, or scheduled).
Team discipline: The simpler the strategy, the more discipline is required from the team.
Collaboration with AI Agents
Collaborating with yourself (using branches) is the first step
Collaborating with others requires additional coordination
Collaborating with AI agents adds new challenges
Planning
Plan before you code
Tell the agent what we want and why
Have it generate a high level plan (e.g. Plan.md)
Define roles and responsibilities
Establish coding standards and guidelines
Context engineering
Explicitly provide the agent with sufficient context (especially for larger code bases)
Relevant files
Documentation
Coding standards
Task to focus on
Test driven development
Ask the agent to generate tests before implementing features
Have the agent continue the PAOR cycle until tests pass
Review and run tests frequently
Code reviews
Always review the agent’s code changes
Use pull requests or merge requests for changes
Provide feedback and request changes as needed
Review Questions
What are the main differences between GitFlow and GitHub Flow?
When would you choose GitHub Flow over GitLab Flow?
Describe one collaboration best practice for working with AI agents?
Describe how to use test-driven development when collaborating with an AI agent?
How would you use GitHub Flow to manage a new feature development with an AI agent?