Linux下Git和GitHub使用方法詳解

來源:互聯網
上載者:User

一、linux上安裝git軟體

可以直接從發行版本的源裡進行安裝

# sudo apt-get install git   //ubuntu發行版下
# yum -y install git     //redhat、centos發行版下

二、使用https使用者名稱密碼認證串連github

1、在github上建立項目

首先需要從github上申請一個帳號,申請完成後在點擊右上方的“+” 號建立一個新的repository項目,如下:

2、主機上初始化項目並同步到github伺服器上

在linux主機上初始化該項目並同步到github伺服器上。

[root@361way abc]# echo '# 361way' >> README.md
[root@361way abc]# git init
[root@361way abc]# git add README.md
[root@361way abc]# git commit -m "first commit"
[root@361way abc]# git remote add origin https://github.com/91it/361way.git
[root@361way abc]# git push -u origin master
Username for 'https://github.com': 91it
Password for 'https://91it@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 201 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/91it/361way.git
 * [new branch]      master -> master
分支 master 設定為跟蹤來自 origin 的遠程分支 master。
[root@361way abc]#

3、免使用者名稱密碼登入

如果想避免互動式輸入使用者密碼,可以將git remote add包更換為:

git remote add origin  https://使用者名稱:密碼@github.com/使用者名稱/repository項目名.git
配置完後再去git push
已經配置過的可以通過修改本地項目目錄下的.git/config檔案裡的 [remote "origin"] 項下的 url 值,也可以通過git remote set-url origin 指令進行修改:

git remote set-url origin  https://使用者名稱:密碼@github.com/使用者名稱/repository項目名.git
本地已經存在的項目,可以省去git init的過程,直接執行最後兩步push到伺服器上

git remote add origin  https://使用者名稱:密碼@github.com/使用者名稱/repository項目名.git
git push -u origin master

三、使用ssh key認證串連github項目

某些雲主機或vps主機,使用https認證進行串連時會出現403錯誤,如下:

[root@91it test]# git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/91it/test.git/info/refs
fatal: HTTP request failed

出現此類情況可以嘗試使用ssh key管理。

1、ssh key認證配置

linux主機使用ssh-keygen指令產生key

[root@361way mnt]# ssh-keygen -t rsa -C "itybku@139.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:
8d:da:34:b3:11:f9:58:e7:5e:28:9c:e4:31:3d:df:38 itybku@139.com
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|         .   .   |
|        o . = oE |
|         B * = o+|
|        S o B . o|
|       + = . o   |
|      . o   .    |
|                 |
|                 |
+-----------------+
這裡使用郵箱地址是我申請github帳號的郵箱地址。將使用者家目錄下的.ssh/id_rsa.pub的內容複寫。

2、github設定及linux主機驗證

登入github.com,進入Account Settings,左邊選擇SSH Keys,Add SSH Key,title隨便填,粘貼id_rsa.pub裡的內容。配置完成後可以使用以下指令在linux主機上進行驗證。

[root@361way test]# ssh -T git@github.com
Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts.
Hi 91it! You've successfully authenticated, but GitHub does not provide shell access.
如果出現上面的提示表示增加key成功,如是出現Agent admitted failure to sign using the key 提示,則可以通過執行下面的指令增加key

ssh-add ~/.ssh/id_rsa

3、使用ssh協議進行同步

echo ' # 361way ' >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:91it/361way.git
git push -u origin master
ssh協議和https協議url 可以通過修改本地項目目錄下的.git/config檔案或git remote set-url origin 指令進行修改。

四、查看git 項目源

可以通過查看項目下的.git/config檔案查看,也可以通過git remote -v指令進行查看,樣本如下:

#https認證
[root@361way test]# git remote -v
origin  https://github.com/91it/test.git (fetch)
origin  https://github.com/91it/test.git (push)
#ssh認證
[root@361way 361way]# git remote -v
origin  git@github.com:91it/361way.git (fetch)
origin  git@github.com:91it//361way.git (push)

五、其他錯誤

1、在執行$ git remote addorigin git@github.com:91it/test.git 錯誤提示:fatal: remote origin already exists.

解決方案:

$ git remote rm origin
然後在執行:$ git remote add origin git@github.com:defnngj/hello-world.git 就不會報錯誤了
2、在執行$ git push origin master錯誤提示:error:failed to push som refs to.......

解決方案:

$ git pull origin master // 先把遠程伺服器github上面的檔案拉下來,再push 上去。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.