Branch operations used in Git common scenarios


1. Pull push branch:

Git Branch Branch Name: create branch
Git checkout Branch Name: switch branch
Git checkout – B Branch Name: create and switch to a new branch
Local pull branch and push to remote:
Git push < remote host name > < local branch name >: < remote branch name >
[note] direct git push will push the current local branch to the corresponding remote branch with the same name. If there is no remote branch with the same name, a new branch with the same name will be created
[for example] git push and git push origin branchtest are equivalent, which push the local branchtest branch to the remote end

2. Branch switching:

For example, switch from branchtest to master branch

[scenario 1] prompt after branch switch: your local changes to the following files would be rewritten by checkout


[reason] just like the push conflict problem, the switch problem is because the current branchtest code conflicts with the master code after modifying the code
For example, there are no 666666 and 777777 lines in file1 of the master. After the branchtest is modified, 777777 is deleted. This problem will occur when switching;
If the branchtest is modified without changing the master code, that is, simply adding 888888888, this problem will not occur.
[solution 1] force checkout will retain the original master code, and the branch modified code will not appear in the master code
Smart checkout allows you to manually resolve conflicts. Please refer to point 4 of the link
Don’t checkout is to let you give up switching branches
[solution 2] if don’t checkout gives up the handover, then commit once and then switch again, there will be no such problem
However, this will result in multiple local commits, which will have to be merged at that time

3. Modify the code in the wrong branch:

[scenario] after branchtest modifies the code, it is found that the branch is wrong. It should be modified in the master
[resolution 1] in the case of no commit, switch the master branch and resolve the conflict as above;
If you commit, you can reset and then switch. Of course, if you don’t change much, you can switch and change again,
[solution 2] if it can be determined that the codes of the two branches before push are the same, GIT push origin B can be used ranchTest:master Push the code submitted by branchtest to the remote commit branch
[solution 3] if the codes of the two remote branches before push are different, they will be pushed rejected
             3-3-a.If the remote master code is pulled to the current branch, the conflict will be prompted and the conflict can be resolved manually


            3-3-b.Code recommit
After manually resolving the conflict, you can see that the previously committed commit has been disconnected in the log, so you need to re commit the commit


After recommit, it will be merged back automatically, so that git push origin B can be implemented ranchTest:master


[suggestion] specify the branch before modifying the code, pull the code before submitting the code, and resolve the conflict if there is conflict.


This article on the use of GIT common scenarios – branching operation is introduced here. For more information about the use of GIT common scenarios, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!

Recommended Today

Java development for two years! You have to know how to handle these exceptions, or you can get a raise!

preface Exception is a kind of instruction flow that causes program interrupt in program. When exception occurs, program will interrupt directly and no further operation will be executed!Example: divide two numbers. If no exception is handled, the operation result can be displayed only when two numbers are correctly entered. public static void main(String[] args) { […]