Git branch management - create, merge, delete branch

Preface:

Almost all version control support branch in some form. Use branch means you can take your work from the main line of development separated, so as not to affect the main line of development.

Git branching model referred to as its "nirvana characteristics", because of this characteristic, so stand out from Git version control system. Git manner branch is incredibly lightweight, creating a new branch of the second stage the operation is completed, and a switching operation between different branches of the same is easy.

Git branch, in fact, is essentially just a pointer to a volatile submit object. The default branch in Git is master. After submitting multiple times, in fact, we already have a master branch points to the final submission of the object. He will automatically move forward in each of the commit operation.

Our practice may encounter a situation the following:

  • Development of a website.
  • In order to achieve a certain new requirements, create a branch.
  • Work in this branch.
  • At that moment, you suddenly got a call that there was a very serious problem in need of urgent repair. You will be handled as follows:
  • Switch to your branch line (production branch).
  • Create a new branch for this urgent task, and where to fix it.
  • After the test passed, switch back to the branch line, and then merge the patch branch push changes to the last line of the branch.
  • After modifying switch back to your original work on the branch, continue to work.

More details on git, or venue and its official documents it!


Bowen outline:
1, initialize a directory of users and e-mail address and declare
2, create, delete and fast merge branch
3, resolve conflicts branch
4, turn off the quick merger
5, Bug branch
6, Git branch management-related commands

1, a directory and initialize statement users and e-mail address

[root@git ll]# git init
[root@git ll]# git config --global user.name admin
[root@git ll]# git config --global user.email [email protected]

2, create, delete and fast merge branches

[root@git ll]# echo "aaaa" > branch.txt
[root@git ll]# git add branch.txt
[root@git ll]# git commit -m "第一次提交 From master"
#创建一个分支并进入新建的分支
[root@git ll]# git checkout -b dev
[root@git ll]# git branch       #查看当前所在分支
* dev           #星号所在的列就是当前所在分支
  master
#在dev分支更新文件并提交
[root@git ll]# echo "bbbb" >> branch.txt 
[root@git ll]# git add *
[root@git ll]# git commit -m "commit From dev branch"
[root@git ll]# cat branch.txt      #确认当前内容
aaaa
bbbb
[root@git ll]# git checkout master       #切换到master分支
[root@git ll]# cat branch.txt       #确认当前内容
aaaa
[root@git ll]# git merge dev        #合并dev分支
[root@git ll]# cat branch.txt    #再次查看其内容
aaaa
bbbb
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit   #查看提交日志
#可以查看到其提交日志前面的星号是在同列的,这是因为采用的是快速合并的方法(默认)
#稍后会写下关闭快速合并,然后可以对比该命令查看的结果
* bc8bd7b commit From dev branch
* 8bb6874 第一次提交 From master
[root@git ll]# git branch -d dev       #删除dev分支

3, resolve conflicts branch

In our practical work experience problems branch of a conflict, that is, when the contents of the file you are working in a branch dev modify, and then before you commit to the repository, content in the master branch has changed, this time your content under dev branch is than is the old master to submit this case, there will be a branch of the concept of conflict, chestnuts as follows:

[root@git ll]# cat branch.txt      #查看master分支的此文件内容
aaaa
[root@git ll]# git checkout -b dev        #创建并切换至自己的工作分支
 #对文件内容进行修改并提交到版本库
[root@git ll]# echo "bbbb" >> branch.txt   
[root@git ll]# git add branch.txt
[root@git ll]# git commit -m "alter from dev"
[root@git ll]# cat branch.txt     #查看文件内容
aaaa
bbbb
[root@git ll]# git checkout master     #切换至master
[root@git ll]# cat branch.txt      #master下的文件内容还是原来的
aaaa
#修改master下的文件内容并提交
[root@git ll]# echo "cccc" >> branch.txt 
[root@git ll]# git add branch.txt
[root@git ll]# git commit -m "alter from master"
[root@git ll]# cat branch.txt     #此时master下的文件内容如下
aaaa
cccc
#接下来将dev分支进行合并:
[root@git ll]# git merge dev        #返回如下报错信息,说有冲突
自动合并 branch.txt
冲突(内容):合并冲突于 branch.txt
自动合并失败,修正冲突然后提交修正的结果。

#解决合并冲突
#其实有上述报错后,dev分支下的内容已经存在了master目录下的文件中,只是没有提交而已,提交即可
#但是工作中不建议直接提交,因为内容有些特殊的地方
[root@git ll]# vim branch.txt       #此时文件的内容如下

aaaa
<<<<<<< HEAD
cccc
=======
bbbb
>>>>>>> dev
[root@git ll]# cat branch.txt   #将冲突报错产生的特殊符号删除再提交
aaaa
cccc
bbbb
[root@git ll]# git add branch.txt
[root@git ll]# git commit -m "冲突已解决"
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit   #查看分支合并情况
*   da2bcdb 冲突已解决
|\  
| * 6abac82 alter from dev
* | 2b5d2f0 alter from master
|/  
* ef014ec alter from master

4, close the quick merger

In the above said, at the time of submission history View git version of its branching structure of the performance is not so intuitive, it is because of the rapid consolidation enabled by default option, write down here how to turn off quickly merge.

#进入分支,修改文件内容,并提交
[root@git ll]# git checkout -b dev
[root@git ll]# echo "ffff" >> branch.txt 
[root@git ll]# git add branch.txt
[root@git ll]# git commit -m "关闭快速合并"
#切换至master分支,进行合并
[root@git ll]# git checkout master 
[root@git ll]# git merge --no-ff -m "分支合并说明" dev    #选项“--no--ff”就是关闭快速合并
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit 
#再次查看提交日志,
*   38c4fad Merge branch 'dev'
|\  
| * 9233297 关闭快速合并
|/  
*   7e0ea1b 冲突已解决    #从这里开始向上看,可以看到是经过一个分支才提交的
|\  
| * f9180c9 alter from dev
* | 107081a alter from branch/bug
|/  
#以下是最初没有关闭快速合并的分支合并操作,可以看到只有一列星号,而不显示分支
* bc8bd7b commit From dev branch          
* 8bb6874 第一次提交 From master
[root@git ll]# git branch -d dev       #删除dev分支

5, Bug branch

Developers in the development process, the same bug as commonplace, with the bug fix is ​​necessary, in git, because the branch is strong, so the bug can be repaired by a new temporary branch, after the repair, branching and merging, then temporary branch will be deleted.

When we receive a bug modification task, it is natural to want to create a branch fix it, but the current work in progress halfway through, can not be submitted, but needs immediate repair bug, this time, you can git stash functionality provided, the current work area can "store up", and so continue to work after recovery site.

Work zone to half the working state as follows:

[root@git ll]# cat branch.txt 
aaaa
cccc
bbbb
[root@git ll]# echo "dddd" >> branch.txt 
[root@git ll]# git status     #提示修改但是尚未提交
# 位于分支 dev
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#   修改:      branch.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

At this point the work area to hide

[root@git ll]# git stash        #就是这条命令,可以将当前工作区隐藏
[root@git ll]# git status        #再次查看当前工作区,就是干净的了
# 位于分支 dev
无文件要提交,干净的工作区
[root@git ll]# cat branch.txt       #文件也没有我们新加的内容
aaaa
cccc
bbbb

Fix the bug on the assumption that the master branch

#切换至master分支并进入bug分支修改
[root@git ll]# git checkout master 
[root@git ll]# git checkout -b bug
[root@git ll]# echo "eeee" >> branch.txt 
[root@git ll]# git add branch.txt
[root@git ll]# git commit -m "alter from bug"
#切换至master分支合并修改后的bug分支
[root@git ll]# git checkout master 
[root@git ll]# git merge bug 
[root@git ll]# cat branch.txt       #合并后的文件内容如下
aaaa
cccc
bbbb
eeee
[root@git ll]# git branch -d bug      #删除bug分支
#回到dev分支恢复之前修改的内容继续自己的工作
#有两种恢复方法:
#一是使用 git stash apply 恢复,但是恢复后,stash 内容并不删除,需要用 git stash drop 来删除;
#另一种方式是用 git stash pop,恢复的同时把 stash 内容也删了;这里我采用第二种方法
[root@git ll]# git stash pop        #恢复存储区的内容
[root@git ll]# cat branch.txt     #我们之前的内容又回来了
aaaa
cccc
bbbb
dddd
#最后工作完成,在合并dev分支的时候,也会有分支冲突,可以参考前面解决分支冲突的方法

6, Git branch management-related commands

[root@git ll]# git checkout -b ops    #创建ops分支并切换到ops分支
[root@git ll]# git checkout master     #切换至master分支
[root@git ll]# git merge dev     #快速合并dev分区到当前分支
[root@git ll]# git branch -d ui       #删除ui分支
[root@git ll]# git branch        #查看所在分支(用星号表示所在分支)
[root@git ll]# git log --graph --pretty=oneline --abbrev-commit 
#查看分支合并图
[root@git ll]# git merge --no-ff -m "合并时提交信息" dev   #不使用快速合并分支
[root@git ll]# git stash        #将当前版本库的状态临时存储
[root@git ll]# git stash pop   #恢复并删除临时存储的信息
[root@git ll]# git stash apply    #恢复临时存储信息,但不删除信息
[root@git ll]# git stash drop     #删除临时存储中的信息
[root@git ll]# git stash show     #查看临时存储的信息
[root@git ll]# git branch -D dev       #强制删除一个分支
[root@git ll]# git remote       #查看当前版本库是否属于远程版本库
[root@git ll]# git remote -v    #查看远程版本库的详细信息
[root@git ll]# git push origin dev     #将本地dev分支推送到远程仓库
[root@git ll]# git checkout -b dev origin/dev   #创建本地dev分支并关联到远程仓库的dev分支
[root@git ll]# git pull    #抓取远程分支,一般用于解决冲突
[root@git ll]# git branch --set-upstream-to=origin/dev dev   #将本地分支dev关联到远程仓库的dev分支

Guess you like

Origin blog.51cto.com/14154700/2450432