Version Control Tutorial: Branching

Working with branches, pull requests, and collaboration

Why Branch?

Imagine: You’re collaborating on a shared project. You want to add a new feature, but your teammate needs the current code to keep working. How do you add your feature without disrupting their work?

Answer: Branching!

Branches let multiple people work on the same project simultaneously - you develop your feature on one branch while your teammates continue using the stable main branch.

What is Branching?

  • Git allows us to create different working versions, or branches, of our repository.

  • This lets you take a snapshot of the repository and try out a new idea without affecting your main branch.

  • If you are collaborating with others, they can keep using (and editing) the main branch while you work in parallel.

  • Once satisfied with your changes, you can merge them back into main using a Pull Request.

Example Git Branching

The Branching Workflow

  1. Pull any code updates! git pull
  2. Create and check out a new branch. git branch and git checkout
  3. Make and push commits to the new branch. git add, git commit, and git push
  4. When ready to merge, create a Pull Request in GitHub
  5. Review the Pull Request
  6. Merge the Pull Request
  7. Check out and pull main branch. git checkout and git pull

Understanding the Main Branch

Git creates a default branch called main.

You could think of the main branch as the trunk of a tree.

Understanding the Main Branch

Git creates a default branch called main.

You could think of the main branch as the trunk of a tree.

Git Bash shows the active branch name to the right of the prompt.

Creating Branches

Create and check out a new branch in Git Bash in one step:

git checkout -b your-name-here
Switched to a new branch 'your-name-here'

After checking out your branch, the branch name should show up next to the command line if you are using Git Bash.

For Our Exercise

  1. Create a blank file called your-name.txt in the poems/ subfolder.
  2. Find a poem on the internet. If you need inspiration, check out poets.org/poems
  3. Copy-paste the text into your-name.txt
  4. Inject the text “TYPO” somewhere in the poem
  5. Save the file
  6. Stage and commit your changes

Pushing Your Branch

After making commits on your branch, push them to GitHub.

The first time you push a new branch, Git will give you an error about no “upstream” branch. Simply run the command Git suggests:

git push --set-upstream origin your-name-here

What is a Pull Request?

A Pull Request (or “PR”) is a GitHub feature that lets you propose merging your branch into another branch (usually main).

  • It’s a request for your code changes to be reviewed and merged.
  • It provides a space for discussion, code review, and approval.
  • Some might say it is a bit of a misnomer. It isn’t related to using git pull to bring changes from GitHub to your local machine.

Creating a Pull Request

Once your changes are ready, create a Pull Request in GitHub:

  1. Navigate to the repository GitHub page

Creating a Pull Request

Once your changes are ready, create a Pull Request in GitHub:

  1. Navigate to the repository GitHub page
  2. Click the “Pull requests” tab

Creating a Pull Request

Once your changes are ready, create a Pull Request in GitHub:

  1. Navigate to the repository GitHub page
  2. Click the “Pull requests” tab
  3. Click “New pull request” button

Creating a Pull Request

Once your changes are ready, create a Pull Request in GitHub:

  1. Navigate to the repository GitHub page
  2. Click the “Pull requests” tab
  3. Click “New pull request” button
  4. Select main as the “base” branch and your new branch as the “compare” branch

Creating a Pull Request

Once your changes are ready, create a Pull Request in GitHub:

  1. Navigate to the repository GitHub page
  2. Click the “Pull requests” tab
  3. Click “New pull request” button
  4. Select main as the “base” branch and your new branch as the “compare” branch
  5. Add a title and description for the Pull Request

Creating a Pull Request

Once your changes are ready, create a Pull Request in GitHub:

  1. Navigate to the repository GitHub page
  2. Click the “Pull requests” tab
  3. Click “New pull request” button
  4. Select main as the “base” branch and your new branch as the “compare” branch
  5. Add a title and description for the Pull Request
  6. Click “Create pull request”

Reviewing Pull Requests

Pull requests provide a great opportunity for collaborators to review code and ensure accuracy.

GitHub makes this process easy by:

  • Identifying lines of code that have changed
  • Allowing collaborators to comment on specific lines
  • Showing additions, deletions, and modifications clearly
  • Providing a structured review workflow

How to Review Code

To review a pull request:

  1. Navigate to the Pull Request and click “Files changed”

How to Review Code

To review a pull request:

  1. Navigate to the Pull Request and click “Files changed”
  2. Comment on your favorite line by clicking the + next to the line number

How to Review Code

To review a pull request:

  1. Navigate to the Pull Request and click “Files changed”
  2. Comment on your favorite line by clicking the + next to the line number
  3. Find the line with “TYPO” injected

How to Review Code

To review a pull request:

  1. Comment on your favorite line by clicking the + next to the line number
  2. Find the line with “TYPO” injected
  3. Suggest a code change to remove the typo using GitHub’s suggestion feature

How to Review Code

To review a pull request:

  1. Find the line with “TYPO” injected
  2. Suggest a code change to remove the typo using GitHub’s suggestion feature
  3. Complete your review: approve, request changes, or just comment. For today, approve.

Addressing Review Feedback

If the reviewer requests changes:

  • Make the requested changes in your branch
  • Commit and push to the same branch
  • The Pull Request will automatically update with your new commits
  • Once approved, you can merge!

Merging Pull Requests

Once approved, merge your changes:

  1. Go to the “Conversation” tab of the Pull Request

Merging Pull Requests

Once approved, merge your changes:

  1. Go to the “Conversation” tab of the Pull Request
  2. Click the “Merge pull request” button at the bottom

The button should be green because your reviewer approved your changes.

Merging Pull Requests

Once approved, merge your changes:

  1. Go to the “Conversation” tab of the Pull Request
  2. Click the “Merge pull request” button at the bottom
  3. Your branch’s changes are now in the main branch!

After Merging

After merging on GitHub, update your local repository:

git checkout main
git pull

After Merging

After merging on GitHub, update your local repository:

git checkout main
git pull

Now your local main branch has all the changes from your merged branch!

Cleaning Up Branches

Ideally, branches are deleted after merging to keep things tidy.

Delete the remote branch on GitHub:

After merging, GitHub shows a “Delete branch” button in the Pull Request.

Delete your local branch:

git branch -d <branch-name>

Handling Conflicts

Sometimes the main branch has changes that conflict with your branch.

GitHub will indicate when there are conflicts that prevent automatic merging.

Handling Conflicts

When conflicts occur:

git checkout main
git pull
git checkout <new-branch-name>
git merge main

Recap

At this point you should understand:

  • ✓ Why branching is essential for collaboration
  • ✓ How to create and check out branches
  • ✓ How to create and review Pull Requests
  • ✓ What to look for when reviewing code
  • ✓ How to merge PRs and clean up branches
  • ✓ How to handle merge conflicts

Next Steps

Continue practicing the branching workflow in your projects!

For more guidance, visit the RFF Data Governance website.

Questions?