Latest web development tutorials

Git branch management

Almost every version control systems support some form of branch. It means you can use a branch from the main line of development to separate and does not affect the main line while continuing to work.

Someone Git branching model called "nirvana Characteristics", but precisely because it distinguished from the Git version control system for the family.

Create a branch instruction:

git branch (branchname)

Branch switching command:

git checkout (branchname)

When you switch branches, Git will use the branch's final submissions snapshots replace the contents of your working directory, multiple branch does not require multiple directories.

Merge Branches command:

git merge 

You can merge into a unified branch many times, you can also choose to delete directly after the merger be incorporated branches.


Git branch management

Lists branch

Basic commands are listed in the branch:

git branch

When no arguments, git branch will be listed in your local branch.

$ git branch
* master

In this case it means that we have called a "master" branch, and the branch is the current branch.

When you run git init, the default Git will create "master" branch for you.

If we want to manually create a branch and switch over. Execute git branch (branchname) can be.

$ git branch testing
$ git branch
* master
  testing

Now we can see that with a new branch testing.

When you submit an update in this way in the last created a new branch, if later also submit updates, and then switch to the "testing" branch, Git will restore your working directory to look like when you create a branch

Next, we will show you how to switch branches, we switched with git checkout (branch) that we want to modify the branch.

$ ls
README
$ echo 'w3cschool.cc' > 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, we add a new file test.txt was removed, the original file is deleted file hello.php has emerged. Switching back to the "master" branch, they have re-emerged.

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

We can also use git checkout -b (branchname) command to create a new branch and immediately switched to the branch, in order 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 have created a branch, remove some files in the context of this branch, and then switch back to our main branch, those files back.

Use separate cutting branch will work, so that we can do things in different contexts, and switch back and forth.

Remove branches

Remove the branch instruction:

git branch -d (branchname)

For example, we want to remove "testing" branch:

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

Merge branch

Once a branch has independent content, you will eventually want to merge it back into your master branch. You can use the following command to any branch merged into the current branch to:

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

The above examples we will newtest merged into the main branch to branch, test2.txt file is deleted.

Merge Conflicts

Combined and not simply add a file, the operation removed, Git will also merge the changes.

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

First, we create a directory called "change_site" branch switching in the past, we will be content to www.w3cschool.cc.

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

The modified content submitted to "change_site" branch. Now, if you switch back to the "master" branch that we can return to see the contents of our modifications before we modify the test.txt file again.

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

Now these changes have been recorded in my "master" of the branch. Next we will "change_site" branch consolidation over.

 $ 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.cc
新增加一行
=======
www.w3cschool.cc
>>>>>>> change_site

We will merge before a branch to "master" branch, a merge conflict arises, then we need to modify it manually.

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

In Git, git add that we can use to tell 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 merge conflicts, and submit the results.