建立本地專案並上傳GitHub

讓蛋蛋飛發表於2019-02-19

1. 建立本地專案

各人根據實際情況建立自己的專案(我在IntelliJ IDEA中建立了SpringBoot專案)

2. 在GitHub上建立倉庫

GitHub上建立倉庫1

GitHub上建立倉庫2

3. 將本地專案上傳到GitHub上

先cd進入建立好的本地專案的資料夾下,然後執行如下Git命令:

## 在GitHub建立倉庫時沒有新增README檔案,先建立README.md檔案
touch README.md

## Git初始化
git init
說明:當前目錄執行git初始化,在當前目錄中新增一個.git資料夾來儲存一些git資料

## 新增所有檔案到暫存區
git add *

## 將暫存區內容提交到本地倉庫
git commit -m "專案本地倉庫上傳"

## 連線遠端倉庫(SSH和HTTPS方式都行)
git remote add origin git@github.com:rangdandanfei/git-use-demo.git

## 提交到遠端倉庫
git push -u origin master
說明:
git push 將當前分支推送至遠端同名分支
git push origin [branch-name] 推送本地某分支至遠端某分支
git push -u origin [branch-name] 推送本地某分支至遠端某分支,並跟蹤
複製程式碼

FAQ :

1. 錯誤:error: failed to push some refs to 'github.com/rangdandanf…'

原因:遠端倉庫和本地倉庫不匹配,需要先合併,再push。

詳細的錯誤資訊如下:

$ git push -u origin master
To https://github.com/rangdandanfei/git-using-demo.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/rangdandanfei/git-using-demo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
複製程式碼

解決方法:執行如下命令即可

## 先拉取(pull)遠端倉庫master分支程式碼,與本地進行合併,再將本地倉庫master分支程式碼push到遠端
git pull --rebase origin master
git push -u origin master
複製程式碼

git pull --rebase 命令相關介紹可參考文章:www.cnblogs.com/kevingrace/…

2. 錯誤:git@github.com: Permission denied (publicKey). fatal: Could not read from remote respository.

原因:GitHub的SSH key不存在或者SSH key驗證不通過。

詳細的錯誤資訊如下:

$ git push -u origin master
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
複製程式碼

解決方法:

<1> SSH key不存在,則新建一個SSH key加入GitHub中。

命令總結如下:

  • 開啟Git Bash,先檢查是否已經存在SSH keys,輸入如下命令:

    ls -al ~/.ssh
    
    說明:
    預設的公鑰名為:
    id_dsa.pub
    id_ecdsa.pub
    id_ed25519.pub
    id_rsa.pub
    複製程式碼

    檢查是否存在SSH keys

  • 若不存在SSH key,生成一個新的SSH key,輸入如下命令:

    ssh-kengen -t rsa -b 4096 -c "your_email@example.com"
    複製程式碼

    生成了一個新的SSH key :

    > Generating public/private rsa key pair.
    複製程式碼
  • 當提示"Enter a file in which to save the key." 按Enter,儲存到預設的檔案位置:

    > Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]
    複製程式碼
  • 提示輸入安全密碼,可以直接按回車Enter鍵

    > Enter passphrase (empty for no passphrase): [Type a passphrase]
    > Enter same passphrase again: [Type passphrase again]
    複製程式碼
  • 把SSH key複製到剪貼簿

    $ clip < ~/.ssh/id_rsa.pub
    # Copies the contents of the id_rsa.pub file to your clipboard
    複製程式碼

    也可以開啟C:/Users/you/.ssh/id_rsa檔案,直接複製。

  • 把SSH key加入到GitHub賬戶上

    生成SSH key並新增到GitHub上

GitHub上有詳細的教程,照著一步一步做就OK了,地址:help.github.com/articles/co…

<2> SSH key存在時,檢測SSH連線和使用情況,嘗試切換以HTTPS的方式連線遠端倉庫,若還是不行,則新建一個SSH key。

開啟Git Bash,檢測SSH連線情況,輸入如下命令:

ssh -T git@github.com
複製程式碼

若得到如下提示:

> The authenticity of host 'github.com (IP ADDRESS)' 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)?
或者
> The authenticity of host 'github.com (IP ADDRESS)' can't be established.
  > RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
  > Are you sure you want to continue connecting (yes/no)?
複製程式碼

輸入yes,則看到如下提示,表示SSH連線驗證成功

> Hi username! You've successfully authenticated, but GitHub does not
> provide shell access.
複製程式碼

若得到如下提示:

$ ssh -T git@github.com
> git@github.com: Permission denied (publickey).
複製程式碼

則在(3. 將本地專案上傳到GitHub上)連線遠端倉庫時,嘗試切換以HTTPS的方式連線。

## 連線遠端倉庫(HTTPS方式)
git remote add origin https://github.com/rangdandanfei/git-use-demo.git
複製程式碼

若提示錯誤資訊:fatal: remote origin already exists. 則 :

# 先輸入
git remote rm origin
# 再輸入
git remote add origin https://github.com/rangdandanfei/git-use-demo.git
複製程式碼

檢測SSH key的使用情況,輸入如下命令:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type -1
> debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/you/.ssh/id_dsa type -1
> debug1: identity file /Users/you/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/you/.ssh/id_rsa
> debug1: Trying private key: /Users/you/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

或提示:
$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/you/.ssh/id_rsa
複製程式碼

沒找到SSH key的資訊回顯

找到SSH key的資訊回顯

擴充套件閱讀:

GIT——新建專案並提交遠端倉庫: blog.csdn.net/qq_37049781…

本地專案提交到GitHub: www.jianshu.com/p/4f3151195…

相關文章