回到顶部


git分支管理命令

查看分支: git branch
创建分支: git branch <name>
切换分支: git checkout <name>
创建+切换分支 git checkout -b <name>
合并某个分支到当前分支:git merge <name>
删除分支: git branch -d <name> 
 创建dev分支
然后切换到dev分支
$git checkout -b dev
​
git checkout 命令加上 -b 参数表示创建并切换
相当于:
$ git branch dev
$ git checkout dev

查看分支

ssgao@ssgao-PC MINGW64 ~/git/hzzhgl (dev)
$ git branch
* dev
  master
git branch命令会列出所有分支,当前分支前面会标一个*号。

查看远程分支

查看本地和远程分支  -a。前面带*号的代表你当前工作目录所处的分支
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/kafka_changed
  remotes/origin/master
  remotes/origin/v1.0
  remotes/origin/v1.1
  remotes/origin/v1.2.0_dubbo_refactor_from_adcm

 

合并分支

git merge 命令用于合并指定分支到当前分支
将dev分支合并到master分支
​
$ git merge dev
Updating d17efd8..fec145a
Fast-forward
 readme.txt |    1 +
 1 file changed, 1 insertion(+)

删除分支

git branch -d <name> 
如果要丢弃一个没有被合并过的分支,可以通过git branch -D <name>强行删除。

分支合并时的冲突解决

准备新的dev 分支,继续我们的开发
$git checkout -b dev
switch to a new branch 'dev'
​
在分支dev上添加修改文件readme.txt ,并在dev上进行提交
$ git add readme.txt 
$ git commit -m "AND simple"
[feature1 75a857c] AND simple
 1 file changed, 1 insertion(+), 1 deletion(-)
切换到master分支
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
-- git 还可以自动提示我们,当前master分支比远程的master分支要超前提交一个分支--
在master分支修改readme.txt,并在mater上进行提交
$ git add readme.txt 
$ git commit -m "& simple"
[master 400b400] & simple
 1 file changed, 1 insertion(+), 1 deletion(-)
这是将dev分支合并到master分支
$ git merge dev
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
这时就发生了冲突,git会提示我们文件readme.txt发生了冲突,必须要我们手动解决后在提交
$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#       both modified:      readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
使用vi编辑器打开该冲突文件
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
​
Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存:
Creating a new branch is quick & simple.
手动修改冲突后,重新加入到缓存区
$ git add readme.txt 
$ git commit -m "conflict fixed"
[master 59bc1cb] conflict fixed
查看分支合并情况
$ git log --graph --pretty=oneline --abbrev-commit
*   59bc1cb conflict fixed
|\
| * 75a857c AND simple
* | 400b400 & simple
|/
* fec145a branch test
...
posted on 2018-04-19 08:55  ssgao  阅读(119)  评论(0编辑  收藏  举报