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
Pull any code updates! git pull
Create and check out a new branch. git branch and git checkout
Make and push commits to the new branch. git add, git commit, and git push
When ready to merge, create a Pull Request in GitHub
Review the Pull Request
Merge the Pull Request
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
Create a blank file called your-name.txt in the poems/ subfolder.
Find a poem on the internet. If you need inspiration, check out poets.org/poems
Copy-paste the text into your-name.txt
Inject the text “TYPO” somewhere in the poem
Save the file
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:
Navigate to the repository GitHub page
Creating a Pull Request
Once your changes are ready, create a Pull Request in GitHub:
Navigate to the repository GitHub page
Click the “Pull requests” tab
Creating a Pull Request
Once your changes are ready, create a Pull Request in GitHub:
Navigate to the repository GitHub page
Click the “Pull requests” tab
Click “New pull request” button
Creating a Pull Request
Once your changes are ready, create a Pull Request in GitHub:
Navigate to the repository GitHub page
Click the “Pull requests” tab
Click “New pull request” button
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:
Navigate to the repository GitHub page
Click the “Pull requests” tab
Click “New pull request” button
Select main as the “base” branch and your new branch as the “compare” branch
Add a title and description for the Pull Request
Creating a Pull Request
Once your changes are ready, create a Pull Request in GitHub:
Navigate to the repository GitHub page
Click the “Pull requests” tab
Click “New pull request” button
Select main as the “base” branch and your new branch as the “compare” branch
Add a title and description for the Pull Request
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:
Navigate to the Pull Request and click “Files changed”
How to Review Code
To review a pull request:
Navigate to the Pull Request and click “Files changed”
Comment on your favorite line by clicking the + next to the line number
How to Review Code
To review a pull request:
Navigate to the Pull Request and click “Files changed”
Comment on your favorite line by clicking the + next to the line number
Find the line with “TYPO” injected
How to Review Code
To review a pull request:
Comment on your favorite line by clicking the + next to the line number
Find the line with “TYPO” injected
Suggest a code change to remove the typo using GitHub’s suggestion feature
How to Review Code
To review a pull request:
Find the line with “TYPO” injected
Suggest a code change to remove the typo using GitHub’s suggestion feature
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:
Go to the “Conversation” tab of the Pull Request
Merging Pull Requests
Once approved, merge your changes:
Go to the “Conversation” tab of the Pull Request
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:
Go to the “Conversation” tab of the Pull Request
Click the “Merge pull request” button at the bottom
Your branch’s changes are now in the main branch!
After Merging
After merging on GitHub, update your local repository:
git checkout maingit pull
After Merging
After merging on GitHub, update your local repository:
git checkout maingit 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 maingit pullgit 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!