count web page visits
Panasonic Viera 3D

git用法小结(2)--git分支

 中间隔了老长时间了啊!!呵呵

今天继续。

上次我们已经将远程仓库建立好了,下面我们主要的工作就是在这个远程仓库里使劲的捣鼓。

首先我们会想到的是--建立分支。你能确保自己的改动就是万无一失的吗?不能的话就来建立自己的分支,让自己的修改停留在自己的分支。不要你影响别人哦!!

1.新建分支

1 software@debian:~/zhoubb/git-test$ 
2 software@debian:~/zhoubb/git-test$ git branch
3 * master
4 software@debian:~/zhoubb/git-test$ git branch yafeng
5 software@debian:~/zhoubb/git-test$ git branch
6 * master
7   yafeng
8 software@debian:~/zhoubb/git-test$

 命令注释:

git branch ---查看当前仓库的分支,其中带有*的表明你当前所在的分支

git branch -v---查看各分支最后一个递交对象信息

git branch XXX---建立名为XXX的分支

有了分支之后,肯定是要在自己建立的分支下开展自己的工作。

2.切换分支

 1 software@debian:~/zhoubb/git-test$ git checkout yafeng
 2 Switched to branch 'yafeng'
 3 software@debian:~/zhoubb/git-test$ git branch
 4   master
 5 * yafeng
 6 software@debian:~/zhoubb/git-test$ git checkout -b test
 7 Switched to a new branch 'test'
 8 software@debian:~/zhoubb/git-test$ git branch
 9   master
10 * test
11   yafeng

命令注释:
git checkout XXX---切换至某一已有分支

当然,要是你在新建的时候就已经想要切换至你新建的分支,则可以将命令进行合并。

git checkout -b XXX---新建XXX分支,并切换至XXX分支

有来必有去,有存在必有消亡。当你不需要一个分支时,你当然要要不见为净了啊

3.删除分支

 1 software@debian:~/zhoubb/git-test$ git checkout yafeng
 2 Switched to branch 'yafeng'
 3 software@debian:~/zhoubb/git-test$ git branch -d test
 4 Deleted branch test (was c2241fd).
 5 software@debian:~/zhoubb/git-test$ git branch
 6   master
 7 * yafeng
 8 software@debian:~/zhoubb/git-test$ git status
 9 # On branch yafeng
10 nothing to commit (working directory clean)
11 software@debian:~/zhoubb/git-test$ 

 命令注释:

git branch -d XXX ---删除已存在的某个分支

git status---查看当前git状态。

当你在多个分支里面活动时,在工作接近尾声或者结束时,肯定会将其他分支的内容合并到master分支。

4.git分支的合并

先来看一下整个操作的效果是如何的。

我们假设你在yafeng分支下向文件添加了若干东西,而此时我们需要将这些修改反映到master分支,并将yafeng分支删掉。

为了看得清楚,我们进行左右的对比,左边的是master分支在未进行合并之前的内容,右边的是master分支在合并之后的内容(假设只在a.txt中有改变)。

 1 hello a                                    hello a
 2 welcome to a.txt                                                       welcome to a.txt
 3 you should know what you are doing                                  you should know what you are doing
 4 end                                                                 end
 5                                                                     Here is yafeng branch write!!

从上面我们看出在合并前后的差异。

下面来看看操作吧:

 1 software@debian:~/zhoubb/git-test$ git branch
 2 * master
 3   yafeng
 4 software@debian:~/zhoubb/git-test$ git merge yafeng
 5 Updating fe31868..a6464bc
 6 Fast-forward
 7  a.txt |    2 ++
 8  b.txt |    2 ++
 9  2 files changed, 4 insertions(+), 0 deletions(-)
10 software@debian:~/zhoubb/git-test$ 
11 software@debian:~/zhoubb/git-test$ git branch -d yafeng
12 Deleted branch yafeng (was a6464bc).
13 software@debian:~/zhoubb/git-test$ git branch
14 * master
15 software@debian:~/zhoubb/git-test$ 

命令注释:

git merge XXX---将XXX分支合并至你当前所在的分支。你在XXX分支所做的操作都会将反映到你所在的分支中。

以上是关于git分支的一些基本的用法,希望对大家的git学习能起到启发的作用哈!!!

posted @ 2012-09-08 15:49  夜半私时  阅读(1758)  评论(0编辑  收藏  举报