Ubuntu環境下GitHub安裝與使用


安裝git

sudo apt-get update sudo apt-get install git
  • 1
  • 2
  • 1
  • 2

配置 你的github

git config --global user.name "Your Name" git config --global user.email "youremail@domain.com"
  • 1
  • 2
  • 1
  • 2

查看配置信息

git config --list
  • 1
  • 1

編輯配置信息

gedit ~/.gitconfig
  • 1
  • 1

修改

[user] name = Your Name email = youremail@domain.com 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

創建公鑰

ssh-keygen -C 'you email address@gmail.com' -t rsa
  • 1
  • 1

這會在 用戶目錄 ~/.ssh/ 下建立相應的密鑰文件

上傳公鑰

在 github.com 的界面中 選擇右上角的 Account Settings,然后選擇 SSH Public Keys ,選擇新加。

Title 可以隨便命名,Key 的內容拷貝自 ~/.ssh/id_rsa.pub 中的內容,完成后,可以再使用

ssh -v git@github.com
  • 1
  • 1

進行測試。看到下面的信息表示驗證成功。

... ... ... 各種信息 ... ... .. . 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

GitHub使用方法

登錄GitHub賬戶,點擊New repository,填寫倉庫名后; 
有兩種方式來初始化倉庫:在本地的工作目錄初始化新倉庫、從現有倉庫克隆 
(1)在本地的工作目錄初始化新倉庫 
進入項目的目錄下:

touch README.md

git init     ##重新初始化Git倉庫地址。如:現存的 Git 倉庫於 /home/zzh/code/.git/ git add * ##添加上傳的文件 git commit -m 'initial project version' git remote add origin git@github.com:TimorChow/FirstDemo.git ## 即剛剛創建的倉庫的地址 git push -u origin master ##推送代碼到遠程代碼庫
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(2)從現有倉庫克隆

    git remote add origin git_address #git_address即現有倉庫的地址 #如 git@github.com:TimorChow/baike_spider git push -u origin master
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

把GitHub里的項目復制到本地:

Git clone git_address

(3)本地代碼更新推送

  #更新文件   vi README   #自動commit更改文件   git commit -a   #更新至遠程   git push origin master   #創建和合並分支   git branch   #顯示當前分支是master   git branch new-feature   #創建分支   git checkout new-feature   #切換到新分支   vi page_cache.inc.php      git add page_cache.inc.php   #Commit 到本地GIT   git commit -a -m "added initial version of page cache"   #合並到遠程服務器   git push origin new-feature   #如果new-feature分支成熟了,覺得有必要合並進master   git checkout master   git merge new-feature   git branch   git push   #則master中也合並了new-feature 的代碼
  • 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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 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
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

剛創建的github版本庫,在push代碼時出錯:

git push -u origin master 
To git@github.com:**/Demo.git 
! [rejected] master -> master (non-fast-forward) 
error: failed to push some refs to ‘git@github.com:**/Demo.git’ 
hint: Updates were rejected because the tip of your current branch is behind 
hint: its remote counterpart. Merge the remote changes (e.g. ‘git pull’) 
hint: before pushing again. 
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

網上搜索了下,是因為遠程repository和我本地的repository沖突導致的,而我在創建版本庫后,在github的版本庫頁面點擊了創建README.md文件的按鈕創建了說明文檔,但是卻沒有pull到本地。這樣就產生了版本沖突的問題。

有如下幾種解決方法:

1.使用強制push的方法:

$ git push -u origin master -f

這樣會使遠程修改丟失,一般是不可取的,尤其是多人協作開發的時候。

2.push前先將遠程repository修改pull下來

$ git pull origin master

$ git push -u origin master

3.若不想merge遠程和本地修改,可以先創建新的分支:

$ git branch [name]

然后push

$ git push -u origin [name]

github常見操作和常見錯誤:http://blog.csdn.net/god_wot/article/details/10522405 
參考文章:http://blog.csdn.net/small_rice_/article/details/45095323


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM