我是靠谱客的博主 愉快电灯胆,这篇文章主要介绍linux虚拟机上使用git和github教程,现在分享给大家,希望可以做个参考。

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

复制代码
1
yum install git git-gui
3.3 生成秘钥对,这样项目可以push到github上

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[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

复制代码
1
2
3
4
5
6
7
8
[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全局用户配置

复制代码
1
2
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中仓库创建完成后网页上的提示执行

复制代码
1
2
3
4
5
6
7
8
9
10
11
# 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操作时,可能会出现如下错误:

复制代码
1
2
3
[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
2
3
4
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

复制代码
1
2
3
4
5
6
7
8
9
10
11
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上了。

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

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

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


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

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


最后

以上就是愉快电灯胆最近收集整理的关于linux虚拟机上使用git和github教程的全部内容,更多相关linux虚拟机上使用git和github教程内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部