Managing Two Branches and Merging Commits from Stage to Main
Introduction 1
In the development process, particularly when managing web projects or software, using version control systems like Git is fundamental. A common approach is to maintain at least two branches: main for production and stage for ongoing development and testing. This article explores the strategy of working with these two branches effectively and how to merge commits from stage to main when necessary.
Setting Up the Branches
Initially, ensure that you have two branches in your repository: main and stage. The main branch holds the production-ready code, while stage is used for development and pre-production testing.
-
Create the
stagebranch (if it doesn’t exist):1git checkout -b stageThis command creates and switches to the
stagebranch. -
Push the
stagebranch to your remote repository:1git push -u origin stageThis establishes
stageon your remote, tracking the branch with your local copy.
Workflow Between stage and main
The workflow typically involves regular development activities on the stage branch, periodic merges into main, and ensuring that main is always in a deployable state.
Development in Stage
Developers commit all new features, bug fixes, and improvements to the stage branch. This is where all integration tests and pre-release activities occur.
1git checkout stage
2# Developers make changes and commit them
3git add .
4git commit -m "Add new features"
Merging Changes to Main
Once the changes in stage are stable and tested, you can merge them into main. This step is critical because it updates the production environment with the latest tested features from development.
1git checkout main
2git pull origin main # Ensure the main is up to date
3git merge stage
4git push origin main
This method of merging keeps the commit history from the stage branch. If you prefer a cleaner history, you might consider using git rebase before merging.
Rebase and Merge
Rebasing is an alternative to merging that can make the project history cleaner and linear.
1git checkout stage
2git rebase main
3# Resolve any conflicts that arise during the rebase
4
5git checkout main
6git merge stage # This will now be a fast-forward merge
7git push origin main
Best Practices
-
Keep
mainstable: Always ensure that themainbranch is stable and deployable. It should only receive changes fromstagethat are thoroughly tested and approved. -
Use Pull Requests: Even if direct merges are used, consider using pull requests for merging
stageintomainas they offer a chance to review changes, run automated checks, and ensure compliance with project standards before merging. -
Regularly Update
Stage: Frequently merge changes frommainback intostageto keep the development branch up-to-date with the production environment. This helps reduce merge conflicts and integration issues.
1git checkout stage
2git merge main
3git push origin stage
Conclusion
Managing two branches, stage for development and main for production, with periodic merges, is a robust strategy to maintain the quality and stability of the software while allowing continuous development and testing. Utilizing Git commands to manage branches effectively ensures a smooth workflow in software development projects.