Linux下使用github教程

来源:互联网 发布:自制相册软件下载 编辑:程序博客网 时间:2024/05/22 12:53

1. Git是什么?

Git是一个分布式版本控制/软件配置管理软件,原是Linux内核开发者Linus Torvalds为更好地管理Linux内核开发而设计。

相比CVS/SVN,Git 的优势:
- 支持离线开发,离线Repository
- 强大的分支功能,适合多个独立开发者协作
- 速度块

2.GITHUB是什么?

GitHub 是一个共享虚拟主机服务,用于存放使用Git版本控制的软件代码和内容项目。

3.Linux虚拟机上使用git和github

3.1 注册github账号

此步骤比较简单,无需赘述。

3.2 虚拟机安装git客户端

我使用的是CentOS虚拟机,安装命令如下:

[cpp] view plain copy print?
  1. yum install git git-gui  
yum install git git-gui
3.3 生成秘钥对,这样项目可以push到github上

[cpp] view plain copy print?
  1. [root@CentOS tuzhutuzhu]# ssh-keygen -t rsa -C “*****@**.com”  
  2. Generating public/private rsa key pair.  
  3. Enter file in which to save the key (/root/.ssh/id_rsa):    
  4. Enter passphrase (empty for no passphrase):   
  5. Enter same passphrase again:   
  6. Your identification has been saved in /root/.ssh/id_rsa.  
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.  
  8. The key fingerprint is:  
  9. 63:68:68:ad:23:0f:8f:85:4a:cc:67:60:47:9e:7a:fa 15895214140@126.com  
  10. The key’s randomart image is:  
  11. +–[ RSA 2048]—-+  
  12. |                 |  
  13. |                 |  
  14. |   .             |  
  15. |  o .o .         |  
  16. | o +o + S        |  
  17. |+ +o o . .       |  
  18. | =+++            |  
  19. |..=B .           |  
  20. |..oEo            |  
  21. +—————–+  
  22. [root@CentOS tuzhutuzhu]#   
[root@CentOS tuzhutuzhu]# ssh-keygen -t rsa -C "*****@**.com"Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa):  Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:63:68:68:ad:23:0f:8f:85:4a:cc:67:60:47:9e:7a:fa 15895214140@126.comThe key's randomart image is:+--[ RSA 2048]----+|                 ||                 ||   .             ||  o .o .         || o +o + S        ||+ +o o . .       || =+++            ||..=B .           ||..oEo            |+-----------------+[root@CentOS tuzhutuzhu]# 
3.4 将.ssh/id_rsa.oub拷贝到GitHub


3.5 测试是否能连接到GitHub

[cpp] view plain copy print?
  1. [root@CentOS tuzhutuzhu]# ssh git@github.com  
  2. The authenticity of host ’github.com (192.30.252.131)’ can’t be established.  
  3. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.  
  4. Are you sure you want to continue connecting (yes/no)? yes  
  5. Warning: Permanently added ’github.com,192.30.252.131’ (RSA) to the list of known hosts.  
  6. Enter passphrase for key ‘/root/.ssh/id_rsa’:   
  7. Permission denied (publickey).  
  8. [root@CentOS tuzhutuzhu]#   
[root@CentOS tuzhutuzhu]# ssh git@github.comThe authenticity of host 'github.com (192.30.252.131)' can't be established.RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known hosts.Enter passphrase for key '/root/.ssh/id_rsa': Permission denied (publickey).[root@CentOS tuzhutuzhu]# 
3.6 设置Git全局用户配置

[cpp] view plain copy print?
  1. # git config –global user.name “Firstname Lastname”  
  2. # git config –global user.email “your_email@youremail.com”  
  3. 此处用户名为自己的实际姓名(自定义的),而非登录用户名  
# git config --global user.name "Firstname Lastname"
# git config --global user.email "your_email@youremail.com"此处用户名为自己的实际姓名(自定义的),而非登录用户名3.7 Git创建一个库(Create a Repository)


创建完成:


3.8 创建本地新项目仓库,此步骤可按照上图GitHub中仓库创建完成后网页上的提示执行

[cpp] view plain copy print?
  1. # mkdir new-project(*1)  
  2. # cd new-project  
  3. # git init  
  4. # touch README  
  5. # git add README  
  6. # git commit -m ‘first commit’  
  7. 定义远程服务器别名origin  
  8. #  git remote add origin git@github.com:***/new-project.git(*2)     
  9. 本地和远程合并,本地默认分支为master  
  10. # git push origin master   
# mkdir new-project(*1)
# cd new-project# git init# touch README# git add README# git commit -m 'first commit'定义远程服务器别名origin# git remote add origin git@github.com:***/new-project.git(*2) 本地和远程合并,本地默认分支为master# git push origin master (*1)  此处的仓库名必须与3.7中在GitHub中创建的仓库名相同
(*2) 此处***就是GitHub的用户名

在执行push操作时,可能会出现如下错误:

[cpp] view plain copy print?
  1. [root@CentOS vmware]# git push -u origin master  
  2. error: The requested URL returned error: 403 while accessing https://github.com/tuzhutuzhu/vmware.git/info/refs  
  3.   
  4. fatal: HTTP request failed  
[root@CentOS vmware]# git push -u origin mastererror: The requested URL returned error: 403 while accessing https://github.com/tuzhutuzhu/vmware.git/info/refsfatal: HTTP request failed
这是因为GitHub好像只支持ssh的方式访问仓库。解决方法如下:

[cpp] view plain copy print?
  1. 1).vim .git/config  
  2. 2).将[remote ”origin”]部分的url按照如下格式设置:  
  3.         url = ssh://git@github.com/tuzhutuzhu/vmware.git  
  4.         fetch = +refs/heads/*:refs/remotes/origin/*  
1).vim .git/config2).将[remote "origin"]部分的url按照如下格式设置:        url = ssh://git@github.com/tuzhutuzhu/vmware.git        fetch = +refs/heads/*:refs/remotes/origin/*
再次执行push操作,结果如下:

[root@CentOS vmware]# git push -u origin master

[cpp] view plain copy print?
  1. Enter passphrase for key ‘/root/.ssh/id_rsa’:   
  2. Counting objects: 7, done.  
  3. Delta compression using up to 2 threads.  
  4. Compressing objects: 100% (3/3), done.  
  5. Writing objects: 100% (7/7), 548 bytes, done.  
  6. Total 7 (delta 0), reused 0 (delta 0)  
  7. To ssh://git@github.com/tuzhutuzhu/vmware.git  
  8.  * [new branch]      master -> master  
  9. Branch master set up to track remote branch master from origin.  
  10. [root@CentOS vmware]#   
Enter passphrase for key '/root/.ssh/id_rsa': Counting objects: 7, done.Delta compression using up to 2 threads.Compressing objects: 100% (3/3), done.Writing objects: 100% (7/7), 548 bytes, done.Total 7 (delta 0), reused 0 (delta 0)To ssh://git@github.com/tuzhutuzhu/vmware.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.[root@CentOS vmware]# 

这样,就将文件上传到GitHub上了。

还有另外一种解决方法,将url做如下变更:

原:url = https:github.com/tuzhutuzhu/helloworld.git 

现:url = https://tuzhutuzhu@github.com/tuzhutuzhu/helloworld.git 


有关git的详细操作,可以参考如下:

http://download.csdn.net/detail/tuzhutuzhu/6966853


原创粉丝点击