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虚拟机,安装命令如下:

yum install git git-gui
3.3 生成秘钥对,这样项目可以push到github上
[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.com
The 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

[root@CentOS tuzhutuzhu]# ssh git@github.com
The 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)? yes
Warning: 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全局用户配置
# 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中仓库创建完成后网页上的提示执行

# 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操作时,可能会出现如下错误:

[root@CentOS vmware]# git push -u origin master
error: The requested URL returned error: 403 while accessing https://github.com/tuzhutuzhu/vmware.git/info/refs

fatal: HTTP request failed
这是因为GitHub好像只支持ssh的方式访问仓库。解决方法如下:
1).vim .git/config
2).将[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

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 -> master
Branch master set up to track remote branch master from origin.
[root@CentOS vmware]#
这样,就将文件上传到GitHub上了。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐