---
title: "使用github管理你的代码"
author: "anzhsoft"
date: "2014-01-04T22:31:00Z"
tags: []
slug: "github"
draft: false
lastmod: "2014-03-14T20:25:41Z"
id: 2031367
url: "https://anzhsoft.iteye.com/blog/2031367"
---

关于为什么使用github，网上已经有很多讨论了。当然选择还有google code， Bitbucket，sourceforge。github有如下优势：

1\. github更有利于开源项目的发展  
source forge并没有充分体现这一点，它更像一个开源软件下载站。至于Google Code，这是个传奇。但是已经被新CEO布林颁布的大扫除政策打死了，属于边缘化业务，Google不会投入新精力了，只是碍于原本有很多项目依旧运行在Google Code上，所以才没有像Google Reader一样彻底关闭。但是基本上活跃用户都迁走了。

在github上使用开源项目的流程是:  
a. 下载，可以直接下载或者git clone下来，可以下载当前版本也可以下载某个分支，或者某个tag，甚至是某个commit  
b. 使用(这个跟sf差不多了)  
c. 修改，直接fork一个就可以改了，改完可以给作者发个pull request，这样才能让开源项目不断的完善起来  
github 让这一切都变得简单，直接；不像很久以前的开源界，想要贡献点代码，你还得先进mail list，先帮着解答，时机成熟了再让你改点bug

2\. github更方便沟通  
任何人可以给项目创建issue， 写上特性需求或者报告bug，作者或项目成员会很快做出回应  
在 sf上，作者和项目成员都不知道在哪，只有在版本发布的时候会在上面更新一下

3\. github引入了社交元素  
github 上的用户是可以follow别人的，也可以watch某个项目  
这很重要，可以每天都多了解一点点，每天多进步一点点  
唯一不足的地方就是没有像twitter那样，有人follow会发一封email通知下，让你看看是不是要回fo

4\. github更开放  
github提供众多的api，可以跟多数管理服务整合  
网站上有大量的帮助文档，从 git的入门到github的使用都有，非常详细、图文并茂  
官方博客经常发布一些技术文章，内容涉及故障处理过程，性能优化，各种技术解决方案的选择思路  
此外，可以为项目创建静态网站，并且没有任何的限制，无广告  
这个功能被很多人用于托管博客，我的博客就是建在这上面，免费

5\. github还在快速发展  
从最初github发展时只具备基本的源代码托管功能，到速度的提升(迁至rackspace使国内用户访问飞快)，到gist的推出，到wiki的版本化，到pull request的出现。  
因为还有一大群扎根在sf，还不知道github或者还不了解 github有多优秀他们会觉得sourceforge已经非常好了，足够使用，直到有一天，他们真正被github的魅力所吸引所以，我们在很长的一段时间内都会看到，知名项目一个接一个地慢慢往github上移最终，sf的创始人会说：妈的，老子也移过去算了。以上内容转自http://www.cnblogs.com/draem0507/articles/2151162.html。知乎上也有很精彩的讨论：[GitHub、Bitbucket、Google Code 各有哪些优缺点？](http://www.zhihu.com/question/20053312)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

如何使用github？

首先去github.com注册账号。windows的客户端我用的是官方的：http://windows.github.com/。当然据说msysgit+TortoiseGit也不错。官方有个15分钟的training不错：http://try.github.com/。Refer to http://training.github.com/ for more trainings.

关于配置，因为git默认采用的https协议，每次的pull(get from git server)/push(upload the local copies to git server)都需要输入密码，因此可以使用ssh key的方式去认证。方法如下：打开Git Shell在其中输入如下命令：ssh-genkey -t rsa -C "email@address"。之后会让你选择是否对存放SSH Key的文件夹进行加密，选择默认值就可以了。在用户的目录下的有新生成的.ssh 文件夹，复制 id\_rsa.pub文件内容，设置到github账户中：登陆你的GitHub账户，依次点击*Account Settings* &gt;*SSH Public Keys* &gt; *Add another public key*，把id\_rsa.pub中的内容拷贝进去 。使用ssh -T git@github.com可以验证设置是否正确：

![](http://img.blog.csdn.net/20140104215307062?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvYW56aHNvZnQ=%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA%3D%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)

接下来要使用git shell 设置账号信息：

首先，git要求使用者必须提供自己的身份标识，为此我们需要在git bash中执行以下命令：

```
git config --global user.name  'anzhsoft'
git config --global user.email  anzhsoft@gmail.com
```

接下来可以将本地的project上传到githcd Project\_pathgit init ##在当前目录下生成一个隐藏目录（.git），这个目录就是git用来管理软件版本的仓库。

```
cd project_path
## 1 initialize the project 
git init
##it will generate the dir .git under the project_path, which is used to manage the current repository
## 2 connect the remote git server
git remote add origin git@github.com:anzhsoft/repository.git
## 3 add the items
git add .
## 4 commit
git commit -m "initial commit"
## 5 push to server
git push -u origin master
```

常见错误可以见 http://blog.csdn.net/dengjianqiang2011/article/details/9260435

如果你在create repository时添加了readme，license等文件，那么##5会出现一些错误：

```
To git@github.com:anzhsoft/Readings.git  
 ! [rejected]        master -> master (fetch first)  
error: failed to push some refs to 'git@github.com:anzhsoft/Readings.git'  
hint: Updates were rejected because the remote contains work that you do  
hint: not have locally. This is usually caused by another repository pushin  
hint: to the same ref. You may want to first merge the remote changes (e.g.  
hint: 'git pull') before pushing again.  
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 
```

直接force这个push就可以了：

```
git push -f origin master
```

最后广告贴： welcome to [https://github.com/anzhsoft](https://github.com/anzhsoft)