Git Learning (2) --- branch operations

Source: Internet
Author: User
1. Purpose This article describes how to operate git branches, including creating and deleting branches, viewing and switching branches, merging branches, and merging conflicts.
All operations involved in this article are performed in the ubuntu14.04 environment, and the GIT version is 1.9.1.


2. Create and delete branches use the GIT branch name command to create a branch named name. Use the GIT branch-D name command to delete a branch named name.

The default branch of git is the master. In actual projects, a next branch usually exists. The master branch is the master version and meets the requirements of the bugfix. The next branch is used for integration testing, and new features are accepted after development. In addition, some topic branches exist. Each topic branch is used to implement a new feature. After the new feature is developed, the maintainer of the next branch is merged into the next branch, then the topic branch can be deleted.



3. View and switch branches. Use the GIT branch command to view all branches. The branch marked with an asterisk represents the current branch. Use the GIT checkout name command to switch to the name branch.

Git uses the head index to point to the current branch. For example, the master and next branches exist in the version. After the GIT checkout next command is used to switch to the next branch, the head points to next; use the GIT checkout master command to switch to the master branch, and the head points to the master.



Iv. merge branches

Common branch merge Commands include git merger, git rebase, and git cherry-pick.


1. Git merger
Git merger gradually merges the commit on each branch in chronological order to obtain a new commit.

For example, the version has two branches: Master and next. After C3, the master submits a C5, And next submits a C4 and C6.


The master uses Git merger next to merge C4 and C6 of the next branch into the master. First, git merges C4 after C3 according to the time sequence of the commit. Then, then combine C5 and C6. After such calculation, the C7 is obtained.



2. Git rebase
Git rebase first caches the commit of the current branch, then merges the commit of the branch to be merged, and then merges the cached commit.
For example, the version has two branches: Master and next. After C3, the master submits a C5, And next submits a C4 and C6.
Next use git merger master to merge the C5 of the master branch into next. First, git caches C4 and C6 of the next branch, and then integrates the C5 of the master branch; the cached C4 and C6.


3. Git cherry-pick
The GIT cherry-pick command is used to merge a specific commit in a branch.
For example, the version has two branches: Master and next. After C3, the master submits a C5, And next submits a C4 and C6.
The master uses the GIT cherry-pick Command to merge only C4 in the next branch, but not C6.



5. Merge conflicts when merging branches, merge conflicts often occur. Use git status to view the cause of the conflict. After resolving the conflict, generate a new commit and submit it to commit.
Git explicitly identifies the conflicting content in the conflicting file.

For example,




Vi. Summary

This article introduces the biggest feature and advantage of git: branch technology. This section describes how to create and delete branches, view and switch branches, and focuses on Branch merge and conflict resolution.


Copyright statement: original works. For non-commercial reproduction, please indicate the source. For commercial reproduction and publishing, please contact the author.

Git Learning (2) --- branch operations

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.