【原】GitHub使用教程

GitHub使用教程

1 注册 GitHub 账户

要想使用github第一步当然是注册github账号了(www.github.com)。

2 安装客户端 msysgit

github是服务端,要想在自己电脑上使用git我们还需要一个git客户端,我这里选用msysgit,是基于命令行的。装完msysgit后,右键鼠标会多出一些选项来。

3 建立本地 git 仓库

在D盘下创建目录git_repository(后续的项目都可以集中放在git_repository中),可以通过在git bash中执行以下命令完成:

$ cd /d
$ mkdir git_repository

4 SSH key

为了把本地的仓库传到github,需要配置ssh key。

(1)生成SSH key

$ ssh-keygen -t rsa -C "your_email@example.com"  //之后一直回车,直到生成

注意:ssh-keygen中的邮箱请使用github注册用户时使用的邮箱。

(2)将新生成的 SSH key加入ssh-agent(可以省略此步骤)

$ ssh-agent -s   //输出:Agent pid 59566
$ ssh-add ~/.ssh/id_rsa

如果输出:

Could not open a connection to your authentication agent.

则先执行如下命令:

$ ssh-agent bash

再执行:

$ ssh-add ~/.ssh/id_rsa

(3)将SSH key 加入GitHub

$ clip < ~/.ssh/id_rsa.pub  //复制到剪切板

之后在 GitHub网站中Add SSH key

(4)验证是否成功加入SSH key

$ ssh -T git@github.com   //之后选yes

若成功,则输出:

Hi “你的用户名”! You've successfully authenticated, but GitHub does not provide shell access.

5 设置username和email

因为github每次commit都会记录他们。

$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"

设置后可以用下面命令查看:

$ git config –list

6 在 GitHub 中创建 Repository

(1)创建 GitHub中的仓库

New Repository,填好名称后Create,之后会出现一些仓库的配置信息,这也是一个git的简单教程,如下:

create a new repository on the command line

git init   //初始化
touch README.md
git add README.md  //开始跟踪新文件或暂存已修改文件
git commit -m "first commit"  //提交更新,并注释信息“first commit” ,注意提交的是暂存区的文件(即add过的文件)
git remote add origin https://github.com/account/demo.git  //连接远程github项目
git push -u origin master   //将本地项目更新到github项目上去

进入.git,打开config,这里会多出一个remote “origin”内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。

push an existing repository from the command line

git remote add origin https://github.com/account/demo.git
git push -u origin master

import code from another repository

You can initialize this repository with code from a Subversion, Mercurial, or TFS project.

(2)在本地创建一个相同项目

$ cd /d/git_repository
$ mkdir demo    //创建一个项目demo
$ cd demo    //打开这个项目

之后按照(1)中的 【create a new repository on the command line】步骤操作即可,具体如下。

$ git init
$ git add .//添加所有文件

此时会出错【warning: LF will be replaced by CRLF】.因为windows中的换行符为 CRLF, 而在linux下的换行符为LF,所以在执行add . 时出现提示,解决办法:

$ rm -rf .git  // 删除.git
$ git config --global core.autocrlf false  //禁用自动转换 
$ git init  
$ git add . 

忽略Warning: Your console font probably doesn't support Unicode. If you experience strange characters in the output, consider switching to a TrueType font such as Lucida Console!

$ git commit -m "first commit"
$ git remote add origin https://github.com/account/demo.git
$ git push -u origin master

(3)以后每次修改项目后提交github

$ git add . 
$ git commit -m "commit msg"
$ git push -u origin master
posted @ 2014-11-07 11:56  seven7seven  阅读(447)  评论(0)    收藏  举报