GitHub Flow
Introduction to GitHub Flow
Section titled “Introduction to GitHub Flow”GitHub Flow is a lightweight, branch-based workflow that supports teams and projects where deployments are made regularly. It is designed to be simple and effective for continuous deployment.
The core idea behind GitHub Flow is that:
- Anything in the main branch is deployable
- Start new work by creating clearly named branches from main
- Commit to that branch locally and regularly push your work to the same named branch on the remote
- When you need feedback or help, or you think the branch is ready for merging, open a pull request
- After someone reviews and signs off on the feature, you can merge it into main (If you’re working solo, you can review and merge your own PR)
- Once it is merged and pushed to main, you can and should deploy immediately
Setting up GitHub Flow
Section titled “Setting up GitHub Flow”Before we start, make sure you have:
- A GitHub account
- SSH keys configured (as covered in the previous collaboration chapter)
- A repository on GitHub
Let’s create a new repository for practicing GitHub Flow. Go to GitHub and create a new repository called github-flow-practice
.
# Clone the repository locallygit clone git@github.com:YOUR_USERNAME/github-flow-practice.gitcd github-flow-practice
# Create a basic READMEecho "# GitHub Flow Practice" > README.mdgit add README.mdgit commit -m "Initial commit"git push origin main
Creating Feature Branches
Section titled “Creating Feature Branches”In GitHub Flow, all development happens on feature branches. These branches are created from the main branch and are used to develop new features, fix bugs, or make other changes.
Create a Feature Branch
Section titled “Create a Feature Branch”Let’s create a feature branch for adding a new feature to our project.
# Make sure you're on main and up to dategit switch maingit pull origin main
# Create and switch to a new feature branchgit switch -c feature/add-user-profile
# Verify you're on the new branchgit branch
Follow-along: Create a feature branch called feature/add-navigation-menu
in your practice repository.
Making Changes and Committing
Section titled “Making Changes and Committing”Once on your feature branch, you can start making changes. Remember to commit regularly with descriptive messages.
# Create a new file for the user profile featureecho "# User Profile Feature" > user-profile.mdgit add user-profile.mdgit commit -m "Add basic user profile structure"
# Make another changeecho "This feature allows users to view their profile." >> user-profile.mdgit add user-profile.mdgit commit -m "Add user profile description"
Follow-along: Add a file called navigation.md
to your feature/add-navigation-menu
branch with some content about navigation.
Pushing to GitHub
Section titled “Pushing to GitHub”Push your feature branch to GitHub so others can see your work and collaborate.
# Push the feature branch to GitHubgit push -u origin feature/add-navigation-menu
Follow-along: Push your feature/add-navigation-menu
branch to GitHub.
Creating a Pull Request
Section titled “Creating a Pull Request”Once your feature is ready for review, create a Pull Request (PR) on GitHub.
- Go to your repository on GitHub
- Click on “Pull requests” tab
- Click “New pull request”
- Select your feature branch as the “compare” branch
- Add a descriptive title and description
- Click “Create pull request”
# After creating PR, you can continue working# Make sure to push any additional commits to the same branchecho "Added user avatar functionality" >> user-profile.mdgit add user-profile.mdgit commit -m "Add user avatar support"git push origin feature/add-user-profile
Follow-along: Create a Pull Request for your navigation menu feature.
Code Review and Merging
Section titled “Code Review and Merging”Once a PR is created, team members can review the code, leave comments, and suggest changes.
-
Review Process:
- Check the code for bugs, style issues, and adherence to standards
- Test the changes if possible
- Leave constructive feedback
-
Merging:
- When approved, merge the PR using “Squash and merge” or “Merge commit”
- Delete the feature branch after merging
Follow-along: If working with others, ask them to review your PR. Otherwise, review your own code and merge it.
Merging Latest Changes from Main into Feature Branch
Section titled “Merging Latest Changes from Main into Feature Branch”As development continues on the main branch, your feature branch can become outdated. To incorporate the latest changes from main into your feature branch, you can use regular merges. This creates a merge commit but keeps the workflow simple and avoids the complexity of rebasing.
When to Merge Main into Feature Branch
Section titled “When to Merge Main into Feature Branch”- When your feature branch is behind main
- Before creating a PR to ensure your branch is up to date
- When you want to test your feature with the latest changes from main
How to Merge Main into Feature Branch
Section titled “How to Merge Main into Feature Branch”# Switch to your feature branchgit switch feature/add-user-profile
# Merge the latest main into your feature branchgit merge main
# If there are conflicts, resolve them and commit# git add <resolved-files># git commit
# Push the merged branchgit push origin feature/add-user-profile
Example: Merging with Conflicts
Section titled “Example: Merging with Conflicts”Let’s simulate a scenario where main has changes that conflict with our feature branch.
# On main branch, someone adds a footerecho "# Footer" > footer.mdgit add footer.mdgit commit -m "Add footer component"git push origin main
# Now on our feature branch, we also modify footer.mdgit switch feature/add-user-profileecho "Navigation links here" > footer.mdgit add footer.mdgit commit -m "Add navigation to footer"
# Try to merge main into feature branchgit merge main
# Git will show conflict in footer.md# Edit footer.md to resolve conflict# For example, combine both changes:# # Footer# Navigation links here
git add footer.mdgit commit -m "Merge main into feature branch and resolve conflicts"
# Push the merged branchgit push origin feature/add-user-profile
Using GitHub’s Update Branch Button
Section titled “Using GitHub’s Update Branch Button”GitHub provides a convenient “Update branch” button on Pull Request pages that automatically merges the latest changes from main into your feature branch. This is often the easiest method!
How to use the Update branch button:
- Open your Pull Request on GitHub
- Look for the “Update branch” button (usually appears when your branch is behind main)
- Click “Update branch”
- GitHub will create a merge commit automatically
- The button will change to show that the branch is up to date
Benefits of using Update branch:
- No need to switch branches locally
- GitHub handles the merge automatically
- Clear indication in the PR timeline when updates were made
- Works well with frequent updates
Exercise: Practice Merging
Section titled “Exercise: Practice Merging”- Create a new feature branch
feature/add-about-page
- On main, add a file called
about.md
with some content - On your feature branch, also add
about.md
with different content - Try to merge main into your feature branch
- Resolve any conflicts
- Push the merged branch
- Alternative: Create a PR and use GitHub’s “Update branch” button
Follow-along: Complete the above exercise in your practice repository. Try both the command-line merge and the GitHub Update branch button.
Best Practices for GitHub Flow
Section titled “Best Practices for GitHub Flow”- Keep branches short-lived: Create branches for specific features or fixes
- Use descriptive names:
feature/add-user-authentication
instead ofmy-feature
- Commit often: Small, focused commits are easier to review
- Write good commit messages: Start with verb, be descriptive
- Delete merged branches: Keep repository clean
- Use merge to keep feature branches up to date: Regularly merge main into your feature branches
- Test before merging: Ensure changes don’t break existing functionality
Common Scenarios
Section titled “Common Scenarios”Scenario 1: Multiple Features in Parallel
Section titled “Scenario 1: Multiple Features in Parallel”# Developer 1git switch -c feature/user-login# Work on login feature...
# Developer 2git switch -c feature/user-registration# Work on registration feature...
# Both can work independently and create separate PRs
Scenario 2: Hotfix on Main
Section titled “Scenario 2: Hotfix on Main”# Urgent bug fix neededgit switch maingit pull origin maingit switch -c hotfix/security-patch# Fix the security issuegit commit -m "Fix security vulnerability"git push origin hotfix/security-patch# Create PR and merge quickly