If you have a single branch and only one master branch, you can solve 80% of the problems mentioned above. This article focuses on branch management in the case of multiple branches.

Branching is important if the official version is needed, but you want to develop without breaking the official version. We can develop new functionality on the branch and then merge it into the main branch, making the whole process transparent to the main branch until the merge.

Create a branch

To be clear, there is little point in creating A local branch, just like copying and pasting. You can’t push A local A branch to A remote A branch. Real development involves associating local branches with remote branches. This allows development in the local A branch to be pushed to the remote A branch.

Git branch is a repository that has only one master. Git branch -a = devleop, feature, and master branches

The master branch is the develop branch, and the feature branch is the patch branch.

For example, if SpringBoot4.2 is the main branch, then maybe SpringBoot5 is the development branch, which is the next iteration. But SpringBoot4.3 adds features and fixes bugs that are developed in the feature branch and then merged into the main branch.

It’s just a simulation. This is what happens

Back to the main theme, there are 3 remote repositories, cloning is only the default clone of the main repository, but normal multi-branch development is usually clone all branches or specific branches. Use git branch XXX origin/ XXX for specific branches. It is generally recommended that the local branch name be the same as the remote branch name for ease of development.

Above, git branch develop origin/develop is used, which means you create a branch called Develop and clone the remote Develop branch to the local branch.

Branch ‘develop’ set up to track remote branch ‘develop’ from ‘origin’. The Develop branch setting tracks the Develop branch from Origin, and only locally committed code that is tracked can be pushed to the corresponding branch of the remote repository.

After executing the command, there are three local branches associated with the remote.

Switch branch

Git checkout [branchName] git checkout [branchName] git checkout [branchName]

You will be prompted to switch to the Develop branch and see the current branch using the Git branch command. Git checkout -b [branchName] creates a branch and switches to the branch.

Merging branches

Local single branches push to their remote counterparts as normal. No matter which branch we develop on, we end up serving the main branch or publishing the branch. We need to merge the content of other branches into the main branch.

  1. Switch to the main branch and you can see that the main branch has only two files
  2. Switch to the development branch and add a new file

  1. Then add, commit, and push it to the remote Develop branch. You can see that the remote Master branch doesn’t have any files added.

The next step is to merge the development branch into the main branch. Note that during real development, the main branch is usually set to the protection branch, and we do not have permissions. It’s possible, it’s possible to push directly to other remote branches of the company, but merging into the main branch requires a warehouse administrator, so this is just a simulation, so it’s done locally.

  1. First you need to switch to the main branch and usegit statusCan be temporary storage area is nothing else
  2. git merge [branchName] Merge from the target branch to the current branchIn this case, merge content from the Develop branch into the main branch
  3. usegit statusCheck git status to see that after the merge, the local branch is ahead of the remote master branch

  1. Then when you push it, you have the file on the remote master branch

Merging different branches is just like merging a remote local repository into a remote company’s repository in the previous single-master development model, which is not covered in this article.

Delete the branch

Normally we don’t delete branches of the company.

But there may be branches that we don’t want to touch that are associated with the company, so we create a branch locally, which is equivalent to a copy. After developing tests on the branch that does not have an associated remote branch, merge to the remote non-master branch, which may require deleting the branch.

  1. Create a branch named Backup
  2. usegit branch -d [branchName]Delete the branch you just created

It is important to note that it is not allowed to delete branch A under branch A

Creation is not easy, if it is helpful to you, welcome to like, collect and share!

Below is an individual public number, have interest can pay attention to, perhaps be your treasure public number!!