Managing Branches
Introduction
Branch is the most commonly used management method in the version management tool. Branches can be used to isolate tasks in project development from each other so that they do not affect each other. Before releasing a version, you can use the Branch Merging to integrate the tasks.
When you create a CodeHub or Git repository, a master branch is generated by default and used as the branch of the latest version. You can create custom branches at any time for personalized scenarios.
GitFlow
As a branch-based code management workflow, GitFlow is highly recognized and widely used in the industry. It is recommended for you to start team-based development.
- Concurrent development: Multiple features and patches can be concurrently developed on different branches to prevent intervention during code writing.
- Team collaboration: In team-based development, the development content of each branch (or each sub-team) can be recorded separately and merged into the project version. An issue can be accurately detected and rectified separately without affecting other code in the main version.
- Flexible adjustment: Emergency bug fixes are developed on hotfix branches without interrupting the main version and sub-projects of each team.
Table 1 Suggestions on using GitFlow branches Branch
Description
Validity
When to Create
When to Develop This Branch
When to Merge Other Branches into This Branch
When to Merge This Branch to Other Branches
When to End
master
Core branch, which is used together with tags to archive historical versions. Ensure that all versions are available.
Long-term
Created when the project repository is created
Never
- When the project version is frozen, the develop or release branch are merged into this branch.
- After bugs found in the released version are fixed, hotfix branches are merged into this branch.
_
_
develop
Main development branch, which is used for routine development and must always be the branch with the latest and most complete functions.
Long-term
Created after the master branch is created.
Not recommended
- After new features are developed, feature branches are merged into this branch.
- When a new version starts to be developed, the last version (release or master branch) is merged into this branch.
- When a version is to be released, this branch is merged into the release branch.
- When a version is to be archived, this branch is merged into the master branch.
_
feature_1\2\3...
Feature development branch, which is used to develop new features. Multiple branches can exist concurrently. Each branch corresponds to a new feature or a group of new features.
Temporary
- Created based on the develop branch when a new feature development task is received.
- Created based on the parent feature branch when the current feature development task is split into sub-tasks.
Developed when being created.
After a child feature branch is developed and tested, it is merged into the parent feature branch.
After new features are developed and tested on this branch, it is merged into the develop branch.
After the corresponding features are accepted (released and stable)
release
Release branch, which is used to check out a version to be released.
Long-term
Created based on the develop branch before the first release.
Never
When a version is to be released, the develop branch is merged into this branch.
- When a version is released and archived, this branch is merged into the master branch.
- When a new version is developed based on a released version, this branch is merged into the develop branch to initialize the version.
_
hotfix_bug1\bug2...
Emergency fix branch, which is used to fix bugs in the current version.
Temporary
Created based on the corresponding version (usually the master branch) when issues are found in the master or bug version.
Developed when being created.
_
When the corresponding bug fixing task is complete, this branch is merged into the master and develop branches as a patch.
After the corresponding bugs are fixed and the version is accepted (released and stable)
GitFlow has the following rules:
- All feature branches are pulled from the develop branch.
- All hotfix branches are pulled from the master branch.
- All commits to the master branch must have tags to facilitate rollback.
- Any changes that are merged into the master branch must be merged into the develop branch for synchronization.
- The master and develop branches are the main branches and they are unique. Other types of branches can have multiple derived branches.
Creating a Branch on the Console
- Access the repository list.
- Click a repository to go to the details page.
- Switch to the Branches tab page. Branches in the remote repository are displayed.
- Click Create Branch. In the displayed dialog box, select a version (branch or tag) based on which you want to create a branch and enter the branch name. You can associate the branch with an existing work item.
- Click OK. The branch list is displayed.
Managing Branches on the Console
You can perform the following operations in the branch list:
- Filtering branches
- All: displays all branches. The default branch is displayed on the top. Other branches are sorted by the last commit time in descending order.
- Active: displays the branches that have been developing in the past three months. Branches are sorted by the last commit time in descending order.
- Inactive: displays the branches that have not been developed in the past three months. Branches are sorted by the last commit time in descending order.
- You can click a branch to go to the Files tab page of the branch and view its content and history.
- You can click a latest commit to view the content committed on the details page.
- You can click to go to the Comparison tab page and compare the current branch with another branch.
- Click to download its compressed package.
- You can click to create a merge request for a branch on the Merge Requests tab page.
- You can click to delete a branch as prompted.
In addition, you can configure branches as follows on the console:
Common Git Commands for Branches
- Creating a branch
git branch <branch_name> # Create a branch based on the current working directory in the local repository.
Example:git branch branch001 # Create a branch named branch001 based on the current working directory in the local repository.
If no command output is displayed, the creation is successful. If the branch name already exists, as shown in the following figure, create a branch with another name.
- Switching a branch
Switching a branch is to check out the branch file content to the current working directory.
git checkout <branch_name> # Switch to a specified branch.
Example:
git checkout branch002 # Switch to branch002.
The following information shows that the switch is successful.
- Switching to a new branch
You can run the following command to create a branch and switch to the new branch directly.
git checkout -b <branch_name> # Create a branch based on the current working directory in the local repository and directly switch to the branch.
Example:
git checkout -b branch002 # Create a branch named branch002 based on the current working directory in the local repository and directly switch to the branch.
The following information shows that the command is successfully executed.
- Viewing a branch
Run the following commands to view the branches of the local repository, remote repository, or all. These commands list only the branch names. To view the specific files in a branch, run the command in Switching a branch.
git branch # View the local repository branch.
git branch -r # View the remote repository branch.
git branch -a # View the branches of the local and remote repositories.
The following figure shows the execution result of the three commands in sequence. Git displays the branches of the local and remote repositories in different formats. (Remote repository branches are displayed in the format of remote/<remote_repository_alias>/<branch_name>.)
- Merging a branch
When a development task on a branch is complete, the branch needs to be merged into another branch to synchronize the latest changes.
git merge <name_of_the_branch_merged_to_the_current_branch> # Merge a branch into the current branch.
Before merging a branch, you need to switch to the target branch. The following describes how to merge branch002 into the master branch.git checkout master # Switch to the master branch. git merge branch002 # Merge branch002 into the master branch.
The following figure shows the execution result of the preceding command. The merge is successful, and three lines are added to a file.
The system may prompt that a merge conflict occurs. The following shows that a conflict occurs in the fileOnBranch002.txt file.
To resolve the conflict, open the conflicting file, manually edit the conflicting code (as shown in the following figure), and save the file. Then run the add and commit commands again to save the result to the local repository.
This is similar to resolving a conflict that occurs when you commit a file from the local repository to the remote repository. For details about the working principle, see Resolving Code Commit Conflicts.
A proper collaboration mode can prevent conflicts.
- Deleting a local branch
git branch -d <branch_name>
Example:
git branch -d branch002 # Delete branch002 from the local repository. The following information shows that the operation is successful.
- Deleting a branch from the remote repository
git push <remote_repository_address_or_alias> -d <branch_name>
Example:
git push HTTPSOrigin -d branch002 # Delete branch002 from the remote repository whose alias is HTTPSOrigin. The following information shows that the deletion is successful.
- Pushing a new local branch to the remote repository
git push <remote_repository_address_or_alias> <branch_name>
Example:
git push HTTPSOrigin branch002 # Push the local branch branch002 to the remote repository whose alias is HTTPSOrigin. The following information shows that the push is successful.
If the push fails, check the connectivity.
Check the established SSH key pair. If necessary, regenerate a key and configure it on the CodeHub console. For details, see SSH Keys.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot