git common operations -- branch

Sharp tools make good work

 

 

 

 

 

branch

 

Branching is a simple concept, which is the same as that used in daily life. For example, hair branching: it is divided into two or more from one branch, and each branch is a branch.

 

 

git's branching function makes it a lot easier to use. Each branch will not be affected by each other.

 

The biggest advantage is that you can do different things in different branches without affecting each other. For example, when you write code on the computer, suddenly the mobile phone receives a push that "the United States and Iran are fighting". Your attention switches from code to mobile phone news, and your attention switches from writing code to looking at the mobile phone. What you do on the mobile phone will not affect the code you write on the computer. At this time, the boss just came over. You quickly put down your mobile phone and continue to write code. When the boss is gone, you continue to look at your mobile phone. The boss passed by frequently. You kept switching between writing code and watching mobile phones. They didn't interfere with each other. Finally, you finished writing code and watching the news. This is the meaning of branching.

 

 

 

 

Take a chestnut

 

Knowing the meaning of branching, let's use a chestnut to see how to use branching in git.

 

Or use git created before_ Test as a demonstration. The latest submission creates a problem. By the way, check GIT

ymj@ymj-pc:~/git_test$ git log --onelinea57b57f (HEAD -> master) Create problem cbdc648 fix buge595e94 add math.txt43b1405 init commitymj@ymj-pc:~/git_test$ git show a57b57f --onelinea57b57f (HEAD -> master) Create problem diff --git a/math.txt b/math.txtindex 2ec5ffb..69636f4 100644--- a/math.txt+++ b/math.txt@@ -1 +1,5 @@ 1 + 1 = 2++Here are a few#?+###+###

You can see that there is an additional "create problem" submission, and the modified content is to add a number of hash marks (#).

 

We have several methods to calculate # the number of, so we use different branches to calculate:

View current branch

ymj@ymj-pc:~/git_test$ git branch * master

The name of the current branch is called master, which is automatically created by git. By default, each warehouse has only one master branch (main branch), preceded by an asterisk (*), indicating that it is the current branch.

Here we know that the "HEAD - > Master" displayed in git log actually means that the current branch is master. HEAD and asterisk mean the same, representing which branch the current branch is.

 

How to create a branch

ymj@ymj-pc:~/git_test$ git branch f1ymj@ymj-pc:~/git_test$ git branch   f1* master

A branch named f1 was created using git branch f1, but the current branch is master.

The created branch is exactly the same as the current branch. Let's create a commit to see:

ymj@ymj-pc:~/git_test$ git show --oneline8abe1f0 (HEAD -> master) 1+1+1+1+1+1diff --git a/math.txt b/math.txtindex 69636f4..72920bb 100644--- a/math.txt+++ b/math.txt@@ -3,3 +3,5 @@ Here are a few#? ### ###++1+1+1+1+1+1 = 6

 

"1 + 1 + 1 + 1 + 1 + 1 = 6" is submitted in the master branch. At this time, the master advances one submission and f1 is still in the original place.

 

Switch branch

ymj@ymj-pc:~/git_test$ git checkout f1 Switch to branch 'f1'ymj@ymj-pc:~/git_test$ git branch * f1  masterymj@ymj-pc:~/git_test$ git log --onelinea57b57f (HEAD -> f1) Create problem cbdc648 fix buge595e94 add math.txt43b1405 init commit

Use "git checkout f1" to switch to the f1 branch, which can be seen from the asterisk of "git branch" and the HEAD of "git log". (through git checkout -b b_name, you can switch to the past while creating branches)

 

 

And "1 + 1 + 1 + 1 + 1 + 1 = 6" cannot be seen in f1 branch

ymj@ymj-pc:~/git_test$ cat math.txt 1 + 1 = 2​Here are a few#?######

Submit a new method in the f1 branch:

ymj@ymj-pc:~/git_test$ git show --onelinea0ed2b7 (HEAD -> f1) 3+3=6diff --git a/math.txt b/math.txtindex 69636f4..abdbee4 100644--- a/math.txt+++ b/math.txt@@ -3,3 +3,4 @@ Here are a few#? ### ###+3+3=6

The f1 branch and the master branch do not interact. If you want to merge the modified contents of the two branches together, you need to merge the branches.

 

Merge the modifications of the two branches together and merge them into the master branch
Git merge B is generally used_ Nane will be named B_ Merge the branches of nane into the current branch.

So switch to the master branch first

git checkout master

Then merge the f1 branch to the current branch master

ymj@ymj-pc:~/git_test$ git merge f1 Auto merge math.txt Conflict (content): Merge math.txt Auto merge failed. Correct the conflict and submit the correction result.

Prompt conflict, auto merge failed. Let's see what happened

ymj@ymj-pc:~/git_test$ cat math.txt 1 + 1 = 2​Here are a few#?######<<<<<<< HEAD​1+1+1+1+1+1 = 6=======3+3=6>>>>>>> f1​

It turns out that both of our branches have answers at the end of the question. At this time, git doesn't know which one to use, so we use

<<<<<<< HEAD​1+1+1+1+1+1 = 6=======3+3=6>>>>>>> f1

This form marks the changes of the current branch (HEAD) and the f1 branch. We need to manually modify this paragraph to the form we want to keep:

Method 1:1+1+1+1+1+1 = 6 Method 2:3+3=6

After modification, continue to add, git merge --continue to complete the merge

ymj@ymj-pc:~/git_test$ git add math.txt ymj@ymj-pc:~/git_test$ git merge --continue[master ce8c35b] Merge branch 'f1'ymj@ymj-pc:~/git_test$ cat math.txt 1 + 1 = 2​Here are a few#?######Method 1:1 + 1 + 1 + 1 + 1 = 6 method 2:3 + 3 = 6

 

Delete useless branches

ymj@ymj-pc:~/git_test$ git branch -d f1 Branch deleted f1(Zeng Wei a0ed2b7). ymj@ymj-pc:~/git_test$ git branch * master

Git branch - D can delete branches that have been merged. If there is content that does not have a merge but does not want it, GIT branch - D will forcibly delete it (use with caution, unless you are sure you don't want it).

 

How to avoid conflict

In fact, there are not conflicts in every merge. Often a direct merge can succeed. Here is to illustrate the conflict.

Conflict is because two branches modify the same place. In order to avoid conflict, do not modify the same place at the same time, so that the automatic merge can succeed. Don't be afraid of conflicts. git helps you manage conflicts. If there are conflicts, it's convenient to change them manually.

 

There are also two particularly easy-to-use commands stash and cherry pick

Start with stash:
If you are modifying a branch but do not commit, switch the branch directly, and the uncommitted content will be brought to the new branch. Due to various reasons, you can't commit now, but you still want to switch to other branches, complete the work of other branches and continue the current work. Then you can use git stash to save the current changes. After you switch back next time, git stash pop will restore the site and continue to work. (this function can also be combined with git reset to avoid merge conflict. Think about how to implement it?)

 

Let's look directly at the use example:

I feel math Txt in front of 1 + 1 = 2 is very eye-catching, I want to delete it. Directly delete the first two lines without add ing or commit ting, but at this time, it suddenly occurred to me that the problem of counting well numbers has not been completed, so I established a branch to implement a new method. At this time, save the changes by stash first:

ymj@ymj-pc:~/git_test$ vi math.txt ymj@ymj-pc:~/git_test$ git diffdiff --git a/math.txt b/math.txtindex 461f29e..595a241 100644--- a/math.txt+++ b/math.txt@@ -1,5 +1,3 @@-1 + 1 = 2- Here are a few#? ### ###ymj@ymj -pc:~/git_ Test $git stash save working directory and index status WIP on Master: ce8c35b merge branch 'F1' ymj@ymj -pc:~/git_ Test $git checkout - B F2 switch to a new branch 'f2'ymj@ymj-pc:~/git_test$ git branch * f2  master

Through this series of operations, I can see that I deleted 2 lines, temporarily modified them, and created and switched to f2 branch.

Then I added two more submissions

ymj@ymj-pc:~/git_test$ git log --onelineda565e9 (HEAD -> f2) 2*3=6ec41b58 2+2+2=6ce8c35b (master) Merge branch 'f1'a0ed2b7 3+3=68abe1f0 1+1+1+1+1+1a57b57f Create problem cbdc648 fix buge595e94 add math.txt43b1405 init commit

It can be seen that the f2 branch has two more commits than the master

da565e9 (HEAD -> f2) 2*3=6ec41b58 2+2+2=6

I think the two new methods are very good. I want to merge them into the master, but this time I don't use git merge, but use git cherry pick:

ymj@ymj-pc:~/git_test$ git checkout master Switch to branch 'master'ymj@ymj-pc:~/git_test$ git cherry-pick ec41b58 da565e9[master b03667e] 2+2+2=6 Date: Wed Jan 8 23:43:16 2020 +0800 1 file changed, 2 insertions(+)[master c1a292a] 2*3=6 Date: Wed Jan 8 23:44:07 2020 +0800 1 file changed, 2 insertions(+)ymj@ymj-pc:~/git_test$ git log --onelinec1a292a (HEAD -> master) 2*3=6b03667e 2+2+2=6ce8c35b Merge branch 'f1'a0ed2b7 3+3=68abe1f0 1+1+1+1+1+1a57b57f Create problem cbdc648 fix buge595e94 add math.txt43b1405 init commitymj@ymj-pc:~/git_test$ 

It can be seen that changes can be merged as long as the commit ID is specified, which is very convenient.

Let's continue the previous work of deleting 1 + 1 = 2:

ymj@ymj-pc:~/git_test$ git stash pop Auto merge math.txt In branch master Changes that have not been staged for submission: (use) "git add <file>..." Update content to submit (using "git checkout -- <file>..." Discard workspace changes)​  Modification: math.txt​Modification has not been added to the submission (use) "git add" and/or "git commit -a")Discarded refs/stash@{0} (5d2cd477bde5327e2ca91d053aa56057be512e08)ymj@ymj-pc:~/git_test$ cat math.txt Here are a few#?######Method 1:1 + 1 + 1 + 1 + 1 = 6 method 2:3 + 3 = 6 method 3:2 + 2 + 2 = 6 method 4:2 * 3= 6ymj@ymj -pc:~/git_ test$ git add math. txt  ymj@ymj -pc:~/git_ Test $git commit - M "delete 1+1=2"[master e98af63] delete 1 + 1 = 2 - 1 file changed, 2 - deletions(-)

Continue to complete the submission, perfect!

 

 

 

 

summary

 

Common operations:

View branch

git branch 

 

 

Create branch

git branch B_NAME

 

Switch branch

git checkout B_NAME 

 

Create branches and switch at the same time

git checkout -b B_NAME 

 

Merge branch

git merge B_NAME 

 

Conflict resolution

Manually modify conflicting files

<<<<<<< HEADHEAD content=======B_NAME content>>>>>>> B_NAME

 

After modification, continue to add, git merge --continue to complete the merge

 

Delete useless branches

git branch -d B_NAME 

git branch -D B_NAME , forcibly delete unmerged branches (use with caution unless you're really sure you don't want them).

 

Avoid conflict

Conflict is because two branches modify the same place. In order to avoid conflict, do not modify the same place at the same time, so that the automatic merge can succeed. Don't be afraid of conflicts. git helps you manage conflicts. If there are conflicts, it's convenient to change them manually.

 

Git stash and git stash pop

Save and restore site for branch switching

 

Cherry picking

Pick one or more submissions to the current branch at a time

git cherry-pick CID [CIDn]


 

 

 

git's branches are lightweight. You can create branches at will without much overhead. git officially recommends using branches.

 

 

----------I'm Peter, a young talent who wants to clarify the problem----------


 


 

Tags: git

Posted by SchweppesAle on Sun, 22 May 2022 10:16:22 +0300