[Git operation] branch operation

1 Introduction

Almost every version control system supports branches in some form. Using branches means that you can separate from the main line of development and continue working without affecting the main line. Some people call Git's branch model a nirvana feature, and it is precisely because of it that distinguishes Git from the family of version control systems. (Excerpted from the rookie tutorial )

2. Use

(1) View branch

View local branches: git branch
View all branches (including remote branches):git branch -a

(2) Create a branch

Create an empty branch: git branch 分支名
create a new local branch from an existing local branch (such as from the master branch), create a dev branch: git checkout -b dev
pull the specified branch in the remote git repository to the local (branch that does not exist locally): git checkout -b 本地分支名 origin/远程分支名
this will A new local branch will be automatically created and associated with the specified remote branch.
For example, there is a branch dev2 in the remote warehouse, and I don't have this branch locally. I want to pull dev2 to my local:
img
if it succeeds, a new branch dev2 will be created locally and automatically switched to dev2.
If prompted:

fatal: Cannot update paths and switch to branch 'dev2' at the same time.
Did you intend to checkout 'origin/dev2' which can not be resolved as commit?

Indicates that the pull was unsuccessful. We need to execute

git fetch

And then execute

git checkout -b 本地分支名 origin/远程分支名

(3) Pull the branch

Pull remote branches: git pull origin dev
pull all remote branches: git fetch
set git push, pull the default commit to obtain branches, so it is very convenient to use git push to submit information or git pull to obtain information.
Setting tracking: git branch --set-upstream-to=origin/dev
cancel tracking of master:git branch --unset-upstream master

(4) Switch branches

View local branches: git checkout BranchName
View all branches (including remote branches):git checkout origin/BranchName

(5) Tracking branch

Set up tracking: git branch --set-upstream-to=origin/BranchName
Cancel the tracking of the master:git branch --unset-upstream master

(6) Delete branch

Delete local branch: git branch -d BranchName
delete remote branch:git push --delete origin BranchName

(7) Rename the branch

Step1, cut the local branch oldbranch to a local branch

 git branch -m oldbranch newbranch

img
Step2, delete the remote branch

git push --delete origin oldbranch

img
Step3, push the local new branch to the remote

git push origin newbranch

img

(8) Merging branches

Method 1: Interface
Step1: Initiate a merge request
Insert picture description here
Step2: Fill in the merge information
Insert picture description here
Step3: Agree to the merge request and complete the merge
Insert picture description here
Method 2: Command line
Once a branch has independent content, you will eventually want to merge it back to your main branch. You can merge any branch into the current branch with the following command:

git merge
$ git branch
* master
  newtest
$ ls
README        test.txt
$ git merge newtest
Updating 3e92c19..c1501a2
Fast-forward
 runoob.php | 0
 test.txt   | 1 -
 2 files changed, 1 deletion(-)
 create mode 100644 runoob.php
 delete mode 100644 test.txt
$ ls
README        runoob.php

In the above example, we merged the newtest branch into the main branch, and the test.txt file was deleted.

After merging, you can delete the branch:

$ git branch -d newtest

Deleted branch newtest (was c1501a2).
After deleting, only the master branch is left:

$ git branch
* master

(9) Branch rebase

When there is a main development branch in the development project, such as develop, then the separate development branches are dev1 and dev2. Make sure that dev1 is always branched from the latest submission of remote develop. The specific steps are as follows
git checkout dev1(switch local branch)
git fetch(Pull all remote content)
git stash(temporarily store the current local content)
git rebase origin/develop(rebase, make the personal development branch consistent with the main development branch)
git stash pop(restore the local current content),
then upload the local branch to the remote branch

git add .
git commit
git push origin dev1 #有时可能需要加上强制推送的参数-f

(10) Conflict resolution

When merging branches or rebasing branches, if there are conflicts between different branches, you need to compare the conflicting points of the two branches, select a branch or re-edit the content, you can use git diff to check whether there are conflicting content, if not , You can continue to complete the rebase operation through the command git rebase --continue

Guess you like

Origin blog.csdn.net/weixin_44704985/article/details/113990949