一挎狸、準(zhǔn)備工作
- github賬號(hào)一枚
- 安裝過(guò)git
brew install git mac電腦使用終端安裝git
- 能夠訪(fǎng)問(wèn)gitHub網(wǎng)址
有些網(wǎng)絡(luò)不知道為何訪(fǎng)問(wèn)不了,比如說(shuō)我現(xiàn)在的網(wǎng)絡(luò)断楷,只能開(kāi)了vpn才能訪(fǎng)問(wèn)锨匆。。冬筒。
二恐锣、GitHub上創(chuàng)建項(xiàng)目
創(chuàng)建完成,這邊沒(méi)啥說(shuō)明的舞痰,就按照流程走就OK了的
三土榴、ssh關(guān)聯(lián)
- 首先先查看ssh是否創(chuàng)建過(guò)
image
ls -al ~/.ssh
ps:主要文件是
id_rsa.pub
id_rsa
1.ssh無(wú)創(chuàng)建過(guò)情況,新增一個(gè)ssh
tianledeMacBook-Pro:.ssh xxx$ ssh-keygen -t rsa -b 4096 -C "xxxxx@xx.com"
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 xxxxxx@xx.com
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 "xxxxx@xx.com"
一路回車(chē)!一路回車(chē)呀打!一路回車(chē)矢赁!不要設(shè)置密碼,不然后面后面每次push和pull都會(huì)提示輸入密碼贬丛,會(huì)相當(dāng)?shù)穆闊?3撩银、4096指的是密鑰的長(zhǎng)度,也可以是2048豺憔,肯定是越長(zhǎng)越安全嘍
- 復(fù)制id_rsa.pub里ssh key的值额获,也就是文件里的一大串够庙,并在GitHub上綁定ssh
2.ssh有創(chuàng)建過(guò)情況
- 查看ssh使用情況,如原來(lái)的需要用在別的地方則可以使用https方式去連接遠(yuǎn)程倉(cāng)庫(kù)咪啡,要是還是不行首启,就刪除原來(lái)的ssh,重新新建一個(gè)
ssh -T git@github.com
ps:
由于我這邊是重新創(chuàng)建的ssh文件撤摸,所以這邊提示成功
三、將本地項(xiàng)目上傳綁定至遠(yuǎn)程倉(cāng)庫(kù)
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 "項(xiàng)目本地倉(cāng)庫(kù)上傳"
[master (root-commit) 01442f6] 項(xiàng)目本地倉(cāng)庫(kù)上傳
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
tianledeMacBook-Pro:yongjiatong-parent xxx$ git remote add origin git@github.com: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 'git@github.com: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: 項(xiàng)目本地倉(cāng)庫(kù)上傳
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創(chuàng)建倉(cāng)庫(kù)時(shí)沒(méi)有添加README文件褒纲,先創(chuàng)建README.md文件
touch README.md
## Git初始化
git init
## 將文件存至?xí)捍鎱^(qū)
git add *
## 將暫存區(qū)內(nèi)容提交到本地倉(cāng)庫(kù)
git commit -m "項(xiàng)目本地倉(cāng)庫(kù)上傳"
## 連接遠(yuǎn)程倉(cāng)庫(kù)(SSH和HTTPS方式都行)
## 使用ssh方式
git remote add origin git@github.com:zjh793666062/yongjiatong-parent.git
## 使用https方式
git remote add origin https://github.com/zjh793666062/yongjiatong-parent.git
## 提交到遠(yuǎn)程倉(cāng)庫(kù)
git push -u origin master
ps:
git push 將當(dāng)前分支推送至遠(yuǎn)程同名分支
git push origin [branch-name] 推送本地某分支至遠(yuǎn)程某分支
git push -u origin [branch-name] 推送本地某分支至遠(yuǎn)程某分支准夷,并跟蹤
## 這邊會(huì)先報(bào)錯(cuò),原因是遠(yuǎn)程倉(cāng)庫(kù)和本地倉(cāng)庫(kù)不匹配莺掠,需要先合并衫嵌,再push
## 先拉取遠(yuǎn)程倉(cāng)庫(kù)master分支,再將本地倉(cāng)庫(kù)master分支代碼push到遠(yuǎn)程
git pull --rebase origin master
git push -u origin master
這樣就完成了GitHub創(chuàng)建項(xiàng)目并關(guān)聯(lián)至本地項(xiàng)目的動(dòng)作彻秆,有問(wèn)題可以隨時(shí)留言哦楔绞。
下面附上官方的中文說(shuō)明文檔與官方的SSH綁定說(shuō)明文檔地址