Git branch management
Almost every version control system supports branching in some form. Using branches means you can branch off 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 version control system family.
Create branch command:
git branch (branchname)
Switch branch command:
git checkout (branchname)
When you switch branches, Git will replace your working directory with the last committed snapshot of the branch content, so multiple branches do not require multiple directories.
Merge branch command:
git merge
You can merge to the same branch multiple times, or you can choose to delete the merged branch directly after the merge.
Git branch management
List branches
Basic command to list branches:
git branch
Without parameters, git branch will list you branch locally.
$ git branch * master
What this example means is that we have a branch called "master", and this branch is the current branch.
When you execute git init, Git will create the "master" branch for you by default.
If we want to manually create a branch and switch to it. Just execute git branch (branchname).
$ git branch testing $ git branch * master testing
Now we can see that there is a new branch testing.
When you create a new branch after the last commit update in this way, if there is an update commit later, and then you switch to the "testing" branch, Git will restore your working directory to the time you created the branch. What it looks like
Next we will demonstrate how to switch branches. We use git checkout (branch) to switch to the branch we want to modify.
$ 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, the new file test.txt we added was removed, and the originally deleted file hello.php appeared again. When switching back to the "master" branch, they reappeared.
$ 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 the branch to operate in the 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 in the context of that branch, then switched back to our master branch and those files are back.
Use branches to separate work, allowing us to do things in different contexts and switch back and forth.
Delete branch
Delete branch command:
git branch -d (branchname)
For example, we want to delete the "testing" branch:
$ git branch * master testing $ git branch -d testing Deleted branch testing (was 85fc7e7). $ git branch * master
Branch merge
Once When a branch has independent content, you will eventually want to merge it back into your main branch. You can use the following command to merge any branch into the current branch:
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 merged the newtest branch into the main branch, and the test2.txt file was deleted.
Merge conflicts
Merge is not just a simple operation of adding and removing files, Git will also merge modifications.
$ git branch * master $ cat test.txt w3cschool.cc
First, we create a branch called "change_site", switch to it, and change the 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(-)
Submit the modified content to the "change_site" branch. Now, if we switch back to the "master" branch we can see that the content is restored to what it was before we modified it, and 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" branch. Next we merge the "change_site" 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.cc 新增加一行 ======= www.w3cschool.cc >>>>>>> change_site
We merged the previous branch into the "master" branch, and a merge conflict occurred. Next, 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, we can use git add to tell Git that the 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 committed the result.