歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux虛擬機上使用git和github教程

linux虛擬機上使用git和github教程

日期:2017/3/3 17:00:58   编辑:關於Linux

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

  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上了。

Copyright © Linux教程網 All Rights Reserved