一、準備工作
- github賬号一枚
- 安裝過git
brew install git mac電腦使用終端安裝git
- 能夠通路gitHub網址
有些網絡不知道為何通路不了,比如說我現在的網絡,隻能開了vpn才能通路。。。
二、GitHub上建立項目
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLw0kaOJzYE9EMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL0IDOzQDMzIjM5ETMxkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
建立完成,這邊沒啥說明的,就按照流程走就OK了的
三、ssh關聯
- 首先先檢視ssh是否建立過
ls -al ~/.ssh
ps:主要檔案是
id_rsa.pub
id_rsa
1.ssh無建立過情況,新增一個ssh
tianledeMacBook-Pro:.ssh xxx$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxx/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:GclQHufCH02UArsAyeYiWvEsE11aOzLQqw6CLOu8F7Y [email protected]
The key's randomart image is:
+---[RSA 4096]----+
| .+.o+.+...o. |
| o.*+ * *.o. |
| B+.+ O o.. |
|. = =o o * . |
|+o = S . |
|*..o |
|o+. o |
|o .E |
|.+o |
+----[SHA256]-----+
ps:
1、郵箱改為自己的郵箱
2、輸入完ssh-keygen -t rsa -b 4096 -C "[email protected]"
一路回車!一路回車!一路回車!不要設定密碼,不然後面後面每次push和pull都會提示輸入密碼,會相當的麻煩
3、4096指的是密鑰的長度,也可以是2048,肯定是越長越安全喽
- 複制id_rsa.pub裡ssh key的值,也就是檔案裡的一大串,并在GitHub上綁定ssh
2.ssh有建立過情況
- 檢視ssh使用情況,如原來的需要用在别的地方則可以使用https方式去連接配接遠端倉庫,要是還是不行,就删除原來的ssh,重新建立一個
GitHub建立項目并關聯至本地項目
ssh -T [email protected]
ps:
由于我這邊是重新建立的ssh檔案,是以這邊提示成功
三、将本地項目上傳綁定至遠端倉庫
tianledeMacBook-Pro:yongjiatong-parent xxx$ touch README.md
tianledeMacBook-Pro:yongjiatong-parent xxx$ git init
Reinitialized existing Git repository in /java/yongjiatong/yongjiatong-parent/.git/
tianledeMacBook-Pro:yongjiatong-parent xxx$ git add *
tianledeMacBook-Pro:yongjiatong-parent xxx$ git commit -m "項目本地倉庫上傳"
[master (root-commit) 01442f6] 項目本地倉庫上傳
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
tianledeMacBook-Pro:yongjiatong-parent xxx$ git remote add origin [email protected]:zjh793666062/yongjiatong-parent.git
tianledeMacBook-Pro:yongjiatong-parent xxx$ git push -u origin master
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
To github.com:zjh793666062/yongjiatong-parent.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:zjh793666062/yongjiatong-parent.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.
tianledeMacBook-Pro:yongjiatong-parent xxx$ git pull --rebase origin master
warning: no common commits
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:zjh793666062/yongjiatong-parent
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: 項目本地倉庫上傳
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging README.md
No changes -- Patch already applied.
tianledeMacBook-Pro:yongjiatong-parent xxx$ git push -u origin master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Everything up-to-date
## 在GitHub建立倉庫時沒有添加README檔案,先建立README.md檔案
touch README.md
## Git初始化
git init
## 将檔案存至暫存區
git add *
## 将暫存區内容送出到本地倉庫
git commit -m "項目本地倉庫上傳"
## 連接配接遠端倉庫(SSH和HTTPS方式都行)
## 使用ssh方式
git remote add origin [email protected]:zjh793666062/yongjiatong-parent.git
## 使用https方式
git remote add origin https://github.com/zjh793666062/yongjiatong-parent.git
## 送出到遠端倉庫
git push -u origin master
ps:
git push 将目前分支推送至遠端同名分支
git push origin [branch-name] 推送本地某分支至遠端某分支
git push -u origin [branch-name] 推送本地某分支至遠端某分支,并跟蹤
## 這邊會先報錯,原因是遠端倉庫和本地倉庫不比對,需要先合并,再push
## 先拉取遠端倉庫master分支,再将本地倉庫master分支代碼push到遠端
git pull --rebase origin master
git push -u origin master
這樣就完成了GitHub建立項目并關聯至本地項目的動作,有問題可以随時留言哦。
下面附上官方的中文說明文檔與官方的SSH綁定說明文檔位址
官方說明文檔
SSH綁定說明文檔位址