Branching Strategy
Develop Branch
The develop branch on your local repository should have the upstream branch set on the remote you are trying to pull from. This allows Git to know which remote branch to pull changes from.
To change from the main branch, you need to set an upstream branch for develop. You have two primary options:
-
Set the upstream branch and pull in one command:
git pull origin developThis command will pull changes from the
developbranch on theoriginremote, and it will automatically set this as the tracking branch for future pulls. -
Set the upstream branch separately and then pull:
git branch --set-upstream-to=origin/develop develop
git pullThe first command sets the
origin/developbranch as the upstream for your localdevelopbranch. The second command then pulls changes from the set upstream branch.
Choose the method that you find more convenient. Once the upstream is set, future git pull operations will not require specifying the remote and branch.
Git Flow
To implement a GitFlow branching strategy on GitHub and adjust your workflows accordingly, follow these steps:
1. Set Up GitFlow Branching Strategy
Branches in GitFlow:
main: Contains production-ready code. Only direct commits due to hotfixes or merges fromreleasebranches.develop(ordev): Integrates feature branches and is used for testing. Eventually, it gets merged intomainfor releases.
First Goal: Development Branch
-
Create
developBranch:-
Create a
developbranch by branching offmain:git checkout main
git checkout -b develop
git push origin develop
-
-
Protect
developBranch:- Go to your repository on GitHub.
- Navigate to Settings > Branches.
- Set up protection rules for
developsimilar tomain. This often includes:- Requiring pull requests before merges.
- Passing status checks.
- Optional: Require a specific number of approving reviews.
-
Maintain
mainProtection: Continue protecting themainbranch, as it represents production code.
2. Modify GitHub Workflows
You'll need adjustments based on where you want these workflows to trigger, especially with the introduction of the develop branch.
ci.yml Workflow
Modify it to run on pushes to or pull requests targeting develop:
name: CI
on:
push:
branches:
- develop
pull_request:
branches:
- develop
publish-packages.yml Workflow
Typically, this workflow remains scoped to main because you usually want to publish packages based on production-ready code:
- Optionally, introduce
releasebranch handling:
on:
push:
branches:
- main
- 'release/**' # Triggers on release branches
deploy-docs.yml Workflow
This could be configured similarly to ensure deployments based on the content in main, as docs should generally reflect production:
on:
push:
branches: ['main']
3. Preparing for Releases
-
Release Branch:
- When you're ready to prepare for a new production release, create a
releasebranch fromdevelop. E.g.,release/v1.0.
git checkout develop
git checkout -b release/v1.0 - When you're ready to prepare for a new production release, create a
-
Tagging:
- Once a release branch is tested and ready, merge it into
main. - Tag releases in
mainwith version numbers:
git checkout main
git merge release/v1.0
git tag -a v1.0 -m "Release version 1.0"
git push origin main --tags - Once a release branch is tested and ready, merge it into
-
Merge Back:
- Merge
releaseback intodevelopafter releasing to include anyreleasebranch changes in ongoing development.
- Merge
With these setups and changes in place, GitFlow will help manage development, releases, and production code effectively.