[博主公告]

“linux大棚”是一个以Linux技术专题为主的博客。
本博客为了保证读者的浏览体验,决定不刊登任何广告信息。

专题

文章发布时间日历

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  
    <<返回主页

  • 14Oct
    %e3%80%8a%e7%9c%8b%e6%97%a5%e8%ae%b0%e5%ad%a6git%e3%80%8b%e4%b9%8b%e4%ba%8c%e5%8d%81%e4%b8%89

    本次讲解“clone和pull的艺术”。大家阅读完本文,就可以掌握如何和其他的合作开发者一起协同开发了!

    1 以rocrocket用户来建立一个git仓库,在master主分支里建立roc.c文件,然后建立wukong分支,在其中改进roc.c文件。保证wukong分支为commit状态。然后再改进master分支的代码以使master分支的roc.c为未提交状态。

    2 登录到bob用户,利用clone来获取rocrocket的信息

    3 以bob为登录用户来改进rocrocket的master的代码,然后切换到rocrocket来pull bob修改的代码。

    开始:

    1 以rocrocket用户来建立一个git仓库,在master主分支里建立roc.c文件,然后建立wukong分支,在其中改进roc.c文件。保证wukong分支为commit状态。然后再改进master分支的代码以使master分支的roc.c为未提交状态。

    [rocrocket@wupengchong git23]$ ls
    roc.c
    [rocrocket@wupengchong git23]$ git init
    Initialized empty Git repository in .git/
    [rocrocket@wupengchong git23]$ git commit -a
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use “git add <file>…” to include in what will be committed)
    #
    #       roc.c
    nothing added to commit but untracked files present (use “git add” to track)
    [rocrocket@wupengchong git23]$ git add .
    [rocrocket@wupengchong git23]$ git commit
    Created initial commit d3d0679: rocrocket:master:001
    1 files changed, 6 insertions(+), 0 deletions(-)
    create mode 100644 roc.c
    [rocrocket@wupengchong git23]$
    从这个初始化过程可以看出,git commit -a命令只可以用在已经建立了index file之后,也就是在第一次初始化时,必须要使用git add命令来建立index file,否则会报错。

    然后建立wukong分支:

    [rocrocket@wupengchong git23]$ git branch wukong
    [rocrocket@wupengchong git23]$ git checkout wukong
    Switched to branch “wukong”
    [rocrocket@wupengchong git23]$ vi roc.c
    [rocrocket@wupengchong git23]$ git commit -a
    Created commit aad38ac: rocrocket:wukong:001
    1 files changed, 1 insertions(+), 0 deletions(-)
    [rocrocket@wupengchong git23]$ cat -n roc.c
    1  #include<stdio.h>
    2  int main()
    3  {
    4          printf(“Please guess who he is.\n”);
    5          printf(“He is born in beijing.\n”);
    6          return 0;
    7  }
    [rocrocket@wupengchong git23]$

    黑体是在master的基础上新加入的一行代码。好了,wukong分支已经处于commit状态了。现在回到master,再改进下代码,并使得master处于未commit状态:

    [rocrocket@wupengchong git23]$ git checkout master
    Switched to branch “master”
    [rocrocket@wupengchong git23]$ vi roc.c
    [rocrocket@wupengchong git23]$ cat -n roc.c
    1    #include<stdio.h>
    2    int main()
    3    {
    4        printf(“Please guess who he is.\n”);
    5        printf(“He is crazy about linux.\n”);
    6        return 0;
    7    }
    [rocrocket@wupengchong git23]$ pwd
    /rocrocket/PSB/home/git23

    在master分支中加入了一句He is crazy about linux. 并且我故意没有git commit.

    2 登录到bob用户,利用clone来获取rocrocket的信息。

    [bob@wupengchong ~]$ whoami
    bob
    [bob@wupengchong ~]$ git clone /rocrocket/PSB/home/git23 bob23
    Initialized empty Git repository in /rocrocket/PSB/bob/bob23/.git/
    [bob@wupengchong ~]$ ls
    bob23
    [bob@wupengchong ~]$ cd bob23/
    [bob@wupengchong bob23]$ cat -n roc.c
    1  #include<stdio.h>
    2  int main()
    3  {
    4          printf(“Please guess who he is.\n”);
    5          return 0;
    6  }
    [bob@wupengchong bob23]$ git branch
    * master

    我们成功的在bob用户下将rocrocket的代码clone过来了,并放到了bob自己定义的bob23目录下。首先可以看到,我们clone到的是rocrocket的master分支的已commit的代码;而且可以看到当前的分支只有一个master主分支。而在rocrocket里的wukong分支并没有被clone过来。恩,不要沮丧和奇怪。git是这样设计的:clone的话,是clone远端的当前分支。通俗的说,远端当前处在哪个分支,你clone来的就是哪个分支。这下,你该知道如何clone到rocrocket的wukong分支了吧,对!就是让rocrocket也chekcout到wukong分支,然后你再clone就OK了!这个时候你到bob23目录下再git branch会得到只有wukong一个分支。对滴,你要明确一点,不是任何git仓库都有master分支的哦~

    By the way, 在执行git checkout branchname时,是必须保证当前本分支处于commit状态,否则git会提示:

    [rocrocket@wupengchong git23]$ git checkout wukong
    error: Entry ‘roc.c’ not uptodate. Cannot merge.

    我们这个时候切换到rocrocket,将未commit的代码commit:

    [rocrocket@wupengchong git23]$ git branch
    * master
    wukong
    [rocrocket@wupengchong git23]$ git commit -a
    Created commit fadfdb4: rocrocket:master:002
    1 files changed, 1 insertions(+), 0 deletions(-)

    3 以bob为登录用户来改进rocrocket的master的代码,然后切换到rocrocket来pull bob修改的代码。

    [bob@wupengchong bob23]$ ls
    roc.c
    [bob@wupengchong bob23]$ git branch
    * master
    [bob@wupengchong bob23]$ cat roc.c
    #include<stdio.h>
    int main()
    {
    printf(“Please guess who he is.\n”);
    return 0;
    }
    [bob@wupengchong bob23]$ vi roc.c
    [bob@wupengchong bob23]$ cat -n roc.c
    1    #include<stdio.h>
    2    int main()
    3    {
    4        printf(“Please guess who he is.\n”);
    5        printf(“His name is roc.\n”);
    6        return 0;
    7    }
    [bob@wupengchong bob23]$ git commit -a
    Created commit 7c1cd89: bob:master:001
    1 files changed, 1 insertions(+), 0 deletions(-)

    改进完毕并成功提交了。下面的任务就是转回到rocrocket来试着pull了!(pull就是取回代码的命令)

    [rocrocket@wupengchong git23]$ cat roc.c
    #include<stdio.h>
    int main()
    {
    printf(“Please guess who he is.\n”);
    printf(“He is crazy about linux.\n”);
    return 0;
    }
    [rocrocket@wupengchong git23]$ git pull /rocrocket/PSB/bob/bob23
    Unpacking objects: 100% (3/3), done.
    remote: Counting objects: 5, done.
    remote: Compressing objects: 100% (2/2)remote: , done.
    remote: Total 3 (delta 0), reused 0 (delta 0)
    Auto-merged roc.c
    CONFLICT (content): Merge conflict in roc.c
    Automatic merge failed; fix conflicts and then commit the result.
    [rocrocket@wupengchong git23]$

    可以看出rocrocket的代码和bob的代码是有冲突的,所以在我git pull远端仓库时,提示我conflict。

    这时,需要我自己解决冲突了。

    [rocrocket@wupengchong git23]$ vi roc.c
    [rocrocket@wupengchong git23]$ cat -n roc.c
    1    #include<stdio.h>
    2    int main()
    3    {
    4        printf(“Please guess who he is.\n”);
    5        printf(“He is crazy about linux.\n”);
    6        printf(“His name is roc.\n”);
    7        return 0;
    8    }
    [rocrocket@wupengchong git23]$
    好,解决完冲突了,下面来commit吧!

    [rocrocket@wupengchong git23]$ git commit -a
    Created commit ed92cd2: new pull!
    好了 一切都安静了,我们已经成功把bob的工作合并到rocrocket的工作之中了。

    下次将重点讲解如何更好的获取别人的代码。

    ===
    如果你对git感兴趣,请继续阅读:

    《看日记学git》之二十四

    《看日记学git》之二十五

    《看日记学git》之二十六

    over~

    我猜您对这些文章感兴趣:

    Posted by rocrocket @ 9:59 am

    Tags: , , ,

    2,390人阅读过了这篇文章。

    如果您还满意我的文章,请您订阅我的博客。点击“我要订阅”即可。谢谢:)

  • <<返回主页

2 Responses

WP_Cloudy

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.