之前一直用的是旧版本的caffe,然后用svn管理,每次官方改了bug,都是手动加进去,很蛋疼….
最近一怒之下转到git上了,那么问题来了,原来的版本我改了很多,怎么合并?以前没接触git,最多就是从github上clone项目下来,对merge啊,branch啊什么的完全没概念。折腾了一晚上终于搞掂了,期间大部分时间是花在手工修改代码上,下面讲讲我的经历。
将我的代码跟最新的caffe代码合并
首先,先clone一份最新的caffe代码:
1
git clone https://github.com/BVLC/caffe.git
然后新建一个空白分支,用来放我的代码
1
2
git checkout --orphan zhujin
rm -r *
然后把我的代码全拷贝进来,并做一次commit
1
2
git add *
git commit -m 'add my codes'
接着切换到master上,并进行合并:
1
2
git checkout master
git merge master zhujin
这时候一般是提示会有冲突,没办法,使用下面命令开始手动清理:
1
git mergetool
清理完毕后,再看一下使用git status再看一下有没有冲突,如果没有,commit一次就完成了合并了。
通常弄完之后我会删除刚才新建的分支:
1
git branch-d zhujin
创建本地仓库
想必大家都不想这么快把自己的代码公开吧,毕竟有时候需要保密,这时候自己搭建一个git服务器是最安全而且方便的。那么先把现有仓库导出为裸仓库,
1
git clone --bare caffe caffe.git
这样会生成一个叫caffe.git的文件夹,然后把它拷贝到你的服务器上,或者说本地的某个目录,这个文件夹就是仓库了,下次直接向这里进行clone/pull/push。我把这个文件夹放到了我的网盘上,直接clone就好:
1
git clone /home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git
合并两个不同repository的分支
现在遇到的问题是,因为我搭建了服务器,不能直接使用git pull从官方的repository更新了,这样很坑爹啊,如果官方修复了一些bug,那我还得手动的一个一个改回来。
然而,git还是很强大的,能够合并不同地址的两个分支。
首先,我本地服务器的git地址是/home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git,先进行clone
1
git clone /home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git
这时候可以使用命令git remote -v查看你当前跟踪的repository有哪些,在我这里显示的只有我本地服务器的分支
1
2
origin /home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git (fetch)
origin /home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git (push)
此外,还可以用命令git branch -av看看目前的branch,显示:
1
2
3
* master 8853960 fix bug, all test case pass except hdf5
remotes/origin/HEAD -> origin/master
remotes/origin/master 8853960 fix bug, all test case pass except hdf5
然后把caffe官方的repository也加进来,注意的是,BVLC是自己取的名字,用来区分不同的repository。
1
git remote add BVLC https://github.com/BVLC/caffe.git
这时候使用命令git remote -v就发现你跟踪的repository多了。我这里显示的是:
1
2
3
4
BVLC https://github.com/BVLC/caffe.git (fetch)
BVLC https://github.com/BVLC/caffe.git (push)
origin /home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git (fetch)
origin /home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git (push)
这时候需要使用命令git fetch BVLC从官网把最新的branch下载下来,如果有下面提示表示成功下载了。
1
2
3
4
5
6
7
From https://github.com/BVLC/caffe
* [new branch] dev -> BVLC/dev
* [new branch] device-abstraction -> BVLC/device-abstraction
* [new branch] gh-pages -> BVLC/gh-pages
* [new branch] master -> BVLC/master
* [new branch] parallel -> BVLC/parallel
* [new branch] tutorial -> BVLC/tutorial
这时候再看一下分支,git branch -av:
1
2
3
4
5
6
7
8
9
10
* master 8853960 fix bug, all test case pass except hdf5
remotes/BVLC/dev 9b0662f Merge pull request #1461 from ixartz/fix_convert_mnist_siamese_data
remotes/BVLC/device-abstraction d52d669 Fix post-rebase errors, including: -device-abstracted version of MVN -new memset/memcpy wrappers (set_void and copy_void) -fixing MKL switching logic
remotes/BVLC/gh-pages b7c55d7 ae4a5b1 Merge pull request #2505 from ronghanghu/matcaffe3
remotes/BVLC/master 72d7089 [bug] fix double instantiation of GPU methods in LogLayer
remotes/BVLC/parallel aa3b877 Distributed training
remotes/BVLC/tutorial e99d108 add tutorial link
remotes/origin/HEAD -> origin/master
remotes/origin/master 8853960 fix bug, all test case pass except hdf5
`
然后切换到本地的分支,并进行合并:
1
2
git checkout master
git merge BVLC/master
当然,合并前可以看看你的分支跟将要合并的分支的区别:
1
git diff master BVLC/master
merge完之后,还要更新到本地服务器:git push,OK,All is done.
更新代码
好了,如果遇到caffe更新了代码怎么办?很简单,切换到BVLC/master分支,更新一下,然后切回master进行合并即可。
1
2
3
4
git checkout BVLC/master
git pull
git checkout master
git merge
Update
搭建git服务器
搭建git服务器也很简单,git是用ssh来访问的,用的是22端口,只要支持ssh访问就可以直接当做git服务器来用。那么得先安装OpenSSH服务:
1
yum install openssh-server
这时候其实也不需要做什么了,git服务器已经可以用了…clone的地址变成:
1
git clone liangzhujin@127.0.0.1:/home/liangzhujin/wangpan/kuaipan/repos/gitrepos/caffe.git
上面,liangzhujin是用户名(这里的用户名是ssh访问时候用的用户名),127.0.0.1是你git服务器的地址。
对了,如果你的仓库是放在家目录下的,那地址可以更简单,这里假设caffe.git就在/home/liangzhujin下:
1
git clone liangzhujin@127.0.0.1:caffe.git
假设你需要用别的账号来使用git服务,而且这些账号是不能登陆shell的,比如我想新建一个用户git,这个账号只能用来使用git的服务器,而不能ssh登陆,那么方法也很简单:
新建用户
1
useradd git
禁用shell登陆
修改/etc/passwd文件:
1
2
3
git:x:118:126::/home/git:/bin/bash
改成
git:x:118:126::/home/git:/usr/bin/git-shell
修改仓库的拥有者
假设仓库是caffe.git,需要把它的拥有者跟组设为git:
1
chown -R git:git caffe.git
然后把caffe.git放到/home/git/下。
进行clone
这时候,就可以进行clone了:
1
git clone git@127.0.0.1:caffe.git
另外,如果想建立严格的权限控制,就要用到Gitolite
Reference