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虛擬機,安裝命令如下:

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 [email protected]
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 [email protected]
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 "[email protected]"
此處用戶名爲自己的實際姓名(自定義的),而非登錄用戶名
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 [email protected]:***/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://[email protected]/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://[email protected]/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


發佈了33 篇原創文章 · 獲贊 6 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章