Git branch management and Git branch management

Source: Internet
Author: User

Git branch management and Git branch management
Git branch management overview

Almost every version control system supports branches in some form. Using a branch means you can separate it from the Development Main Line and continue working without affecting the main line.

Some people refer to Git's branch model as a "killer feature" because it distinguishes Git from the version control system family.

Create branch command:

Git branch (branchname)

Switch branch command:

Git checkout (branchname)

When you switch the branch, Git will replace the contents of your working directory with the snapshot submitted at the end of the branch. Therefore, multiple directories are not required for multiple branches.

Merge branch commands:

Git merge

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

1. List branches

List basic branch commands:

Git branch

If no parameter exists, git branch will list your local branches.

$ Git branch * master

In this example, we have a branch called "master" and the branch is the current branch.

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

If you want to manually create a branch and switch to another branch. Run 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 update is submitted in this way, if you submit an update later, and then switch to the "testing" branch, git restores your working directory to the way you created the branch.

Next we will demonstrate how to switch the branch. 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 testingSwitched to branch 'testing'

$ Ls

README hello. php

When we switch to the "testing" branch, the new test.txt file is removed, and the deleted file hello. php appears again. When switching back to the "master" branch, they reappear.

$ Git checkout masterSwitched to branch 'master'

$ Ls

README test.txt

You can also use the git checkout-B (branchname) command to create a new branch and immediately switch it to the branch to operate it.

$ Git checkout-B newtestSwitched 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 masterSwitched to branch 'master'

$ Ls

README test.txt test2.txt

As you can see, we have created a branch, removed some files from the context of the branch, and then switched back to our main branch. Those files are returned.

Splitting jobs with branches allows us to work in different contexts and switch back and forth.

Ii. 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 testingDeleted branch testing (was 85fc7e7 ).

$ Git branch * master

3. merge branches

Once a branch has its own content, you will eventually want to merge it back to 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 newtestUpdating 2e082b7 .. 556f0a0Fast-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 to the primary branch, and delete the test2.txt file.

Iv. Merge conflicts

Merging is not only a simple operation of adding and removing files, but also a merge and modification of Git.

$ Git branch * master

$ Cat test.txt

W3cschool. cc

First, we create a branch called "change_site" and switch it over to www. w3cschool. cc.

$ Git checkout-B change_siteSwitched 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 internal volume is restored to the one we modified before, and we will modify the test.txt file again.

$ Git checkout masterSwitched to branch 'master'

$ Head-1 test.txt

W3cschool. cc

$ Vim test.txt

$ Cat test.txt

W3cschool. cc adds a new row

$ 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 + Add a new row

$ Git commit-am' Add a new row '[master 14b4dca] Add a new row

1 file changed, 1 insertion (+)

 

Now these changes have been recorded in my "master" branch. Next we will merge the "change_site" branches.

$ GitHub merge change_siteAuto-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 adds a new line ======

Www. w3cschool. cc >>>>>> change_site

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

$ Vim test.txt

$ Cat test.txt

Www. w3cschool. cc adds a new row

$ Git diff

Diff -- cc test.txt

Index f84c2a4, bccb7c2 .. 0000000 --- a/test.txt ++ B/test.txt @--+ @-w3cschool. cc + www. w3cschool. cc

+ Add a new row

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 results.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.