1 认识Git,并安装Git
1.1 认识Git
Git就是一个分布式的源码版本控制系统。关于集中式与者分布式版本控制系统的区别看下面两图就知道个差不多了。
图来源:http://git-scm.com/book/en/Getting-Started-About-Version-Control1.2 安装Git
Linux下安装XX install git-core(XX根据系统而定,Fedora下XX就是yum,Ubuntu下XX是apt-get) Windows下就是傻瓜式点击安装http://windows.github.com/。
1.3 Git配置
git配置就是指明自己的身份,以便可以跟踪具体是哪个用户对源码做了哪些修改。 使用git config 命令来完成git配置,配置时可以使用参数—global或者--system来指定配置信息的有效范围。--global表示配置信息只对当前用户有效,对其它用户无效。--system表示这太电脑的所有用户都使用此配置。 git config –global user.name “your name” git config –global user.email “your email”
2 创建代码仓库
以创建hello仓库为例:
2.1 建立空仓库
mkdir hello cd ./hello git init //初始化为一个空仓库,建立必要的git文件
2.2 填充仓库
在hello下写个hello.c程序后保存 git add hello.c //将文件hello.c添加到仓库,这里也可以使用git add .将目录下的所有文件都添加到仓库中去 git commit -m “first commit:hello.c is added” //将hello.c提交给仓库,注意这里的提交只会包含刚刚git add的那些文件,对于其它文件,即使它在这个代码仓库里面,也不属于代码控制的一部分
2.3 将仓库中的代码提交给远端
在GitHub上新建一个网络仓库,命名为hello,仓库地址为:https://github.com/username/hello git remote add origin https://github.com/username/hello //添加一个远端仓库,命名为origin, 注意命令格式:git remote add name address
git push origin master //将当前仓库的主分支代码推送到origin远端,注意:因为没有额外建立代码分支,所以当前仓库只有一个主分支master;默认情况下,直接使用git push也可以实现推送
/* 合并上面两个命令,git push https://github.com/username/hello master也可以实现推送 命令格式:git push 目的地 要推送的代码分支 */
3 克隆仓库
mkdir hello2 //在hello文件同目录下,新建hello2文件夹 git clone https://github.com/username/hello //将网络仓库的源码复制到本地,此时hello2是也是一个源码仓库,这个时候,hello2仓库默认已经有了一个叫做origin的远端仓库,读者可以使用git remote查看 git pull origin master //可以在后期阶段将远端仓库的更新代码同步到hello2
4 嵌套仓库(仓库下的子模块仓库)
mkdir hello_and_other //新建一个文件夹,在这个文件下将有多个子仓库 git init //仓库初始化 git submodule add https://github.com/username/hello hello //hello文件夹下就是一个子仓库 git submodule add https://github.com/kennyledet/Algorithm-Implementations.git algorithm //algorithm文件夹下是另外一个子仓库 git submodule init && git submodule update //进行子模块的初始化与更新