Basic use of [Git] git branch (branch)

Source: Internet
Author: User

Branch (branches) refers to the development of the main line of separation, do further development without affecting the original main line.

Git does not store a series of change sets (changeset), but rather a series of snapshots. When you perform a commit, Git stores a Commit object that contains a pointer to a snapshot of what you currently need to commit.

The function of the master branch in Git is the same as the other branches. Master is common in git projects because the GIT init command runs with the default creation of a branch and is named Master.

Create a new branch, which is to create a new pointer to move between snapshots. Git uses the HEAD pointer to point to the local branch of the current work.

$ git Branch Testing

You can switch branches by using the git checkcout command.

$ git checkout Testing

The branch pointer is moved after the file is modified and the commit code is changed

' Update TEST.RB '

You can switch back to the master branch by checkout. The following command does two things, one is to point the HEAD pointer to the master branch, and the second is to restore the current working directory file to the snapshot version that master points to. That is, the changes that 87AB2 commits are removed when switching to the master branch.

$ git Checkout Master

Make some changes and commit again. Note that at this point the history begins to appear forked.

' Make other changes '

The branch in Git is actually a simple 40-character file, and the 40 characters are the SHA-1 checksum generated by a single commit. Therefore, the cost of creating and destroying a branch in Git is very low. Compared to most other version management systems, Git doesn't have to copy the entire project's files, which is a huge difference.

Basic branching and merging

The following example is a more common scenario where branching and merging functions are applied.

    1. In developing a Web site
    2. Create a branch (ISS53) to handle the new project requirements (user story)
    3. On this branch, I did some work.

At this point, receive another urgent problem that needs immediate repair (hotfix)

4. Switch to the Production environment branch

5. Create a new branch (hotfix) to fix the problem on the line

6. After the test, merge the repair branch into the main branch and push it to the production environment

7. Switch to the original demand branch and continue working

The initial status is as follows

Through Branch/checkout/commit and other operations, to do the 5th step above, the status is as follows

Next, merge the hotfix branch and then deploy to the online environment.

$ git checkout master$ git merge hotfixupdating f42c576. 3a0874cfast-21file2 insertions (+)

In the above hint, there is the fast-forward word, which indicates that the hotfix branch points to the C4 commit, which is the ancestor of the C3 submission that the master branch points to, so move master directly to C4. Post-merger effect relationships are as follows

Since hotfix has been merged into the master branch, both master and hotfix point to the same place, and subsequent operations do not need to use the hotfix branch, so they can be deleted.

$ git branch-d Hotfix

Down you can switch to the ISS53 branch to continue working, continue to modify, and then commit. When the modification is complete, the relationship is roughly as follows

" iss53 "  'finished the new footer [issue]'1  file1 insertion (+)

Then start merging.

' Master '  'recursive'|    1 +1file1 insertion (+)

There is no fast-forward word for merging here, because it is not simple to move the branch pointer. Git merges with the three-way merge (Three-way merge) and creates a commit (C6) that contains the merged results. The three-way merging method refers to the branch merging by the newest commits of two branches (C4 and C5), and their common ancestor C2. C6 is a merge commit record, with two ancestors in particular.

At this point, you no longer need a iss53 branch, you can delete it.

$ git branch-d iss53

Basic merge conflict handling

If two branches modify the same part of the same file, such as the same line of code, Git does not know how to merge, which is considered a conflict. For example, if you modify the same part of the index.html file in the iss53 and hotfix branches, merging Iss53 to the master branch that already contains hotfix, the effect is as follows

$ git merge iss53auto- in then commit the result.

At this point, the automatic merge pauses and requires a manual merge. Git status shows conflicting files

$ git Statuson branch masteryou has unmerged paths.   " git commit " ) unmerged paths:  "git add <file> " To Mark resolution)    Both modified:      "git add""git commit-a" )

Git adds a standard conflict resolution identifier to the conflicting file.

<<<<<<< HEAD:index.htmlid="footer"ID ="footer"> Contact us at [email protected]</div>>>>> >>> iss53:index.html

This indicates that the HEAD version of the code is ======= the above section, iss53 version is ======= the following section.

Fight index.html file, modify it to your desired version, and remove <<<<<<< ======= >>>>>>> logo, then use git add The identity conflict is close, and then git commit commits to resolving the conflicting version.

Resources

3.1 Branches In a nutshell, GIT-SCM

3.2 Basic branching and merging, GIT-SCM

Basic use of [Git] git branch (branch)

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.