Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the operations of the local branch of Git

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "what are the operations of the local branch of Git", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the operation of the local branch of Git" can help you solve your doubts.

I. the role of the Git branch

In the multi-person collaborative development, in order to prevent mutual interference and improve the experience of collaborative development, it is recommended that each developer develop the project function based on the branch.

Second: master main branch

When initializing the local Git repository, Git has created a branch called master for us by default. Usually we call this master branch the main branch.

In practice, the function of the main branch of master is to save and record the completed functional code of the whole project.

Therefore, programmers are not allowed to modify the code directly on the master branch, because the risk of doing so is too high and can easily lead to the collapse of the whole project, so we need to develop on the branch we are responsible for developing.

three。 Functional branch

Because programmers cannot develop functions directly on the master branch, there is the concept of a functional branch.

A feature branch refers to a branch dedicated to the development of new features. It is temporarily forked from the main branch of master. When the new features are developed and tested, they eventually need to be merged into the main branch of master, as shown in the figure:

four。 Local branch operation 1. View a list of branches

Use the following command to view a list of all branches in the current Git repository:

Git branch

Note: the * in front of the branch indicates the current branch

two。 Create a new branch

Using the following command, you can create a new branch based on the current branch, and the code in the new branch is exactly the same as the current branch: (so we need to create a new branch on the main branch during development)

Git branch branch name 3. Switch branch

You can switch to the specified branch for development using the following command:

Git checkout branch name 4. Fast creation and switching of branches

Use the following command to create a new branch with a specified name and immediately switch to the new branch:

1 #-b means to create a new branch 2 # checkout means to switch to 3 git checkout-b branch name 5. Merging molecule

After the code development and testing of the functional branch is complete, you can merge the completed code into the master main branch using the following command:

1 switch to master branch 2 git checkout master3 run the git merge command on the master branch, merge the code of the merged branch into the master branch 4 git merge branch name 6. Delete Branch

After merging the code of the functional branch into the main branch of master, you can delete the corresponding functional branch using the following command:

Git branch-d branch name 7. Branch merging in case of conflict

If you make different changes to the same file in two different branches, Git will not be able to merge them cleanly. At this point, we need to open these files that contain conflicts and resolve them manually.

Conflict assumption: during merging the reg branch into the master branch, the code conflicts 2 git checkout master3 git merge reg

Open the conflicting file to resolve manually (you can also use the auxiliary resolution of vs code (inside the red circle))

Resubmit and merge after resolution

# Open the file containing the conflict, resolve the conflict manually, and then execute the following command git add. Git commit-m "solved the problem of branch merge conflict" git merge branch name 8. Push the local branch to the remote warehouse

If this is the first time that the local branch is pushed to the remote warehouse, you need to run the following command:

1 git push u origin payment means to associate the local branch with the remote branch, and only need the alias local branch name of the remote repository with the-u parameter 2 git push-u on the first push. Local branch name: remote branch name 3 actual case: 5 git push-u origin payment: pay67# if you want the name of the remote branch to be the same as the local branch name, you can simplify the command: 8 local branch

If it is not the first time that the local branch has been pushed to the remote warehouse, you need to run the following command:

After switching to the branch to be pushed, the git push directly pushes the local branch to the remote warehouse.

9. View the list of all branches of the remote warehouse git remote show remote warehouse name 10. Tracking branch

Tracking branch refers to downloading the remote branch to the local warehouse from the remote warehouse. The commands you need to run are as follows:

11. Pull the latest code for the remote branch

You can download the latest code of the remote branch to the corresponding local branch using the following command:

From the remote warehouse, pull the latest code of the current branch and keep the code of the current branch consistent with the remote branch code 2 git pull12. Delete remote branch

You can delete the specified branch in the remote warehouse using the following command:

1 # Delete from the remote warehouse, specify the name of the remote branch 2 git push remote warehouse name-- delete remote branch name 3 # example: 4 git push origin-- delete pay read here, this article "what is the operation of the local branch of Git" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, Welcome to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report