您的位置:首页 > 编程语言

使用git和github 进行代码的版本控制

2012-09-06 16:41 686 查看
1. 为什么要使用

可能在国内,有很大一批程序员还有团队都喜爱用subversion。 其实我也一直在使用, 但是随着项目到了后期, 需要不断对分支进行工作 。 而subversion扯分支与合并分支显得过于繁琐。 相反git的优势就在于方便的扯分支与合并分支等操作。 git同时采用了本地化的版本控制,这意味着不需要远程服务器也可以进行代码的 commit和add等操作。github 提供了一个免费的 git remote 平台。 github.com

2. git的工作原理

git 和 subversion 最大的不同就是 仓库管理是本地化的 不像subversion所有版本库的管理都是通过一个中央服务svnserve来完成. 这意味着 我们不需要有远程的服务器就可以在本地进行 git commit/add/branch 等操作。 而远程服务 (github) 扮演的角色只是当前版本库的一个远程存储。 通过github 来共享你的代码,实现团队的合作。

3. git & github 的安装和注册

(a) github 的注册
https://github.com/signup/free
(b) git 的下载安装

如果是ubuntu 用户应该可以很方便地安装 直接运行 apt-get install git 就可以了

如果是CentOS 用户可以按照以下步骤进行git的安装

1. yum install gettext-devel expat-devel curl-devel zlib-devel openssl-devel (安装必要的库)

2. wget http://kernel.org/pub/software/scm/git/git-1.7.0.1.tar.gz
3. tar xvfz git-1.7.0.1.tar.gz

4. cd git-1.7.0.1

make prefix=/usr/local all

make prefix=/usr/local install

(c) ssh key 的生成
http://help.github.com/msysgit-key-setup/
可能在进行git remote 操作的时候 需要进行加密通信, 所以就有了这一步去生成key 的操作吧( 对于加密方面我也不是太懂 )

ssh-keygen -t rsa -C “tekkub@gmail.com”

粘贴pub-key 到 https://github.com/account
4. git 命令简介

(a) git clone

举例 git clone git://github.com/schacon/grit.git

从远程 克隆一个

(b) git status

查看当前版本库的状态

(c) git add

添加到版本库

例如:git add folder

(d) .gitignore

添加 .gitignore 文件到版本库根目录下 然后编辑文件可以 设置需要被忽视的文件和文件夹 举例如下

# a comment – this is ignored

*.a # no .a files

!lib.a # but do track lib.a, even though you’re ignoring .a files above

/TODO # only ignore the root TODO file, not subdir/TODO

build/ # ignore all files in the build/ directory

doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt

(e) git commit

进行一次提交

git commit -m “first commit”

(f) git pull [remote-name]

从远程 获取版本库 默认的 remote-name 都是 origin

(f) git push [remote-name] [branch]

将本地push 到远程 可以选择push 的分支是哪个分支
http://progit.org/book/ch3-1.html
英文好的朋友可以认真读一下这一节 这里详细介绍了 git 分支工作原理

(g) git branch [branch-name]

创建一个分支

(h) git checkout [branch-name]

将当前工作环境 移到指定分支上

(i) git mergy [branch-name]

把指定分支合并到当前分支上来

5. (推荐)BASH脚本

1. ship

#!/bin/sh -x

# Git workflow ship script from: http://reinh.com/blog/2008/08/27/hack-and-and-ship.html
# git name-rev is fail

ref=$(git symbolic-ref HEAD 2> /dev/null) || exit 0

CURRENT=”${ref#refs/heads/}”

git checkout master

git merge ${CURRENT}

git push origin master

git checkout ${CURRENT}

把代码保存成 vim ship 并且 chmod +x ship

这段shell 的意思就是 自动切换到主线 并且把分支合并到主线 并且push 到远程上 再切换到分支状态。 方便地用户分支提交代码

2.hack

#!/bin/sh -x

# hack: Merge the latest changes from the master branch into your current branch

ref=$(git symbolic-ref HEAD 2> /dev/null) || exit 0

CURRENT=”${ref#refs/heads/}”

git checkout master

git pull origin master

git checkout ${CURRENT}

git rebase master

这段shell 的意思就是 自动切换到主线 并且从远程更新到最新状态 然后切换到分支 把主线合并到分支上 方便用户及时把当前工作分支更新到最新状态

6. 参考文献
http://progit.org/book/ http://www.how-to-linux.com/2009/01/install-git-161-on-centos-52/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: