如果是第一次連接github,需要添加SSH KEY
添加SSHKEY
- 第一步揭厚,先在你的電腦上生成 ssh key
huanyu@ubuntu:~/.ssh$ ssh-keygen
需要注意的是齿椅,要進入到 .ssh 目錄下戈轿,中間要輸入key的名稱,就按圖中的來就可以
- 第二步宛官,把key 添加到 ssh 中
huanyu@ubuntu:~/.ssh$ ssh-add id_rsa
- 第三步葫松,把ssh公鑰添加到github上
huanyu@ubuntu:~/.ssh$ cat id_rsa.pub
把下面這一長串碼復制
在github上找到setting,點擊
進入 SSH and GPG keys
image.png
把復制的碼粘貼到下面這里
- 測試本地與github連接是否成功
huanyu@ubuntu:~/.ssh$ ssh -T git@github.com
看到下面的信息說明配置成功了
把項目添加到github上
- 第一步:先到github建立一個空的倉庫
不要初始化readme文件底洗,真的很麻煩
不要選那個勾腋么,直接創(chuàng)建倉庫
創(chuàng)建好之后它會告訴你怎么把本地的項目添加到github上
- 第二步,這時可以去到你所在項目的文件夾亥揖,把你項目的文件夾初始化為一個git本地倉庫
git init
把你的項目添加到git倉庫中
git add . // . 表示當前目錄下的所有文件夾
git commit -m "first commit" //這個是注釋說明
- 第三步珊擂,關(guān)聯(lián)本地倉庫與github
git remote add origin git@github.com:HarryHq/homework.git
- 第四步,把本地倉庫的文件上傳到github上
git push -u origin master
第二種情況
在github上創(chuàng)建新倉庫的時候费变,勾選了初始化README文件
執(zhí)行到下面語句的時候會出現(xiàn)不一樣的情況
huanyu@ubuntu:~/Downloads/love/love$ git push -u origin master
To github.com:HarryHq/Lovepage.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:HarryHq/Lovepage.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.
hint: (e.g., 'git pull ...') before pushing again.
按照提示摧扇,執(zhí)行以下代碼
huanyu@ubuntu:~/Downloads/love/love$ git pull
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:HarryHq/Lovepage
* [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
git branch --set-upstream-to=origin/<branch> master
按照提示,執(zhí)行以下代碼
huanyu@ubuntu:~/Downloads/love/love$ git branch --set-upstream-to=origin/master master
Branch 'master' set up to track remote branch 'master' from 'origin'.
現(xiàn)在可以試一下上傳到github了
huanyu@ubuntu:~/Downloads/love/love$ git pull
fatal: refusing to merge unrelated histories
原因是: 兩個 根本不相干的 git 庫挚歧, 一個是本地庫扛稽, 一個是遠端庫, 然后本地要去推送到遠端滑负, 遠端覺得這個本地庫跟自己不相干在张, 所以告知無法合并
- 解決辦法,強制合并
huanyu@ubuntu:~/Downloads/love/love$ git pull origin master --allow-unrelated-histories
From github.com:HarryHq/Lovepage
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
然后再把本地項目上傳到github上矮慕,這時就成功了
huanyu@ubuntu:~/Downloads/love/love$ git push -u origin master
Counting objects: 17, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (17/17), 64.86 KiB | 5.40 MiB/s, done.
Total 17 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:HarryHq/Lovepage.git
d899f47..3719996 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
因為第二種操作比較麻煩帮匾,所以建議用第一種辦法。