GitHub是个耳熟能详的东西,大大小小的开源软件多是在GitHub上建的git仓库,GitHub中只要你建立公共的仓库就是免费服务,当然私有的仓库就需要一些money了。
在GitHub上创建一个自己的仓库来存储代码是一件非常轻松且愉快的事情,只要有网络,你可以在任意时间和地点去同步你的代码,是不是很令人兴奋!
使用GitHub需要先注册一个帐户,关于注册不再赘述。
有了帐号之后,开始实验一下如何在本地与GitHub上的仓库进行同步。
1、还是ssh-key
在本地与GitHub进行交互,需要先添加本地机器的ssh-key到GitHub中,ssh-key通过”ssh-keygen“获取,然后保存到GitHub中的”帐户设置“->”SSH Keys”当中,这样本地才能够与GitHub中的仓库进行联系。
2、创建github仓库
在这里创建了一个名为”githubTest“的公共仓库,并自动创建了README.md文件。
仓库创建完成后,显示在”SSH“后方输入框中的就是用于clone的地址。
3、本地操作
在本地clone下来GitHub上刚创建的仓库,并新建一个文件推送到GitHub仓库中去:
[root@localhost ~]# git clone git@github.com:******/githubTest
正克隆到 'githubTest'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
接收对象中: 100% (3/3), done.
[root@localhost ~]# cd githubTest/
[root@localhost githubTest]# ls
README.md
[root@localhost githubTest]#
[root@localhost githubTest]# cat README.md
# githubTest[root@localhost githubTest]#
[root@localhost githubTest]# echo "this second file" > 2th.txt
[root@localhost githubTest]# ls
2th.txt README.md
[root@localhost githubTest]# git add 2th.txt
[root@localhost githubTest]# git commit -m "add file -- 2th.txt"
[master 3f7dffd] add file -- 2th.txt
1 file changed, 1 insertion(+)
create mode 100644 2th.txt
[root@localhost githubTest]# git push origin master
完成推送后,再刷新GitHub上的仓库,就能够看到刚刚推送上去的新文件了。