github初探(一)

  周末在家闲着没事,开始倒腾github,很早就知道github开源社区了,但是真正开始折腾还是昨天开始的。

  github是一个著名的开源社区,程序员们可以把自己的项目放到github上共享,同时github也提供了分布式版本控制的功能,很多IDE不是集成了git就是提供了有关git的插件。笔者并没有太深入的就git丰富的shell进行探讨,还是从简单的环境搭建开始说吧:

  首先要到github.com注册git账号,在此就不详述了。注册之后可以更换头像,git的头像是放在avatar.com托管的,github再从avatar的api获取头像。

  其次谈谈关于git的两个windows下的客户端,

  1. github for windows:http://windows.github.com/
  2. git for windows: https://code.google.com/p/msysgit/downloads/list?q=full+installer+official+git

  前者是github的官方版本,提供了metro风格的UI,提供了本地git仓库的创建、管理及github上仓库的同步等功能,此外还提供git shell命令行。

  后者是google code上的一个开源项目,除了提供git bash命令行之外,与前者不同的是:前者的安装路径是系统指定的,而后者的安装路径是用户自定义的,而且安装后会自动添加git的path环境变量,方便使用,打开cmd后,输入git我们可以看到

 1 Microsoft Windows [版本 6.1.7601]
 2 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
 3 
 4 C:\Users\yourname>git
 5 usage: git [--version] [--exec-path[=<path>]] [--html-path] [--man-path] [--info
 6 -path]
 7            [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
 8            [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
 9            [-c name=value] [--help]
10            <command> [<args>]
11 
12 The most commonly used git commands are:
13    add        Add file contents to the index
14    bisect     Find by binary search the change that introduced a bug
15    branch     List, create, or delete branches
16    checkout   Checkout a branch or paths to the working tree
17    clone      Clone a repository into a new directory
18    commit     Record changes to the repository
19    diff       Show changes between commits, commit and working tree, etc
20    fetch      Download objects and refs from another repository
21    grep       Print lines matching a pattern
22    init       Create an empty git repository or reinitialize an existing one
23    log        Show commit logs
24    merge      Join two or more development histories together
25    mv         Move or rename a file, a directory, or a symlink
26    pull       Fetch from and merge with another repository or a local branch
27    push       Update remote refs along with associated objects
28    rebase     Forward-port local commits to the updated upstream head
29    reset      Reset current HEAD to the specified state
30    rm         Remove files from the working tree and from the index
31    show       Show various types of objects
32    status     Show the working tree status
33    tag        Create, list, delete or verify a tag object signed with GPG
34 
35 See 'git help <command>' for more information on a specific command.
36 
37 C:\Users\yourname>

这样,我们可以很方便的将git集成到第三方IDE中,比如webStorm,只需设置git.exe的路径即可。

  先讲讲如何将github上的项目clone到本地

 1 yourname@yourname-PC ~
 2 $ cd ~/path/to/yourproject
 3 yourname@yourname-PC ~/path/to/yourproject
 4 $ git clone https://github.com/username/projectname
 5 Cloning into 'projectname'...
 6 remote: Counting objects: 57, done.
 7 remote: Compressing objects: 100% (47/47), done.
 8 remote: Total 57 (delta 20), reused 36 (delta 7)
 9 Unpacking objects: 100% (57/57), done.
10 yourname@yourname-PC ~/path/to/yourproject
11 $

  只要知道的想要clone的项目的github地址就可以就行clone,上面用的是https协议,当然我们也可以采用ssh和git协议。

  下面讲讲本地仓库和github服务器仓库的同步:

  安装git for windows的客户端之后,我们要做的是生成git的ssh key,生成的ssh key是我们本地和远程github仓库链接的密钥。

  首先,打开git bash,输入以下命令:

 1 Welcome to Git (version 1.8.1.2-preview20130201)
 2 
 3 Run 'git help git' to display the help index.
 4 Run 'git help <command>' to display help for specific commands.
 5 yourname@yourname-PC ~
 6 $ cd ~/.ssh
 7 yourname@yourname-PC ~/.ssh
 8 $ ssh-keygen -t rsa -C "youremail@example.com"
 9 Generating public/private rsa key pair.
10 Enter file in which to save the key(/c/Users/yourname/.ssh/id_rsa):
11 Enter passphrase again:
12 Your identification has been saved in /c/Users/yourname/.ssh/id_rsa.
13 Your public key has been saved in /c/Users/yourname/.ssh/id_rsa.pub.
14 The key fingerprint is:
15 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx youremail@example.com
16 yourname@yourname-PC ~./ssh
17 $

在三个Enter可以输入三个回车使用默认路径和空暗号,也可以自定义路径和输如用户自定义的暗号。

  ssh key成功生成后,我们可以在~/.ssh/中看到id_rsa和id_rsa.pub一对文件,一个是密钥,一个是公钥。

  本地的ssh key生成后,登录github后,在account settings的SSH Keys选项卡中Add SSH key,然后打开id_rsa.pub,将里面的内容粘贴到key文本框中,再Add SSH key我们就完成了本地和github密钥的配对。

  为了测试与github是连接是否成功,我们可以执行如下命令:

1 yourname@yourname-PC ~/.ssh
2 $ ssh git@github.com
3 Hi youremail! You're successfully authenticated, but GitHub does not provide shell access.
4 Connection to github.com closed.
5 yourname@yourname-PC ~/.ssh
6 $

  可以看到授权成功的提示信息,如果提示失败可以执行ssh-add命令,或者查找其他原因。

  至此,我们就建立和github服务器的ssh链接了,我们就可以将本地的仓库和代码改动提交到github上了。

  在github for windows中,打开Github,选择local中的仓库,我们可以看到近期提交的改动,当然我们可以通过Sync将本地提交的改动同步到github上。

  当然我们也可以通过git bash完成上面的操作:

1 yourname@yourname-PC ~
2 $ cd ~/path/to/yourproject
3 yourname@yourname-PC ~/path/to/yourproject(master)
4 $ git push origin master
5 Username for 'https://github.com': youremail@example.com
6 Password for 'https://youremail@example.com@github.com':
7 Everything up-to-date
8 yourname@yourname-PC ~/path/to/yourproject(master)
9 $

  如果没有用git init初始化你的项目,那么进入到你项目路径下将不会有(master)分支,那么我们在git push之前

1 yourname@yourname-PC ~
2 $ cd ~/path/to/yourproject
3 yourname@yourname-PC ~/path/to/yourproject
4 $ git init
5 Initialized empty Git repository in c:/Users/yourname/path/to/yourproject/.git/
6 yourname@yourname-PC ~/path/to/yourproject(master)
7 $

然后再git push即可。

posted @ 2013-04-14 22:01  FingerDancing  阅读(870)  评论(2编辑  收藏  举报