Git Branch Management
Almost every version control system supports branches 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 branching model a "nirvana feature", and it is precisely because of it that it 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 use the last commit snapshot of the branch Replace the contents of your working directory, so multiple branches don't 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 merging.
Git branch management
List branches
Basic command to list branches:
git branch
Without parameters, git branch will list your local branches.
$ 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 switch to the "testing" branch, Git
Your working directory will be restored to the way it was when you created the branch
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,
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 a branch has Standalone content, you'll eventually want to merge it back into your master 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 首先,我们创建一个叫做"change_site"的分支,切换过去,我们将内容改为 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 "master" In the branch, we can see that the content is restored to what it was before we modified it. 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 appeared. 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 submitted the result.
The above is the detailed explanation of branch management in Git tutorial. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!