Branching
Loading...
Branching is a core concept in Git, and the entire Git workflow is based around it. Branches are lightweight pointers to a commit, and they are used to isolate work from the main codebase. This allows you to work on a feature or bug fix without affecting the main codebase. Once you are done with your work, you can merge the branch back into the main codebase.
Branching Strategies
There are several branching strategies that you can use in Git. The most common ones are:
-
Feature Branching: Each feature or bug fix is developed in a separate branch. This allows you to isolate the work and merge it back into the main codebase once it is complete.
-
Release Branching: A release branch is created when you are preparing to release a new version of your software. This branch is used to stabilize the code and fix any last-minute bugs before the release.
-
Hotfix Branching: A hotfix branch is created to fix critical bugs in the production code. This branch is created off the main codebase and is merged back into the main codebase once the bug is fixed.
Best Practices
Here are some best practices for using branches in Git:
-
Use Descriptive Branch Names: Use descriptive branch names that indicate the purpose of the branch. This makes it easier to understand what each branch is for.
-
Keep Branches Small: Keep branches small and focused on a single task. This makes it easier to review and merge the code.
-
Merge Frequently: Merge frequently to keep your branch up to date with the main codebase. This reduces the chances of merge conflicts.
-
Delete Branches After Merging: Delete branches after merging them back into the main codebase. This keeps your repository clean and makes it easier to manage branches.
-
Use Pull Requests: Use pull requests to review and merge code. This allows you to get feedback on your code before merging it into the main codebase.
Example Workflow
Each Repository has a default branch, usually called main. When you want to work on a new feature or bug fix, you create a new branch off the main branch. You make your changes in the new branch and then create a pull request to merge the changes back into the develop branch. Once the changes are reviewed and approved, you merge the branch back into the main branch.
Feature/Hotfix Branch
To create a new feature or hotfix branch, you can use the following command:
1git checkout -b feat/my-feature2
3git checkout -b hotfix/my-hotfixThis command creates a new branch called feat/my-feature and switches to it.
PR and Merge
Once you have made your changes, you can create a pull request to merge the changes back into the develop branch. You can do this using the following command:
1git push origin feat/my-feature2
3git push origin hotfix/my-hotfixThis command pushes the changes to the remote repository. You can then create a pull request from the remote repository interface. Once the changes are reviewed and approved, you can merge the branch back into the develop branch. After that, you can delete the feature branch.
Before creating a pull request, make sure to pull the latest changes from the main branch (git pull --rebase origin main) and resolve any conflicts. The QA team can then create a QA1/2 branch to test the changes and merge them back into the develop branch.
After successful testing, you can merge the changes from develop into the main branch.
QA1/2 Testing Branch
To create a QA1/2 testing branch, you can use the following command:
1git checkout -b qa1/feat/my-feature2
3git checkout -b qa1/hotfix/my-hotfixThis command creates a new branch called qa1/feat/my-feature and switches to it.
PR and Merge
Once the QA team has tested the changes, they can create a pull request to merge the changes back into the feature/hotfix branch. You can do this using the following command:
1git push origin qa1/feat/my-feature2
3git push origin qa1/hotfix/my-hotfixThis command pushes the changes to the remote repository. You can then create a pull request from the remote repository interface. Once the changes are reviewed and approved, you can merge the branch back into the feature/hotfix branch. After that, you can delete the QA1/2 branch.
Once the changes are merged back into the feature/hotfix branch, you can merge the changes back into the develop branch.