---
title: "centos6 git github"
author: "mofeichen"
date: "2015-03-22T23:30:16Z"
tags: []
categories: ["Web前端"]
slug: "centos6-git-github"
draft: false
lastmod: "2015-03-22T23:30:16Z"
id: 2194613
url: "https://mofeichen.iteye.com/blog/2194613"
---

git的出现让传统的svn陷入尴尬的境地，分布式的版本控制是局势所需，svn目前也称要支持分布式，但至今依然是浮云。下面介绍安装git与简单实用，以github做例子。

### 安装

最简单，可以直接下载资源包，然后安装就好，如果是centos，可以实用yum命令安装：

[?](http://www.qttc.net/201307351.html)

1

`# yum install git git-gui`

### 注册github

打开github站点github.com根据提示注册就好，免费的

### 导入公钥

首先，要生成公钥

[?](http://www.qttc.net/201307351.html)

1

`# ssh-keygen -t rsa -C "你注册github所用的邮箱地址"`

查看公钥

[?](http://www.qttc.net/201307351.html)

1

`# cat ~/.ssh/id_rsa.pub`

复制它，然后在github右上角点击设置按钮

![](http://www.qttc.net/static/upload/2013/13738574259274.jpg)

点击左侧菜单栏的 SSH keys 项，打开SSH keys 管理页面，然后在key管理页面中点击 Add SSH key 添加公钥 

![](http://www.qttc.net/static/upload/2013/13738575723576.jpg)

在弹出的dialog中，Title随意填写，key栏复制你在linux机器上创建的公钥

![](http://www.qttc.net/static/upload/2013/13738576974491.jpg)

### 账号配置

[?](http://www.qttc.net/201307351.html)

1

2

`# git config --global user.name "用户名"`

`# git config --global user.email github注册的邮箱地址`

配置好了以后，执行以下命令校验

[?](http://www.qttc.net/201307351.html)

1

`# ssh git@github.com`

如果提示：

[?](http://www.qttc.net/201307351.html)

1

2

3

`PTY allocation request failed on channel 0`

`Hi lizhong8532! You've successfully authenticated, but GitHub does not provide shell access.`

`Connection to github.com closed.`

表示验证通过。

### 创建github仓库

我们创建一个test仓库

![](http://www.qttc.net/static/upload/2013/13738583091408.jpg)

复制test仓库ssh git地址，提交需要用

![](http://www.qttc.net/static/upload/2013/13738584033597.jpg)

### 创建项目目录

[?](http://www.qttc.net/201307351.html)

1

2

`[root@localhost ~]# mkdir test`

`[root@localhost ~]# cd test`

初始化：

[?](http://www.qttc.net/201307351.html)

1

2

`[root@localhost test]# git init`

`Initialized empty Git repository in` `/root/test/.git/`

创建README.md文件

[?](http://www.qttc.net/201307351.html)

1

`[root@localhost test]# echo "这是一个测试的案例" > README.md`

### 提交到本地

[?](http://www.qttc.net/201307351.html)

1

2

3

4

5

`[root@localhost test]# git add .`

`[root@localhost test]# git commit -m "第一次提交"`

`[master (root-commit) c9206dc] 第一次提交`

`1 files changed, 1 insertions(+), 0 deletions(-)`

`create mode 100644 README.md`

其中git add . 中的点表示提交这个项目下所有文件，也可以直接指定文件 git add README.md

### 提交到github

以上，只是提交到本地版本库，github仓库上还没有文件，现在提交到github

[?](http://www.qttc.net/201307351.html)

1

2

3

4

5

6

7

8

`[root@localhost test]# git remote add origin git@github.com:lizhong8532/test.git`

`[root@localhost test]# git push -u origin master`

`Counting objects: 3, done.`

`Writing objects: 100% (3/3), 255 bytes, done.`

`Total 3 (delta 0), reused 0 (delta 0)`

`To git@github.com:lizhong8532/test.git`

`* [new branch]      master -> master`

`Branch master set` `up to track remote branch master from origin.`

其中，git@github.com:lizhong8532/test.git是我的test仓库的提交地址，你需要改成你的test仓库地址就好。

然后刷新你的仓库，就能看见刚才提交的文件了

![](http://www.qttc.net/static/upload/2013/13738590781170.jpg)