各人根據實際狀況建立本身的項目(我在IntelliJ IDEA中建立了SpringBoot項目)html
先cd進入建立好的本地項目的文件夾下,而後執行以下Git命令: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] 推送本地某分支至遠程某分支,並跟蹤
複製代碼
緣由:遠程倉庫和本地倉庫不匹配,須要先合併,再push。github
詳細的錯誤信息以下:shell
$ 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/…bash
緣由:GitHub的SSH key不存在或者SSH key驗證不經過。ssh
詳細的錯誤信息以下:ide
$ 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.
複製代碼
解決方法:fetch
<1> SSH key不存在,則新建一個SSH key加入GitHub中。ui
命令總結以下:
打開Git Bash,先檢查是否已經存在SSH keys,輸入以下命令:
ls -al ~/.ssh
說明:
默認的公鑰名爲:
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub
複製代碼
若不存在SSH key,生成一個新的SSH key,輸入以下命令:
ssh-keygen -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帳戶上
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
複製代碼
GIT——新建項目並提交遠程倉庫: blog.csdn.net/qq_37049781…
本地項目提交到GitHub: www.jianshu.com/p/4f3151195…