Git Step by step– (5) Git branch (branch)

Source: Internet
Author: User
Tags using git

The basic principles of git are described in the previous two-disk article, which is theoretical knowledge. In this article we go back to practice and look at the use of Git branching (branch).

In the Code versioning tool, there is a branch concept. When we started building the repository, we had only one main branch (Master Branch), and it was not possible to put the concept of daily new functionality development, code optimization, and bug fixes on the main branch, which would make the main branch difficult to maintain. That's why there are branch.

Creation of branching and deletion of branches

In Git, the creation of branch is simple, and we can create a "release-1.0" branch with the following command.

You can then switch branch with the "Checkout" command.

In Git, we can use the "Git checkout-b" command to create and switch branch effects at the same time.

Note : We must remember the second article in the "Checkout" command to restore the update in workspace, in the restored command we use "checkout-", if there is no "-" on behalf of the switch branch.

Based on the knowledge of the previous two articles, we entered the ". Git/refs/heads" directory, found "HEAD" and "release-1.0" two files, and two files contain the same hash value, according to "Git log" You can tell that this hash represents the most recent commit on Master. So, after we create the branch, we get the following diagram, which we can see from the value map that the branch switch corresponds to the change in the head reference value.

With the new branch, we can work on separate branch. Let's say we Update "app.py" now and submit it on the release-1.0 branch to see the object diagram again.

Based on the above object graph, you can analyze the following information:

    • The hash value in the "ref/heads/release-1.0" file will be updated to the latest update on release-1.0 branch
    • release-1.0 Branch The above update will not be reflected in the master branch

After viewing, the contents of the "ref/heads/release-1.0" file are indeed the latest updates on release-1.0 branch. And when we switch to Master branch, everything on the master doesn't change.

Deletion of Branches

The deletion of the branch is relatively simple, you can use the "Git branch-d" command, in order to not affect the later content, here is the demo new, delete Dev branch.

Merging of branches

Branch is created to facilitate development, fix bugs, and keep master stable. But eventually the content on the branch is going to be merged into Master, then look at the merging of the branches.

In Git, you can merge branches with "git merge" and continue with the previous example, and we can merge the "app.py" update into the master branch.

At this point, Master's head is updated, consistent with the release-1.0 content, which is what the merge command does.

Merge conflicts

In the branch merger, many times we encounter conflicts, then we need to resolve the conflicts manually before committing.

To demonstrate the conflict merge, we rolled back master to the last commit (remember git reset), and then the same update "app.py" was submitted to master.

As you can see above, the "app.py" is also modified on master and release-1.0, resulting in conflicts when merging. In Git, the conflict areas are marked with "<<<<<<<", "=======", ">>>>>>>", and we need to find all the conflicts and resolve them according to these symbols.

After the conflict is resolved, we produce some results through "git log", but this time we will add some parameters to "git log".

At the same time, the most recent object diagram is given here.

Branch related operations

In the use of branch, we will often use the Stash and diff operations, respectively, described below.

Stash

In Git, stash is a useful command that preserves half of our work and can be understood as a saving area for unfinished work.

If we do some updates in release-1.0 branch, but what we want to do is not all done yet, we cannot commit, this is what we have to switch to Master branch, which is git will disable branch switch.

For example, we removed "div (16, 4)" from app.py.

At this point we need to use stash to save the unfinished work.

If you need to continue with the previous work, we can use "git stash apply" to restore the saved work state.

Stash space is like a stack of space, each time through stash preservation and other content will be pressed into the stash stack. Commands are more than just support for simple list and apply operations, so let's look at more stash commands.

    1. Git stash Save: You can use custom information to describe a stash

    2. git stash apply [email protected]{n}: With this command, we can select stash in the stash stack to revert to a specific state; "Git stash apply" uses the top of the stack to restore workspace.
    3. Git stash pop: like "git stash apply" uses the top of the stack to restore workspace, but it also removes the current stack top stash from the stash stack.
Stash Working principle

I believe we all see the strong stash, the following we look at stash work principle. After using stash save, we will find two new files in the. git directory ". Git/refs/stash" and ". Git/logs/refs/stash". The contents of the two files were as follows:

. Git/refs/stash

Dcac98e565864edc6f636b08660baebe2c97e7d2

. Git/logs/refs/stash

0000000000000000000000000000000000000000 1420201804 +0800    WIP on release-1.0: ed17809 update app.py on release branch

Based on what we learned in the Object Relational model article, we can get the following information, so we can see that the content of stash storage can be found in a little bit according to the object Relational model.

This does not provide object diagram, more complex, interested students can draw a look at the relationship between each object.

diff between the branch

In the previous article we compared the differences in Wrokspace, stage, and repo with the content on the same branch by diff.

The same diff can support comparisons between branches.

    • git diff branchname: Compare the current branch with Branchname, or use "Git diff Branchnamea Branchnameb"
    • git diff branchname--filename: Compare the filename file differences of two branch

Summarize

This article introduces the basic use of the branch and introduces a very useful stash command.

Branching is easy to create in code control using Git, so branching is also recommended for developing new features and bug fixes.

Git Step by step– (5) 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.