# Github的使用


@(Github)[Git|安裝|代碼倉庫|代碼存放]

github 是一個基於git的代碼託管平臺,付費用戶可以建私人倉庫,我們一般的免費用戶只能使用公共倉庫,也就是代碼要公開。GithubChris Wanstrath, PJ HyettTom Preston-Werner三位開發者在2008年4月創辦。迄今擁有59名全職員工,主要提供基於git的版本託管服務。

目前看來, github這場冒險已經勝出。根據來自維基百科關於GitHub的描述,我們可以形象地看出GitHub的增長速度:

enter image description here


1 Git 遠程倉庫(Github)

Git 並不像 SVN 那樣有個中心服務器。
目前我們使用到的 Git 命令都是在本地執行,如果你想通過 Git 分享你的代碼或者與其他開發人員合作。 你就需要將數據放到一臺其他開發人員能夠連接的服務器上。本例使用了Github 作爲遠程倉庫,你可以先閱讀我們的Github 簡明教程

添加遠程庫

要添加一個新的遠程倉庫,可以指定一個簡單的名字,以便將來引用,命令格式如下:

   git remote add [shortname] [url]

本例以Github爲例作爲遠程倉庫,如果你沒有Github可以在官網https://github.com/註冊。

  • 1 - 由於你的本地Git倉庫和GitHub倉庫之間的傳輸是通過SSH加密的,所以我們需要配置驗證信息:
  • 2 - 使用以下命令生成SSH Key:
  $ ssh-keygen -t rsa -C "[email protected]"
  • 3 - 後面的 [email protected] 改爲你在 github 上註冊的郵箱,之後會要求確認路徑和輸入密碼,我們這使用默認的一路回車就行。成功的話會在~/下生成**.ssh**文件夾,進去,打開 id_rsa.pub,複製裏面的 key

  • 4 -回到 **github **上,進入 Account => Settings(賬戶配置)。

    enter image description here

  • 5 -左邊選擇 SSH and GPG keys,然後點擊 New SSH key 按鈕,title 設置標題,可以隨便填,粘貼在你電腦上生成的 key。

    enter image description here

  • 6 -爲了驗證是否成功,輸入以下命令:

   $ ssh -T [email protected]
   Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access.

以下命令說明我們已成功連上 Github。

2 配置Git

首先在本地創建ssh key;

 $ ssh-keygen -t rsa -C "[email protected]"

後面的[email protected]改爲你在github上註冊的郵箱,之後會要求確認路徑和輸入密碼,我們這使用默認的一路回車就行。成功的話會在~/下生成.ssh文件夾,進去,打開id_rsa.pub,複製裏面的key。回到github上,進入 Account Settings(賬戶配置),左邊選擇SSH Keys,Add SSH Key,title隨便填,粘貼在你電腦上生成的key。 生成key

enter image description here

爲了驗證是否成功,在git bash下輸入:

  $ ssh -T [email protected]

如果是第一次的會提示是否continue,輸入yes就會看到:You’ve successfully authenticated, but GitHub does not provide shell access 。這就表示已成功連上github
接下來我們要做的就是把本地倉庫傳到github上去,在此之前還需要設置usernameemail,因爲github每次commit都會記錄他們。

  $ git config --global user.name "your name"
  $ git config --global user.email "[email protected]"

進入要上傳的倉庫,右鍵git bash,添加遠程地址:

假如你在自己的github上面創建了一個Repository項目文件裏面有如下所示,按照下面執行

  …or create a new repository on the command line

  echo "# spring-boot-demo" >> README.md   //創建readme.md文件
  git init                                 //初始化git
  git add README.md                        //向項目裏面添加readme.md文件,你也可以用*添加所有文件
  git commit -m "first commit"              //添加提交信息
  git remote add origin https://github.com/weiaigewang/spring-boot-demo.git
  				          //添加到要提交的倉庫Repository地址
  git push -u origin master                 //提交執行,等待執行信息

出現的錯誤

錯誤提示信息

在使用git推送項目時候出現 "fatal: The remote end hung up unexpectedly " 原因是推送的文件太大。

解決方案:
在克隆/創建版本庫生成的**.git目錄下面修改生成的config文件**增加如下:

     [http]
     postBuffer = 524288000

重新推送即可。

enter image description here

出現以下題

3 出現的問題

出現這樣的問題是由於:自己當前版本低於遠程倉庫版本

解決方法:

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]

鏈接:https://www.jianshu.com/p/004f47f908c5

原文鏈接: http://www.runoob.com/w3cnote/git-guide.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章