以前写过很多测试程序,但是代码在本地到处乱扔,也从来没有归整过,现基于github来维护自己的代码,这样只要能连网,随时就可以查了。
目标
-
配置SSH证书连接github
-
克隆github代码仓库到本地开发
下面跟着以下步骤就可以完成了。
- 生成证书
[root@ceshi03 test]# mkdir -p /root/test #所有操作都在此目录中进行
[root@ceshi03 test]# ssh-keygen -t rsa -C "xxx@126.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): githubkey
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in githubkey.
Your public key has been saved in githubkey.pub.
The key fingerprint is:
xxx:xxx xxx@126.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| ..|
| o+|
| . .oo|
| S . .o|
| . E ..|
| .. +..|
| + *o++ |
| o .ooBo+o|
+-----------------+
[root@ceshi03 test]# ls
githubkey githubkey.pub
#生成公私钥对,把公钥拷贝出来,放到github中。
在github的个人头像的"setting"=》"SSH and GPG keys"中添加
- 设置git用户名和邮箱
这里把xxx设置成你自己的就可以了
git config --global user.name "xxx"
git config --global user.email "xxx@126.com"
- 启动ssh-agent
[root@ceshi03 test]# eval "$(ssh-agent -s)"
Agent pid 304881
#启动后可查看有如下进程
[root@ceshi03 test]# ps -ef|grep agen
root 303069 1 0 05:20 ? 00:00:00 ssh-agent -s
-
添加生成的 私钥githubkey到 ssh-agent
保证私钥的权限为600,可通过
chmod 600 githubkey
修改
[root@ceshi03 test]# ssh-add ./githubkey
Identity added: ./githubkey (./githubkey)
- 测试
如果出现如下提示,说明认证这一关配置已经成功
[root@ceshi03 test]# ssh -T git@github.com
Hi sweird1! You've successfully authenticated, but GitHub does not provide shell access.
- 从github中clone自己的代码仓库
在github网站上找到自己的SSH仓库地址:git@github.com:sweird1/c_study_test.git
[root@ceshi03 test]# git clone git@github.com:sweird1/c_study_test.git
Cloning into 'c_study_test'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.
- 向仓库add文件
以上步骤完成后,在/root/test/会有上述仓库的目录c_study_test/,然后进行如下操作:
[root@ceshi03 test]# cd c_study_test/
[root@ceshi03 c_study_test]# ls
hello.c README.md
[root@ceshi03 c_study_test]# touch test.c
[root@ceshi03 c_study_test]# echo "111112223344hh">test.c
You have mail in /var/spool/mail/root
[root@ceshi03 c_study_test]# cat test.c
111112223344hh
[root@ceshi03 c_study_test]# git add test.c
[root@ceshi03 c_study_test]# git commit test.c -m "测试使用"
[master a111cbb] 测试使用
1 file changed, 1 insertion(+)
create mode 100644 test.c
[root@ceshi03 c_study_test]# git push
Counting objects: 3, done.
Delta compression using up to 24 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 325 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:sweird1/c_study_test.git
b4077fd..a111cbb master -> master
- 验证是否真的把文件提交到了github
访问项目地址https://github.com/sweird1/c_study_test
到此为止就可以在本地基于github开发了。
注意:如果要使用git向github提交目录的话,空目录是提交不了的,只有先在目录里创建文件后才能提交。