git- create branching and merging branches

# Git- create branching and merging branches

Each commit, Git regard they strung together a timeline, this timeline is a branch. Up to now, only one timeline, in Git, this branch is called the main branch, that is, the master branch. HEAD strictly speaking is not directed to submit, but point to the master, master is the point of submission, so, HEAD is pointing to the current branch.

In the beginning, master branch is a line, Git with master points to the latest submission, and then point to master HEAD, will be able to determine the current branch, and the submission of the current branch point

Each submission, master branch will move one step forward, so, as you continue to submit, master branch lines are getting longer and longer.

When we create a new branch, for example, dev, Git built a pointer called dev, points at the same master, and then point to HEAD dev, says that the current branch on the dev:

Git create a branch soon, because in addition to adding a dev pointer to change to change HEAD point, the workspace file have not changed!

However, from now on, to modify the workspace and submission is for a dev branch, such as new submissions after the first, dev pointer moves one step forward and the master pointer unchanged:

if we work on dev completed, you can the dev merged into the master. Git how merge it? The easiest way is to point directly to the master dev current submission, it completed the merger:

So Git Merge branch soon! To change to change hands, workspace content is the same!

After merging branches, or even delete the dev branch. Dev branch is to delete the dev pointer to delete it, delete, we will rest a master branch:


Combat:

The basic experimental environment:

1. 我们创建dev分支,然后切换到dev分支:使用 `git checkout -b 分支名`


git checkout command with the -b parameter indicates the switching and corresponds to the following two commands:
git branch devandgit checkout dev

2. 用git branch命令查看当前分支


git branch command lists all the branches, ahead of the current branch monogram an asterisk.

3. 在dev分支上正常提交,比如对readme.txt做个修改,加上一行:


4. 提交

5. 现在,dev分支的工作完成,我们就可以切换回master分支:

切换回master分支后,再查看一个readme.txt文件,刚才添加的内容不见了!因为那个提交是在dev分支上,而master分支此刻的提交点并没有变:


6. 现在,我们把dev分支的工作成果合并到master分支上:
git merge 分支名

git merge命令用于合并指定分支到当前分支。合并后,再查看readme.txt的内容,就可以看到,和dev分支的最新提交是完全一样的。
7. 合并完成后,就可以放心地删除dev分支了:

8. 查看分支 git branch就只剩下master分支了:

因为创建、合并和删除分支非常快,所以Git鼓励你使用分支完成某个任务,合并后再
删掉分支,这和直接在master分支上工作效果是一样的,但过程更安全。


summary

查看分支:git branch

创建分支:git branch

切换分支:git checkout

创建+切换分支:git checkout -b

合并某分支到当前分支:git merge

删除分支:git branch -d

Guess you like

Origin www.cnblogs.com/chenyameng/p/11370464.html