Learn Git:git Branch notes

Source: Internet
Author: User

Learning Video: http://edu.51cto.com/course/course_id-1838.html Finishing

Git Learning-git Branch


In Git, branch is a text, put a hash value, where the git branch command lists all the branch

$ git branch* master$ git branch--list* Master


Creating a branch command is a git branch <branch_name>, such as creating a Web branch

$ git branch web$ git branch

* Master

Web


You can see that the Web Branch has been created and can be listed with the GIT branch command.


From the above can see we are currently in *master this branch, if we switch to the web this branch, you can use the command git checkout Web

$ git checkout Web

Switched to branch ' web '

$ git Branch--list

Master

* Web


You can see that there is a * number in front of the web, which means the web is the current branch.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/74/6D/wKioL1YdIbexWKE9AAAxL3lk7tw616.jpg "title=" Webbranch-pointer.png "alt=" Wkiol1ydibexwke9aaaxl3lk7tw616.jpg "/>


As a matter of fact, the Web branch is also pointing to the C commit object. Verify the following command

Enter the./git/refs/heads folder and you can see the Web file

by command

$ cat *

86a4a0ecd556382aac675869697e19b7dc3f37b7

86a4a0ecd556382aac675869697e19b7dc3f37b7


You can see that both the master file and the Web file contents are the same hash values, so they point to the same commit object,

$ git rev-parse web

86a4a0ecd556382aac675869697e19b7dc3f37b7

$ git rev-parse Master

86a4a0ecd556382aac675869697e19b7dc3f37b7


It can be seen through the two commands above.


But you passed the order.

$ cat HEAD

Ref:refs/heads/web

You can see that the head content has changed, from the original head point to master to point to the Web branch, that is, the same way

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/70/wKiom1YdIiLA5w-0AAAykk1H-r4839.jpg "title=" Webbranch-pointer2.png "alt=" Wkiom1ydiila5w-0aaaykk1h-r4839.jpg "/>

$ git Checkout Master

Switched to branch ' master '

Your branch is up-to-date with ' origin/master '.

$ cat. Git/head

Ref:refs/heads/master

$ git Branch

* Master

Web


The first command above is to switch back to the master branch, and the next two proves that the switchover was successful.

Deleting branch is simple: Git branch-d Web

$ git branch-d web

Deleted Branch web (was 86a4a0e).

$ git Branch

* Master


~~~~


git checkout–b <branch> command can create a branch branch, and switch to this branch, see the following command

$ git Branch

* Master

$ git checkout-b web

Switched to a new branch ' web '

$ git Branch

Master

* Web


See the * number in front of the Green Web Branch, which means that the Web branch was created and switched to this branch.


Add a file to this new branch and commit

$ echo "This is a Java beginner project" > readme.md
$ git status

On Branch web

untracked files:

(use "git add <file> ..." to include in what'll be committed)


Readme.md


Nothing added to commits but untracked files present (use "Git add" to track)

$ git Add readme.md

WARNING:LF is replaced by CRLF in README.MD.

The file would have their original line endings in your working directory.

$ git status

On Branch web

Changes to be committed:

(use "Git reset HEAD <file> ..." to Unstage)


New FILE:README.MD


Then try to delete the Web branch

$ git branch-d web

Error:the Branch ' web ' is not fully merged.

If you're sure want to delete it, run ' git branch-d web '.

Look at the above tips to know the problem, if you delete a branch with-D, you need to merge this branch; If you want to not merge delete, use the-D parameter

$ git Merge Web

Updating 86a4a0e. d1bc16f

Fast-forward

readme.md | 2 + +

1 file changed, 2 insertions (+)

Create mode 100644 readme.md

$ git branch-d web

Deleted Branch web (was d1bc16f).

The above fast-forward represents the most recent commit of the original Web branch parent commit object is the Master branch, if the Web branch parent commit object is not the most recent commit of the Master branch, it is not fast-forward

3-way Merge

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/74/70/wKiom1YdIu2S9FnpAAB6G7iWS4Y821.jpg "title=" 3- Way-merge.png "alt=" Wkiom1ydiu2s9fnpaab6g7iws4y821.jpg "/>


The above figure is I based on the video presentation and my own understanding, more than the video map in the Master and Web branch of a commit, when the merger

1. compare C and H first, then generate a patch

2. Apply patch to E, generate I this Commit object,

3.The master pointer points to the I object

4. If you point to the git merge Web and git branch–d Web commands, the Web branch is deleted,

5. and F,g,h These commits are discarded by Git's garbage collection mechanism.

The above method is 3-way merge

In my practice, the commit hash value of C is commit 971043d5f42cb3c917bfb1b75df9bedf91eef08a

The commit hash value for h in my web branch is commit e0da286c1621328a43dc1d5eaa7a57c28251f0d4

The commit hash value of E in my master branch commits 83424500c510ae68bbc25ee1e641f14e492bf132

$ git Merge Web

Auto-merging Firstfile.txt

CONFLICT (content): Merge CONFLICT in Firstfile.txt

Automatic merge failed; Fix conflicts and then commits the result.


When I was in the merge, there was a merge conflict that needed to be solved manually.

$ git status

On Branch Master

You have unmerged paths.

(Fix conflicts and run "git commit")


unmerged paths:

(use "git add <file> ..." to mark Resolution)


Both Modified:firstfile.txt


No changes added to commit (use "git add" and/or "Git Commit-a")

$ git Add firstfile.txt

$ git commit-m "master branch:prompt merge conflict, so need to merge manually"

[Master 8A63C46] Master branch:prompt merge conflict, so need to merge manually


The above on behalf of the merge successfully, manually resolved.

$ git log

Commit 8A63C46C7E757EC893C134542CB78FA4EE3D4F2F

merge:8342450 e0da286


With the above log, you can see that commit I has appeared, and that he is a combination of commit E and commit H production, and the diagram above us is consistent

$ git branch-d web

Deleted Branch web (was e0da286). Look at the value of parentheses, just like the H commit hash value in the Web branch

This article is from the "mundane" blog, make sure to keep this source http://6430765.blog.51cto.com/6420765/1702677

Learn Git:git Branch notes

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.