歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 如何在ubuntu下使用Github?

如何在ubuntu下使用Github?

日期:2017/3/3 13:46:00   编辑:Linux技術

安裝Git

一個全新的ubunt系統,需要安裝Git(系統是不具有該工具的),方法如下:
在terminel中輸入如下命令:
[code]sudo apt-get install git git-core git-gui git-doc git-svn git-cvs gitweb gitk git-email git-daemon-run git-el git-arch

接下來需要檢查SSH
因為GitHub會用到SSH,因此需要在shell裡檢查是否可以連接到GitHub:
[code]ssh -T [email protected]

如果看到:
Warning: Permanently added ‘github.com,204.232.175.90’ (RSA) to the list of known hosts.
Permission denied (publickey).
則說明可以連接。
(參考)
這裡假設你已經就有了GitHub用戶(如果沒有,需要去注冊GitHub)

安裝SSH keys

在安裝GitHub之前,需要先安裝SSH keys
第一步:檢查是否已井具有ssh keys,如果已經具有,則進行第二步,否則,進行第三步
[code]cd ~/.ssh
ls


查看該目錄下是否已經具有ssh keys,發現並沒有id_rsa(私鑰)和id_rsa.pub(公鑰)這兩個文件
第二步:備份並移除已經存在的ssh keys
[code]mkdir key_backup
cp id_rsa* key_backup
rm id_rsa*

即將已經存在的id_rsa,id_rsa.pub文件備份到key_backup文件夾
第三步:執行如下命令(不具有ssh keys時):
[code]ssh-keygen -t rsa -C "你自己的github對應的郵箱地址"

注1:“”是需要的!
注2:是在ssh目錄下進行的!
得到結果如下:

發現,id_rsa(私鑰)和id_rsa.pub(公鑰)這兩個文件被創建了
(通過ls查看~/.ssh下面的所有內容查看)
第四步:將剛剛創建的ssh keys添加到github中
(1)利用gedit/cat命令,查看id_rsa.pub的內容
(2)在GitHub中,依次點擊Settings -> SSH Keys -> Add SSH Key,將id_rsa.pub文件中的字符串復制進去,注意字符串中沒有換行和空格。
第五步:再次檢查SSH連接情況(在~/.ssh目錄下):
輸入如下命令:
[code]ssh -T [email protected]

如果看到如下所示,則表示添加成功:
Hi alioth310! You’ve successfully authenticated, but GitHub does not provide shell access.
此時,發現github上已有了SSH keys
注1:之前在設置公鑰時如果設置了密碼,在該步驟會要求輸入密碼,那麼,輸入當時設置的密碼即可。
注2:通過以上的設置之後,就能夠通過SSH的方式,直接使用Git命令訪問GitHub托管服務器了

開始使用github

參考廖雪峰github教程;Github 簡明教程
;Linux操作Git遠程倉庫與本地倉庫同步的教程;

配置git

即利用自己的用戶名和email地址配置git
[code]git config --global user.name "你的github用戶名"
git config --global user.email "你的github郵箱地址"

如何推送本地內容到github上新建立的倉庫

github上新建立倉庫

具體內容不做介紹,假設,新建的倉庫為dockerfiels

在本地建立一個目錄

該目錄名稱與github新建立的目錄相同,假設本地目錄為~/Document/dockerfiles

本地倉庫初始化

[code]cd ~/Document/dockerfiles
git init

對本地倉庫進行更改

例如,添加一個Readme文件
[code]touch Readme

對剛剛的更改進行提交

該步不可省略!
[code]git add Readme
git commit -m 'add readme file'

push

首先,需要將本地倉庫與github倉庫關聯
注:https://github.com/你的github用戶名/你的github倉庫.git 是github上倉庫的網址
[code]git remote add originhttps://github.com/你的github用戶名/你的github倉庫.git

然後,push,此時,可能需要輸入github賬號和密碼,按要求輸入即可
[code]git push origin master

如何推送本地內容到github上已有的倉庫

從github上將該倉庫clone下來

[code]git clonehttps://github.com/你的github用戶名/github倉庫名.git

對clone下來的倉庫進行更改

例如,添加一個新的文件
[code]touch Readme_new

對剛剛的更改進行提交

該步不可省略!(其實是提交到git緩存空間)
[code]git add Readme_new
git commit -m 'add new readme file'

push

首先,需要將本地倉庫與github倉庫關聯
注:https://github.com/你的github用戶名/你的github倉庫.git 是github上倉庫的網址
[code]git remote add originhttps://github.com/你的github用戶名/你的github倉庫.git

有時,會出現fatal: remote origin already exists.,那麼,需要輸入git remote rm origin 解決該問題
然後,push,此時,可能需要輸入github賬號和密碼,按要求輸入即可
[code]git push origin master

注:有時,在執行git push origin master時,報錯:error:failed to push som refs to…….,那麼,可以執行
[code]git pull origin master

至此,github上已有的倉庫的便有了更新
如果需要添加文件夾,有一點需要注意:該文件夾不能為空!否則不能成功添加

操作命令小結

克隆github上已有的倉庫
[code]git clone' target='_blank'>https://github.com/你的github用戶名/github倉庫名.git[/code] 
或者是在github上新建倉庫並且在本地新建同名的倉庫
[code]cd ~/Document/dockerfiles
git init

對本地倉庫內容進行更改(如果是多次對本地的某個倉庫進行這樣的操作,直接從此步開始即可,不要前面的操作了,因為本地倉庫已有具有了github倉庫的.git文件了)
對更改內容進行提交
[code]git add 更改文件名或者是文件夾名或者是點"."
git commit -m "commit內容標注"

本地倉庫與github倉庫關聯
[code]git remote add originhttps://github.com/你的github用戶名/你的github倉庫.git

push
[code]git push origin master

注:另外可能用到的命令
[code]git remote rm origin
git pull origin master

查看當前git緩存空間狀態

[code]git status
Copyright © Linux教程網 All Rights Reserved