您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
33 把本地仓库同步到GitHub
发布时间:2020-07-25 17:23:07编辑:雪饮阅读()
添加远端
这里要将上次本地仓库上传到远程仓库中,那么首先就是要先在本地添加新的远端,这里以ssh方式为例,则
点击这里复制ssh地址
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git remote add github git@github.com:xueyin220807/moral.git
把本地所有分支推到远端
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git push github --all
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/[fingerprint])? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Enumerating objects: 64, done.
Counting objects: 100% (64/64), done.
Delta compression using up to 12 threads
Compressing objects: 100% (53/53), done.
Writing objects: 100% (64/64), 314.50 KiB | 612.00 KiB/s, done.
Total 64 (delta 10), reused 4 (delta 1), pack-reused 0
remote: Resolving deltas: 100% (10/10), done.
To github.com:xueyin220807/moral.git
* [new branch] branch2 -> branch2
* [new branch] branch3 -> branch3
* [new branch] newBranch1 -> newBranch1
* [new branch] temp -> temp
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:xueyin220807/moral.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.
这里会发现除过master分支其它分支都成功推送上去了。
毕竟我们本地其它分支的确是远端没有的,而master分支则和远端冲突,远端也存在这个master分支。
按照这里的提示,我们这里先单独从远程fetch下这个master。
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git fetch github master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1.18 KiB | 86.00 KiB/s, done.
From github.com:xueyin220807/moral
* branch master -> FETCH_HEAD
* [new branch] master -> github/master
然后我们会发现远端这个master和本地的不是在一个根上面的
那么接下来我们用合并,将这两个master合并到同一个根上
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git checkout master
Already on 'master'
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git merge github/master
fatal: refusing to merge unrelated histories
我们发现在合并时候又报错了,意思是拒绝合并没有关联的历史
那么我们就得允许合并这些不相干的才能合并
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git merge --allow-unrelated-histories github/master
Merge made by the 'recursive' strategy.
LICENSE | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 LICENSE
注意:上面命令执行后中间有一个交互,是让输入合并理由的,可以直接退出不用填写的。
然后我们再来看看此时的git树
发现远端和本地端共用了一个父亲,接下来我们看看我们github的提交
看来还是只有一个提交,因为github这个提交是我们上次创建的新仓库时默认的,也就是说我们的本地的master并没有上去
上面我们只是合并了,但是没有重新推送,所以我们需要重新推送到远端
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/web (branch3|REBASE 4/5)
$ git push github master
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 308 bytes | 308.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:xueyin220807/moral.git
a3c51ad..d57d96e master -> master
推送成功之后我们再来看看
Ok任务完成
关键字词:git,github,本地,远端,fetch,push,merge,不相干,历史
上一篇:31 配置公私钥