一,创建 git 账户
1,在用作服务器的机器 Server 上创建 git 账户。我们可以通过 System Preferences->accounts 来添加。在这里我添加一个 git 的 administrator 账户,administrator 不是必须的,在这里仅仅为了方便。


Mac搭载git_git服务器的搭建

2,设置远程访问
logout 当前账户,使用 git 账户登录;在 System Preferences->Sharing 中,勾选:Web Sharing 和 Remote Logig。

二,下载安装 gitosis
1,Mac Snow默认已经为我们安装了 Git 和 Python,可以使用如下命令查看其版本信息:

thingingdeMacBook-Air:~ git$  python --version
Python 2.7.6
thingingdeMacBook-Air:~ git$ git --version
git version 2.3.2 (Apple Git-55)


2,通过命令 "git clone git://eagain.net/gitosis.git" 来下载 gitosis

thingingdeMacBook-Air:~ git$ git clone git://eagain.net/gitosis.git
Cloning into gitosis
remote: Counting objects: 614, done.
remote: Compressing objects: 100% (183/183), done.
remote: Total 614 (delta 434), reused 594 (delta 422)
Receiving objects: 100% (614/614), 93.82 KiB | 45 KiB/s, done.
Resolving deltas: 100% (434/434), done.

 如果这个不行:
 Cloning into 'gitosis'...
 fatal: unable to connect to eagain.net:
 eagain.net[0: 208.78.102.120]: errno=Connection refused
 则可从如下位置clone gitosis包:
 git clone https://github.com/res0nat0r/gitosis.git
 Cloning into 'gitosis'...
remote: Counting objects: 727, done.
 remote: Compressing objects: 100% (216/216), done.
 remote: Total 727 (delta 510), reused 701 (delta 495)
 Receiving objects: 100% (727/727), 111.73 KiB | 44 KiB/s, done.
 Resolving deltas: 100% (510/510), done.


3,进入 gitosis 目录,使用命令 "sudo python setup.py install" 来执行 python 脚本来安装 gitosis。

thingingdeMacBook-Air:~ git$ cd gitosis/
thingingdeMacBook-Air:gitosis git$ sudo python setup.py install


三,制作 ssh rsa 公钥
1,回到 client 机器上,制作 ssh 公钥。在这里我的使用同一台机器上的另一个账户作为 client。如果作为 client 的机器与作为 server 的机器不是同一台,也是类型的流程:制作公钥,放置到服务的 /tmp 目录下。只不过在同一台机器上,我们可以通过开启另一个 terminal,使用 su 切换到 local 账户就可以同时操作两个账户。

thingingdeMacBook-Air:~ git$ su thinging

Password:
bash-3.2$ cd ~
bash-3.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/local_account/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/local_account/.ssh/id_rsa.
Your public key has been saved in /Users/local_account/.ssh/id_rsa.pub.

thingingdeMacBook-Air:git thinging$  cd .ssh
thingingdeMacBook-Air:git thinging$  ls

id_rsa        id_rsa.pub
thingingdeMacBook-Air:git thinging$  cp id_rsa.pub /tmp/yourame.pub


在上面的命令里,首先通过 su 切换到 local 账户(只有在同一台机器上才有效),然后进入到 local 账户的 home 目录,使用 ssh-keygen -t rsa 生成 id_rsa.pub,最后将该文件拷贝放置到  /tmp/yourname.pub,这样 git 账户就可以访问 yourname.pub了,在这里改名是为了便于在 git 中辨识多个 client。

四,使用 ssh 公钥初始化 gitosis
1,不论你是以那种方式(邮件,usb等等)拷贝 yourname.pub 至服务器的 /tmp/yourname.pub。下面的流程都是一样,登入服务器机器的 git 账户,进入先前提到 gitosis 目录,进行如下操作初始化
gitosis,初始化完成后,会在 git 的 home 下创建 repositories 目录。

thingingdeMacBook-Air:gitosis git$ sudo -H -u git gitosis-init < /tmp/yourname.pub
Initialized empty Git repository in /Users/git/repositories/gitosis-admin.git/
Reinitialized existing Git repository in /Users/git/repositories/gitosis-admin.git/

在这里,会将该 client 当做认证受信任的账户,因此在 git 的 home 目录下会有记录,文件 authorized_keys 的内容与 yourname.pub 差不多。


在这里,会将该 client 当做认证受信任的账户,因此在 git 的 home 目录下会有记录,文件 authorized_keys 的内容与 yourname.pub 差不多。
thingingdeMacBook-Air:~ git$ cd ~
thingingdeMacBook-Air:~ git$ cd .ssh
thingingdeMacBook-Air:.ssh git$ ls
authorized_keys

我们需要将 authorizd_keys 稍做修改,用文本编辑器打开它,删除里面的"command="gitosis-serve yourname",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty " 这一行:
yourname:.ssh git$ vi authorized_keys


然后,我们对 post-update 赋予可写权限,以便 client 端可以提交更改。
yourname:gitosis git$ sudo chmod 755 /Users/git/repositories//gitosis-admin.git/hooks/post-update
Password:
hingingdeMacBook-Air:~ git$ cd ~
thingingdeMacBook-Air:~ git$ cd repositories/
thingingdeMacBook-Air:~repositories git$ ls
gitosis-admin.git
thingingdeMacBook-Air:~repositories git$

在上面的命令中可以看到,gitosis 也是作为仓库的形式给出,我们可以在其他账户下 checkout,然后对 gitosis 进行配置管理等等,而无需使用服务器的 git 账户进行。

最后一步,修改 git 账户的 PATH 路径。
thingingdeMacBook-Air:gitosis git$ touch ~/.bashrc
thingingdeMacBook-Air:gitosis git$ echo PATH=/usr/local/bin:/usr/local/git/bin:\$PATH > .bashrc
thingingdeMacBook-Air:gitosis git$ echo export PATH >> .bashrc
thingingdeMacBook-Air:gitosis git$ cat .bashrc
PATH=/usr/local/bin:/usr/local/git/bin:$PATH
export PATH

至此,服务器的配置完成。

五,client 配置
1,回到 local 账户,首先在 terminal 输入如下命令修改 local 的 git 配置:

thingingdeMacBook-Air:git thinging$  git config --global user.name "yourgitname"
thingingdeMacBook-Air:git thinging$  git config --global user.email "yourmail@yourcom.com"


2,测试服务器是否连接正确,将 192.168.1.104 换成你服务的名称或服务器地址即可。
thingingdeMacBook-Air:git thinging$ ssh git@192.168.1.104

Last login: Mon Nov  7 13:11:38 2011 from 192.168.1.1043,然后exit退出,回到client

在本地 clone 服务器仓库,下面以 gitosis-admin.git 为例:
thingingdeMacBook-Air:git thinging$  git clone git@192.168.1.104:repositories/gitosis-admin /Users/thinging/git

Cloning into gitosis-admin
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 5 (delta 0)
Receiving objects: 100% (5/5), done.
bash-3.2$ ls
Desktop        InstallApp    Music        Sites
Documents    Library        Pictures    gitosis-admin
Downloads    Movies        Public
bash-3.2$ git

在上面的输出中可以看到,我们已经成功 clone 服务器的 gitosis-admin 仓库至本地了。

4,在本地管理 gitosis-admin:
进入 gitosis-admin 目录,我们来查看一下其目录结构:gitosis.conf 文件是一个配置文件,里面定义哪些用户可以访问哪些仓库,我们可以修改这个配置;keydir 是存放ssh 公钥的地方。
thingingdeMacBook-Air:git thinging$  cd gitosis-admin/
thingingdeMacBook-Air:git thinging$  ls
gitosis.conf keydir
thingingdeMacBook-Air:git thinging$  cd keydir/
thingingdeMacBook-Air:git thinging$  ls
yourname.pub

我们只需要将其他 client 产生的 ssh 公钥添加到 keydir 目录下,并在 gitosis.conf 文件中配置这些用户可以访问的仓库(用户名与放置在 keydir 下sh 公钥名相同,这就是在前面我们要修改ssh 公钥名的原因),然后将改动提交至服务器,这样就可以让其他的 client 端访问服务器的代码仓库了。


第六:往仓库添加代码

1.往仓库里面添加代码,在这里添加上去了一个jiankemall的代码

2.输入git status 查看本地变动

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/JKHelper.h
    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/JKStringHelper.h
    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/JKStringHelper.m
    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/NSString+JKHelper.h
    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/NSString+JKHelper.m
    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKSignHelper.h
    new file:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKSignHelper.m

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   1.2 new jiankemall/jiankemall.xcworkspace/xcshareddata/jiankemall.xccheckout
    modified:   1.2 new jiankemall/jiankemall.xcworkspace/xcuserdata/thinging.xcuserdatad/UserInterfaceState.xcuserstate
    modified:   1.2 new jiankemall/jiankemall/General/JKWebEngine/Helper/JKSignHelper.m
    modified:   1.2 new jiankemall/jiankemall/Sections/PersonalCenter/CheckLogisticsViewController.xib
    modified:   1.2jiankemall/.DS_Store
    modified:   1.2jiankemall/jiankemall.xcodeproj/project.pbxproj
    modified:   1.2jiankemall/jiankemall.xcworkspace/xcuserdata/thinging.xcuserdatad/UserInterfaceState.xcuserstate
    modified:   1.2jiankemall/jiankemall/.DS_Store
    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/JKStringHelper.h
    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/JKStringHelper.m
    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/NSString+JKHelper.h
    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKHelper/NSString+JKHelper.m
    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKSignHelper.h

    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKSignHelper.m
    modified:   1.2jiankemall/jiankemall/Genneral/JKWebEngine/JKWebEngine.m
    modified:   jiankemall/.DS_Store
    modified:   jiankemall/jiankemall.xcworkspace/xcshareddata/jiankemall.xccheckout


Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .DS_Store
    1.2jiankemall/jiankemall.xcworkspace/xcshareddata/
    jiankemall/Pods/Pods.xcodeproj/xcuserdata/thinging.xcuserdatad/
    jiankemall/jiankemall.xcodeproj/xcuserdata/thinging.xcuserdatad/
    jiankemall/jiankemall.xcworkspace/xcuserdata/thinging.xcuserdatad


Untracked files下面的文件就是还没有提交的文件。


3.sudo git add -A 或者sudo git add * //添加到暂缓区


4.提交代码到本地仓库

 sudo git commit -m'jiankemall'     //-m后面表示提交时候的批注。这个最好要越详细约好。以后出了错误可以回来看到是哪个开发者的错误,还有他的修改思路。


5, sudo git pull origin master

然后在提交给服务端之前先要,从服务端把仓库代码拉到本地做比较。有没有冲突(不同开发者修改了同一个文件称为冲突)

冲突解决参考一下链接

http://blog.csdn.net/u012150179/article/details/14047183


如果报如下错误:

fatal: Couldn't find remote ref master
fatal: The remote end hung up unexpectedly
因为还没有任何人提交过代码,第一次要先直接push代码上去就可以了。

6,sudo git push origin master

把最终代码放到仓库里边替换仓库里面的代码


第七:生产上面都是会开分支来完成代码的开发的,这里也顺便说一下分支的创建

1 查看远程分支

   $ git branch -a  

  1.   master  

  2.   remotes/origin/HEAD -> origin/master  

2 查看本地分支

    $git branch

  1.   master  


3 创建分支

 $ git branch test1

 $ git branch  

  1.   master  

  2.   test1


线面是把分支推到远程分支 

$ git push origin test1

//$ git push origin test1:master         // 提交本地test分支 作为 远程的master分支,这个和上面的操作是一样的
//$ git push origin test1:test1              // 提交本地test1分支作为远程的test1分支


4 切换分支到test

   $ git branch  

  1. * master  

  2.   test1

   $ git checkout test1

Switched to branch 'test'  

  1. $ git branch  

  2.   master  

  3. * test1


M 表示从 原来分支(上一次修改没有提交)带过来的修改

5 删除本地分支   git branch -d xxxxx

  1. $ git br -d test1

  2. Deleted branch test1(was 17d28d9).  


   $ git br  

  1.   master  


6 查看本地和远程分支  -a。前面带*号的代表你当前工作目录所处的分支

 remotes/origin/HEAD -> origin/master #啥意思呢?  

        ”在clone完成之后,Git 会自动为你将此远程仓库命名为origin(origin只相当于一个别名,运行git remote –v或者查看.git/config可以看到origin的含义),并下载其中所有的数据,建立一个指向它的master 分支的指针,我们用(远程仓库名)/(分支名) 这样的形式表示远程分支,所以origin/master指向的是一个remote branch(从那个branch我们clone数据到本地)“

       这个是执行 git remote -v 的结果,看出来origin其实就是远程的git地址的一个别名。

   $ git remote  -v  

  1. origin  git@gitlab.alibaba-inc.com:middleware/jingwei.git (fetch)  

  2. origin  git@gitlab.alibaba-inc.com:middleware/jingwei.git (push)  

7 删除远程版本

$git push origin :test1


第八:合并分支

git checkout master

git merge test1;


第九:版本冲突解决

如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候,

在发布这个配置文件的时候,会发生代码冲突:

error: Your local changes to the following files would be overwritten by merge:
        protected/config/main.php
Please, commit your changes or stash them before you can merge.

如果希望保留生产服务器上所做的改动,仅仅并入新配置项, 处理方法如下:

git stash
git pull
git stash pop

然后可以使用git diff -w +文件名 来确认代码自动合并的情况.


反过来,如果希望用代码库中的文件完全覆盖本地工作版本. 方法如下:

git reset --hard
git pull

其中git reset是针对版本,如果想针对文件回退本地修改,使用

git checkout HEAD file/to/restore 

(http://www.yiibai.com/git/git_handling_conflicts.html)另外一种情况


第十:更改分支名字

thingingdeMacBook-Air:gitcode thinging$ git branch
  master
* test1

现在想把test1 变为jkmall_1.2,做法如下

thingingdeMacBook-Air:gitcode thinging$ git push origin jkmall_1.2
Password:
Counting objects: 43, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (41/41), done.
Writing objects: 100% (43/43), 103.18 KiB | 0 bytes/s, done.
Total 43 (delta 27), reused 0 (delta 0)

To git@192.168.1.101:repositories/gitosis-admin
 * [new branch]      jkmall_1.2 -> jkmall_1.2
thingingdeMacBook-Air:gitcode thinging$
thingingdeMacBook-Air:gitcode thinging$ git branch
* jkmall_1.2
  master
thingingdeMacBook-Air:gitcode thinging$ git branch -a
* jkmall_1.2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/jkmall_1.2
  remotes/origin/master
  remotes/origin/test1
thingingdeMacBook-Air:gitcode thinging$ git push --delete origin test1
Password:
To git@192.168.1.101:repositories/gitosis-admin
 - [deleted]         test1
thingingdeMacBook-Air:gitcode thinging$ git branch -a
* jkmall_1.2
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/jkmall_1.2
  remotes/origin/master

另外一些有用的命令在http://zengrong.net/post/1746.htm