原文链接:http://blog.lmlphp.com/archives/7 来自LMLPHP后院
maybe yes 发表于2014-10-25 12:12
GITHUB是全球最出名的基于GIT的代码托管平台之一,可以免费的托管开源代码。作为一名软件工程师,对代码的管理养成一个良好的习惯是非常重要的。本人将讲解如何使用GIT BASH管理GITHUB中的代码库。
Git 是基于 Linux内核开发的版本控制工具。与常用的版本控制工具 CVS,Subversion等不同,它采用了分布式版本库的方式,不必服务器端软件支持,使源代码的发布和交流极其方便。 Git的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git最为出色的是它的分支、合并、跟踪的能力。
申请GITHUB账号
关于GITHUB账号如何申请,本文将不再描述。但是需要注意的一点是,国外的网站对密码的要求都非常的高,密码设置的不够复杂可能导致无法注册提交。
安装GIT客户端
GITHUB 官网有上提供GITHUB for Windows应用程序,关于GITHUB for Windows的使用非常的方便,图形界面同时也提供了GIT Bash。要求必须是Windows7及以上的操作系统,如果您使用的XP的系统,就只能安装其他GIT客户端了。
配置SSH KEYS
要使用SSH协议连接GITHUB,首先需要生成SSH KEYS。生成的密钥是两个文件,一个公钥一个私钥。公钥需要提交给GITHUB官网您的账号中。关于如何生成SSH Keys,请看如下步骤示例:
检查SSH keys
$ ls -al ~/.ssh # Lists the files in your .ssh directory, if they exist
生成 keys
$ ssh-keygen -t rsa -C "" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. # Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]
然后按照提示输入密码,最后出现如下提示则说明生成成功。
Your identification has been saved in /c/Users/you/.ssh/id_rsa. # Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub. # The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]
将生成的新秘钥加入SSH客户端
# start the ssh-agent in the background $ ssh-agent -s # Agent pid 59566 $ ssh-add ~/.ssh/id_rsa
复制公钥
复制公钥可以打开文件复制,也可以使用命令,建议使用命令复制,否则可能出现无法授权的问题。如下命令:
$ clip < ~/.ssh/id_rsa.pub # Copies the contents of the id_rsa.pub file to your clipboard
将公钥加入到GITHUB账号
登录GITHUB在Settings->SSH keys菜单下添加,将剪切板的内容粘贴到Key文本框中,名称可以随意填写。到现在为止,配置工作已经完成。
GIT BASH命令详解
git init
初始化GIT,只有初始化了以后才可以使用GIT相关命令。在初始化之前,可以先创建一个文件夹。如下示例
$ mkdir lmlphp $ cd lmlphp $ git init
git clone
获取远程项目,并下载到本地。远程库的地址在GITHUB项目中会有提供。下面是我测试时显示的内容,若执行成功,则将显示下面类似的内容。
C:\Users\May\Documents\GitHub\test> git clone [email protected]:leiminglin/LMLPHP.g it Cloning into 'LMLPHP'... Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of know n hosts. remote: Counting objects: 210, done. remote: Total 210 (delta 0), reused 0 (delta 0) Receiving objects: 100% (210/210), 66.48 KiB | 15.00 KiB/s, done. Resolving deltas: 100% (102/102), done. Checking connectivity... done. C:\Users\May\Documents\GitHub\test>
git branch
git branch命令用于创建分支,查看分支。查看分支可以使用参数-a,-v,-r等,a代表所有,v代表版本信息,r代码显示远程分支。下面的例子使用“git branch develop”创建了一个新的分支。
C:\Users\May\Documents\GitHub\test> cd .\LMLPHP C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch -av * master 405960a session_write_close() when fatal error occured remotes/origin/HEAD -> origin/master remotes/origin/develop 405960a session_write_close() when fatal error occured remotes/origin/master 405960a session_write_close() when fatal error occured C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch develop C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch -av develop 405960a session_write_close() when fatal error occured * master 405960a session_write_close() when fatal error occured remotes/origin/HEAD -> origin/master remotes/origin/develop 405960a session_write_close() when fatal error occured remotes/origin/master 405960a session_write_close() when fatal error occured C:\Users\May\Documents\GitHub\test\LMLPHP [master]>
git checkout
git checkout命令用于创建分支和切换分支。*号代表当前分支,下面通过checkout命令切换到develop分支。"checkout"在英文中 的意思是检出,但是也不难理解,GIT中分支其实就是一个指向,速度很快;现在我的本地有两个分支,但是只有一份代码,当使用checkout切换分支并 且两个分支的内容不同时,你会发现磁盘上的文件内容会发生变化。checkout 命令还可以用来创建分支并切换到这个分支,使用checkout -b 参数即可,下面的例子使用此命令创建了newFeature分支并切换到了这个分支。
C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git checkout develop Switched to branch 'develop' C:\Users\May\Documents\GitHub\test\LMLPHP [develop]> git branch -av * develop 405960a session_write_close() when fatal error occured master 405960a session_write_close() when fatal error occured remotes/origin/HEAD -> origin/master remotes/origin/develop 405960a session_write_close() when fatal error occured remotes/origin/master 405960a session_write_close() when fatal error occured C:\Users\May\Documents\GitHub\test\LMLPHP [develop]> git checkout -b newFeature Switched to a new branch 'newFeature' C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git branch -av develop 405960a session_write_close() when fatal error occured master 405960a session_write_close() when fatal error occured * newFeature 405960a session_write_close() when fatal error occured remotes/origin/HEAD -> origin/master remotes/origin/develop 405960a session_write_close() when fatal error occured remotes/origin/master 405960a session_write_close() when fatal error occured C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]>
git status
git status命令用来查看当前分支状态。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git status On branch newFeature nothing to commit, working directory clean
git pull
git pull命令用来更新代码,该命令相当于git fetch和git merge的组合。需要注意的是,如果来源是远程分支develop,则必须这样写“origin develop”,origin后面有个空格。如果远程分支存在有和当前分支一样的名字,则可以不指定分支。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git pull Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know n hosts. There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details git pull If you wish to set tracking information for this branch you can do so with: git branch --set-upstream-to=origin/ newFeature C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git pull origin develop Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know n hosts. From github.com:leiminglin/LMLPHP * branch develop -> FETCH_HEAD Already up-to-date. C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]>
git commit
git commit命令用来提交更新。更新时需要提交注释,使用-m 参数。GITHUB提供的GIT客户端做的非常好,如果您忘记添加注释,它会弹出文本框让你填写。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git commit -m "test" On branch newFeature nothing to commit, working directory clean
git push
git push命令用来推送更新到远程库,此命令一般在commit命令之后使用。如果远程没有对应的分支名,则需要通过设置参数“--set-upstream”指定提交到哪个分支。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git push fatal: The current branch newFeature has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin newFeature C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git push --set-upstream origin develop Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know n hosts. Branch develop set up to track remote branch develop from origin. Everything up-to-date
GIT BASH的功能非常强大,使用也非常方便,特别适合大型项目多人同时开发。图形界面工具虽然方便简单,但是效率远远不能跟命令行相比。本文简单的介绍了 GIT BASH的使用,更多的功能请使用--help命令查看,系统会使用浏览器打开HTML版本文档,描述的非常详细。