Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Git branch management


May 25, 2021 Git


Table of contents


Git branch management

Almost every version control system supports branches in some form. U sing branches means that you can separate from the development main line and then continue to work without affecting the main line.

Some people refer to Git's branching model as a "must-kill feature" because it distinguishes Git from the version control system family.

Create a branch command:

git branch (branchname)

Switch branch commands:

git checkout (branchname)

When you switch branches, Git replaces the contents of your working directory with the last committed snapshot of the branch, so multiple branches do not require multiple directories.

Merge branch commands:

git merge 

You can merge into a unified branch multiple times, or you can choose to delete the merged branch directly after the merge.


Git branch management

List the branches

List the branch basic commands:

git branch

When there are no parameters, git branch lists your local branch.

$ git branch
* master

This example means that we have a branch called Master, and the branch is the current branch.

When you execute git init, Git creates a "master" branch for you by default.

If we want to manually create a branch and switch past. J ust execute the git branchname.

$ git branch testing
$ git branch
* master
  testing

Now we can see that there is a new branch of testing.

When you create a new branch after you last committed an update, and if you later commit an update, and then switch to the "testing" branch, Git restores your working directory to the way it was when you created the branch

Next we'll show you how to switch branches, and we'll switch with git checkout to the branch we want to modify.

$ ls
README
$ echo 'w3cschool.cn' > test.txt
$ git add .
$ git commit -m 'add test.txt'
[master 048598f] add test.txt
 2 files changed, 1 insertion(+), 3 deletions(-)
 delete mode 100644 hello.php
 create mode 100644 test.txt
$ ls
README       test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README      hello.php

When we switched to the "testing" branch, the new file test.txt we added was removed, and the original deleted file hello .php appeared again. When you switch back to the "master" branch, they reappeate.

$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt

We can also use the git checkout -b (branchname) command to create a new branch and immediately switch to it to operate in that branch.

$ git checkout -b newtest
Switched to a new branch 'newtest'
$ git rm test2.txt 
rm 'test2.txt'
$ ls
README      test.txt
$ git commit -am 'removed test2.txt'
[newtest 556f0a0] removed test2.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test2.txt
$ git checkout master
Switched to branch 'master'
$ ls
README        test.txt    test2.txt

As you can see, we created a branch, removed some files from the context of that branch, and then switched back to our main branch, and those files came back.

Using branches separates the work, allowing us to do things in different contexts and switch back and forth.

Delete the branch

Remove branch commands:

git branch -d (branchname)

For example, we're going to delete the "testing" branch:

$ git branch
* master
  testing
$ git branch -d testing
Deleted branch testing (was 85fc7e7).
$ git branch
* master

Branch merge

Once a branch has stand-alone content, you'll want to merge it back into your main branch. You can merge any branch into the current branch using the following command:

git merge
$ git branch
* master
  newtest
$ ls
README       test.txt    test2.txt
$ git merge newtest
Updating 2e082b7..556f0a0
Fast-forward
 test2.txt | 1 -
 1 file changed, 1 deletion(-)
 delete mode 100644 test2.txt
$ ls
README      test.txt

In the above example, we merge the newtest branch into the main branch, and the test2 .txt the file is deleted.

Merge conflict

Merge is not just a simple file addition or removal operation, Git also merges modifications.

$ git branch
* master
$ cat test.txt
w3cschool.cn

First, we create a branch called change_site" and switch past, changing the content to www.w3cschool.cn.

$ git checkout -b change_site
Switched to a new branch 'change_site'
$ vim test.txt 
$ head -1 test.txt 
www.w3cschool.cn
$ git commit -am 'changed the site'
[change_site d7e7346] changed the site
 1 file changed, 1 insertion(+), 1 deletion(-)
 

Submit the modified content to the change_site Branch. N ow, if we switch back to the "master" branch we can see that the content is restored before we modify it, we modify the test .txt file again.

$ git checkout master
Switched to branch 'master'
$ head -1 test.txt 
w3cschool.cn
$ vim test.txt 
$ cat test.txt
w3cschool.cn
新增加一行
$ git diff
diff --git a/test.txt b/test.txt
index 704cce7..f84c2a4 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 w3cschool.cn
+新增加一行
$ git commit -am '新增加一行'
[master 14b4dca] 新增加一行
 1 file changed, 1 insertion(+)
 

Now these changes have been recorded in my "master" branch. Next we'll change_site the "Called" branch.

 $ git merge change_site
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat test.txt 
<<<<<<< HEAD w3cschool.cn 新增加一行 ======= www.w3cschool.cn >>>>>>> change_site

We merge the previous branch into the "master" branch, and a merge conflict arises, and then we need to modify it manually.

$ vim test.txt 
$ cat test.txt 
www.w3cschool.cn
新增加一行
$ git diff
diff --cc test.txt
index f84c2a4,bccb7c2..0000000
--- a/test.txt
+++ b/test.txt
@@@ -1,2 -1,1 +1,2 @@@
- w3cschool.cn
+ www.w3cschool.cn
 +新增加一行

In Git, we can use git add to tell you that the Git file conflict has been resolved

$ git status -s
UU test.txt
$ git add test.txt 
$ git status -s
M  test.txt
$ git commit
[master 88afe0e] Merge branch 'change_site'

Now we have successfully resolved the conflict in the merge and submitted the results.