概述:
除了分支的创建的和修改,有时候我们需要涉及查询各个分支的状态,本章涉及的命令有
1、git branch (查询当前的工程的分支,*代表当前操作的分支)
2、git branch -v (查询各个分支最后提交的内容)
3、git branch --merged (查询合并的分支,注意这是两个短杠)
4、git branch --no-merged(查询未合并的分支)
5、git branch -d 分支名 ( 一般都需要合并之后才能删除,暴力就是加参数-D)
情景介绍:创建两个分支b2和b3,还有现有firstbranch分支,将在b2提交一个文件
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch
firstbranch
* master
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch -v
firstbranch ac67cd6 README
* master 5aae589 [ahead 15] hello
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch --merged
firstbranch
* masterAdministrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch --no-merged
总结:
1.查看当前分支有两个 firstbranch和 master ,而master是当前操作的分支
2.查看两个分支最后提交的内容 firstbranch 是README ,而 master 是hello
3.查看当前分支的合并情况,全部都已经合并
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git checkout -b b2
Switched to a new branch 'b2'
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git branch
* b2
firstbranch
master
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git checkout -b b3
Switched to a new branch 'b3'
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git branch
b2
* b3
firstbranch
master
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git branch -v
b2 5aae589 hello
* b3 5aae589 hello
firstbranch ac67cd6 README
master 5aae589 [ahead 15] hello
总结:上面分出两个分支 b2 和b3 、同时你会发现从master分来的分支最后提交的文件都是hello
在这之前我把firstbranch 删掉了
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git checkout b2
Switched to branch 'b2'
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ echo "heelooo" > hello.txt
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git commit -a -m "helllo"
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
[b2 bcb72e7] helllo
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git branch --no-merged
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git branch --merged
* b2
b3
master
总结:上面重要在b2新增一个文件hello.txt然后提交,查看是否有分支情况发现并没有没有合并的分支,原因的是它是以自身(b2)为考虑,b2现在已经是最新的,当然就是没有需要合并的分支
如果你切换到master,你看看
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 15 commits.
(use "git push" to publish your local commits)
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch --no-merged
b2
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git checkout b2
Switched to branch 'b2'
总结:这时候以master为标准,它发现有个b2走在他前面,当然算未合并的分支
分支的删除问题
Administrator@USER-20170424ZG MINGW64 ~/grit (b2)
$ git branch -d b3
Deleted branch b3 (was 5aae589).
总结:发现我并没有提交,它也可以删除,因为b3和master是一样的,(就像合并的之后的两个分支是一样的效果)
$ git checkout -b b3
Switched to a new branch 'b3'
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ echo "b3" > hello.txt
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git branch -d b3
error: Cannot delete branch 'b3' checked out at 'C:/Users/Administrator/grit'
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git branch -D b3
error: Cannot delete branch 'b3' checked out at 'C:/Users/Administrator/grit'
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git checkout master
error: Your local changes to the following files would be overwritten by checkot:
hello.txt
Please commit your changes or stash them before you switch branches.
Aborting
总结:修改的同一个地方hello.txt,发现都不能切换master,让我必须处理这个b3分支,这就是说明你已经影响主分支,而主分支必须要明确你修改是否有效,它不会为你做确定的,这是一种透明的方式,有时候忘记提交,发现恢复以前版本,多尴尬
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git commit -a -m "b3提交的"
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
[b3 935c40a] b3鎻愪氦鐨▒
1 file changed, 1 insertion(+), 1 deletion(-)
Administrator@USER-20170424ZG MINGW64 ~/grit (b3)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 15 commits.
(use "git push" to publish your local commits)
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch -d b3
error: The branch 'b3' is not fully merged.
If you are sure you want to delete it, run 'git branch -D b3'.
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ git branch -D b3
Deleted branch b3 (was 935c40a).
Administrator@USER-20170424ZG MINGW64 ~/grit (master)
$ cat hello.txt
hello
总结:提交之后就可以切换分支的了,-d不能直接删除, -D可以强制删除,删除后发现hello.txt没有变,因为b3提交的内容并没有合并到master的分支上,它提交到b3分支上,而b3分支被删除了
总结:明确提交的内容,不允许切换分支和删除分支,这就是git最大保护你文件。