Save Your Production Server by Mastering GitHub Branching and Merging
Managing code changes and collaboration efficiently is crucial for any development team. One of the best practices for achieving this is using a well-defined branching and merging strategy. In this blog, we’ll walk you through a step-by-step guide on how to use develop and production branches in GitHub, including how to handle merge conflicts and raise pull requests (PRs).
Why We Need a Branching and Merging Strategy
Imagine you’re developing a new feature for your application. Without a clear strategy, integrating your work could lead to conflicts, instability, and deployment issues. A structured approach not only streamlines development but also safeguards the stability of your production code.
Scenario: Developing a New Feature
Step 1: Creating a Feature Branch
To add a new feature, you start by creating a feature branch from the production branch. This ensures your work is based on the latest stable code.
git checkout production
git pull origin production
git checkout -b feature/user-authentication
Step 2: Developing and Testing
Develop the user authentication feature on your feature/user-authentication
branch. Regular commits and pushes keep your work up-to-date and backed up.
git add .
git commit -m "Add login functionality"
git push origin feature/user-authentication
Step 3: Integrating into Develop
Once the feature is complete and tested, create a PR to merge it into the develop branch. This step allows the feature to be tested alongside other ongoing developments.
However, you might encounter merge conflicts due to simultaneous changes by other developers. To resolve this, merge the develop branch into your feature branch, fix any conflicts, and then merge your feature branch back into develop. But what if the develop branch contains untested or buggy code? You don’t want these issues to affect your production code.
Here’s where a robust branching strategy comes into play.
The Branching Strategy
Our strategy uses two primary branches:
- Develop: This is the integration branch for all feature development. It holds the latest code ready for testing and integration.
- Production: This branch contains stable, production-ready code. Features are merged into production only after thorough testing in the develop branch.
Step-by-Step Workflow
1. Creating a Feature Branch
Start by creating a feature branch from the production branch. This ensures your new feature is based on the latest stable code.
git checkout production
git pull origin production
git checkout -b feature/your-feature-name
Replace your-feature-name
with a descriptive name for your feature.
2. Developing and Testing Your Feature
Work on your feature branch. Implement the necessary changes and test them thoroughly. Commit your changes regularly to keep your feature branch updated.
git add .
git commit -m "Describe your changes"
git push origin feature/your-feature-name
3. Merging into Develop
Once you’re satisfied with your feature and it’s ready for integration, create a PR from your feature branch to the develop branch. This allows your feature to be tested and integrated with other features.
Handling Merge Conflicts
Merge conflicts can be tricky, but with a systematic approach, you can resolve them efficiently.
a. Create a Merge Branch from Develop
git checkout develop
git pull origin develop
git checkout -b merge/feature-into-develop
b. Merge Your Feature Branch into the Merge Branch
git merge feature/your-feature-name
c. Resolve Any Merge Conflicts
Edit the conflicting files to resolve the conflicts. After resolving, add and commit the changes.
git add .
git commit -m "Resolve merge conflicts between feature branch and develop"
d. Push the Merge Branch and Create a PR
git push origin merge/feature-into-develop
Create a PR from merge/feature-into-develop
to develop
on GitHub.
4. Raising a PR to Production
After your feature has been merged into develop and has passed all tests, prepare to merge it into production.
git checkout production
git pull origin production
git checkout -b merge/develop-into-production
git merge develop
git push origin merge/develop-into-production
Create a PR from merge/develop-into-production
to production
on GitHub.
By following this branching and merging strategy, you can ensure smooth feature integration and maintain the stability of your production code. Happy coding!