最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】把本地的git仓库合并上传到远程某个git仓库

Git crifan 624浏览 0评论

折腾:

【未解决】Gitbook Editor中设置gitbook的文件根目录

后,基本上没法自定义gitbook Editor中创建的book的文件存放的根目录

但是还是想要实现把gitbook的代码都上传到github

则就存在一个问题:

如何把本地已有的gitbook editor创建的book的源码,上传到GitHub上已有的某个仓库

本地git 上传到已有远程仓库

Git将本地仓库上传到远程仓库 – 简书

Git 创建本地仓库并上传到远程仓库 – CSDN博客

将本地已有项目 添加到 git管理中去 – CSDN博客

git 本地项目推送到远程仓库(或者仓库分支)Window版-01 – CSDN博客

gitbook html template

Theming · GitBook Toolchain Documentation

整合 GitHub | GitBook 中文解說 – 2.4

发布到github仓库 | gitbook入门教程

GitbookIO/gitbook: 📝 Modern documentation format and toolchain using Git and Markdown

去试试

先去github上建一个仓库,用于测试合并。

https://github.com/crifan/http_tech_demo_merge.git

很明显,在线的README.md:

和本地的README.md:

是不同的。

然后去试试如何合并。

结果:

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

nothing to commit, working tree clean

➜  httpzhi_shi_zheng_li git:(master) git add remote origin https://github.com/crifan/http_tech_demo_merge.git

fatal: pathspec ‘remote’ did not match any files

➜  httpzhi_shi_zheng_li git:(master) git remote add origin https://github.com/crifan/http_tech_demo_merge.git

➜  httpzhi_shi_zheng_li git:(master) git pull –rebase origin master

warning: no common commits

remote: Counting objects: 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), done.

From https://github.com/crifan/http_tech_demo_merge

* branch            master     -> FETCH_HEAD

* [new branch]      master     -> origin/master

First, rewinding head to replay your work on top of it…

Applying: Initial commit

Using index info to reconstruct a base tree…

Falling back to patching base and 3-way merge…

Auto-merging README.md

CONFLICT (add/add): Merge conflict in README.md

error: Failed to merge in the changes.

Patch failed at 0001 Initial commit

The copy of the patch that failed is found in: .git/rebase-apply/patch

When you have resolved this problem, run “git rebase –continue”.

If you prefer to skip this patch, run “git rebase –skip” instead.

To check out the original branch and stop rebasing, run “git rebase –abort”.

好像是由于:

前面所处于的branch是master?

导致此处:

  • (没想到)从远程master去rebase的pull之后,本地的原先的内容,都被清空了:

  • 并且(可以预见)README.md冲突了

-》此处,要先去想办法,如何恢复:

git pull –rebase origin master

之前的本地的内容

恢复git pull rebase 之前的本地内容

恢复git pull rebase origin master 之前的本地内容

找回Git中丢失的Commit – 简书

    Initial commit

…skipping…

➜  httpzhi_shi_zheng_li git:(master) git status

commit 1fbbcb4539158240c7c2d6d2a303dbaa86b1fed6 (HEAD, origin/master)

Author: Crifan Li <admin@crifan.com>

Date:   Mon Nov 20 17:41:11 2017 +0800

    Initial commit

(END)

所以去恢复试试:

➜  httpzhi_shi_zheng_li git:(1fbbcb4) ✗ git reset –hard 1fbbcb453

HEAD is now at 1fbbcb4 Initial commit

结果更惨,本地的连之前新建的chapter1等文件都没了:

算了。放弃目前这个demo。

再去重新用Gitbook,弄点内容出来,用于再去测试合并。

不过有意思的是:

貌似gitbook自己有自己的一套git管理,所以再去用gitbook打开时,自动就恢复了之前的内容:

命令行中是:

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

You are currently rebasing branch ‘master’ on ‘1fbbcb4’.

  (all conflicts fixed: run “git rebase –continue”)

nothing to commit, working tree clean

去试试:

➜  httpzhi_shi_zheng_li git:(master) git pull remote orgin

fatal: ‘remote’ does not appear to be a git repository

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

➜  httpzhi_shi_zheng_li git:(master) git pull remote origin

fatal: ‘remote’ does not appear to be a git repository

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

➜  httpzhi_shi_zheng_li git:(master) git branch –set-upstream-to=origin/master master

Branch master set up to track remote branch master from origin.

➜  httpzhi_shi_zheng_li git:(master) git pull remote origin

fatal: ‘remote’ does not appear to be a git repository

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

还是不行。

➜  httpzhi_shi_zheng_li git:(master) git remote add origin https://github.com/crifan/http_tech_demo_merge.git

fatal: remote origin already exists.

➜  httpzhi_shi_zheng_li git:(master) git pull

fatal: refusing to merge unrelated histories

➜  httpzhi_shi_zheng_li git:(master) git pull origin master

From https://github.com/crifan/http_tech_demo_merge

* branch            master     -> FETCH_HEAD

fatal: refusing to merge unrelated histories

fatal: refusing to merge unrelated histories

git pull fatal: refusing to merge unrelated histories

rebase – Git refusing to merge unrelated histories – Stack Overflow

–allow-unrelated-histories

➜  httpzhi_shi_zheng_li git:(master) git pull origin master –allow-unrelated-histories

From https://github.com/crifan/http_tech_demo_merge

* branch            master     -> FETCH_HEAD

Auto-merging README.md

CONFLICT (add/add): Merge conflict in README.md

Automatic merge failed; fix conflicts and then commit the result.

好像是我所需要的:

本地内容不动,把远程的内容合并进来

(远程和本地都有个README.md,所以冲突了,是正常的,希望看到的)

然后修改掉冲突:

然后去提交,貌似(除了rebase的提示,其他)基本上都正常了:

➜  httpzhi_shi_zheng_li git:(master) git commit -m “1. has merge remote to local”

[master 044d9a6] 1. has merge remote to local

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

Your branch is ahead of ‘origin/master’ by 25 commits.

  (use “git push” to publish your local commits)

You are currently rebasing branch ‘master’ on ‘1fbbcb4’.

  (all conflicts fixed: run “git rebase –continue”)

nothing to commit, working tree clean

➜  httpzhi_shi_zheng_li git:(master) git push

Counting objects: 90, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (85/85), done.

Writing objects: 100% (90/90), 1.77 MiB | 311.00 KiB/s, done.

Total 90 (delta 35), reused 0 (delta 0)

remote: Resolving deltas: 100% (35/35), done.

To https://github.com/crifan/http_tech_demo_merge.git

   1fbbcb4..044d9a6  master -> master

去看在线的内容,则是已经合并的了:

但是还是要去解决掉:

You are currently rebasing branch ‘master’ on ‘1fbbcb4’.

  (all conflicts fixed: run “git rebase –continue”)

git You are currently rebasing branch ‘master’ on

git – rebase in progress. Can not commit. How to proceed or stop (abort)? – Stack Overflow

此处对于rebase,不需要了,所以去试试git rebase –abort:

➜  httpzhi_shi_zheng_li git:(master) git rebase –abort

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

Your branch is behind ‘origin/master’ by 5 commits, and can be fast-forwarded.

  (use “git pull” to update your local branch)

nothing to commit, working tree clean

➜  httpzhi_shi_zheng_li git:(master) git pull

Updating b655c64..044d9a6

Fast-forward

README.md | 4 ++++

1 file changed, 4 insertions(+)

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

Your branch is up-to-date with ‘origin/master’.

nothing to commit, working tree clean

然后就可以了。

后来继续去试了试,在用Gitbook打开编辑文件到同时,在终端中看看结果,则是:

gitbook编辑和保存的同时:

就自动commit提交到本地仓库了

所以终端中查看状态是ahead了8个commit:

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

Your branch is ahead of ‘origin/master’ by 8 commits.

  (use “git push” to publish your local commits)

nothing to commit, working tree clean

所以此处直接在终端中提交即可把最新代码上传到远程服务器中:

➜  httpzhi_shi_zheng_li git:(master) git push

Counting objects: 25, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (24/24), done.

Writing objects: 100% (25/25), 2.56 KiB | 1.28 MiB/s, done.

Total 25 (delta 16), reused 0 (delta 0)

remote: Resolving deltas: 100% (16/16), completed with 3 local objects.

To https://github.com/crifan/http_tech_demo_merge.git

   044d9a6..fa6df35  master -> master

➜  httpzhi_shi_zheng_li git:(master) git status

On branch master

Your branch is up-to-date with ‘origin/master’.

nothing to commit, working tree clean

->所以,在把本地集成到远程后,则后续操作的逻辑是:

尽量在Gitbook中编辑文章,保存-》则会自动commit到local的仓库

然后在terminal中push即可提交到远程仓库。

【总结】

此处,把本地已有的代码,去合并到远程仓库的逻辑是:

1.把远程仓库地址添加到remote的origin

git remote add origin https://github.com/crifan/http_tech_demo_merge.git

2.不知道此处的设置默认的origin/master为master是否有效

git branch –set-upstream-to=origin/master master

3.获取远程仓库的内容(到本地)

git pull remote origin

如果有冲突,则合并处理冲突。

4.如果需要的话,正常修改本地内容,再commit到本地

git commit -m “xxx”

5.提到本地内容,合并到远程仓库

git pull

【后记】

然后再去把真实的本地的gitbook的代码合并到在线远程仓库

结果出错:

➜  htttp_summary git:(master) git add remote origin https://github.com/crifan/http_summary.git

fatal: pathspec ‘remote’ did not match any files

➜  htttp_summary git:(master) git add remote origin https://github.com/crifan/http_summary.git

fatal: pathspec ‘remote’ did not match any files

➜  htttp_summary git:(master) git branch –set-upstream-to=origin/master master

error: the requested upstream branch ‘origin/master’ does not exist

hint:

hint: If you are planning on basing your work on an upstream

hint: branch that already exists at the remote, you may need to

hint: run “git fetch” to retrieve it.

hint:

hint: If you are planning to push out a new local branch that

hint: will track its remote counterpart, you may want to use

hint: “git push -u” to set the upstream config as you push.

然后还是要去搞懂逻辑才行。

结果是之前顺序搞错了,改为:

git remote add origin https://github.com/crifan/http_summary.git

即可。

➜  htttp_summary git:(master) git pull remote origin

fatal: ‘remote’ does not appear to be a git repository

fatal: Could not read from remote repository.

Please make sure you have the correct access rights

and the repository exists.

➜  htttp_summary git:(master) git branch –set-upstream-to=origin/master master

error: the requested upstream branch ‘origin/master’ does not exist

hint:

hint: If you are planning on basing your work on an upstream

hint: branch that already exists at the remote, you may need to

hint: run “git fetch” to retrieve it.

hint:

hint: If you are planning to push out a new local branch that

hint: will track its remote counterpart, you may want to use

hint: “git push -u” to set the upstream config as you push.

然后还是不行:

➜  htttp_summary git:(master) git pull origin master

fatal: Couldn’t find remote ref master

fatal: Couldn’t find remote ref master

github常见操作和常见错误!错误提示:fatal: remote origin already exists. – CSDN博客

➜  htttp_summary git:(master) git pull origin master

fatal: Couldn’t find remote ref master

➜  htttp_summary git:(master) git remote add origin https://github.com/crifan/http_summary.git

fatal: remote origin already exists.

➜  htttp_summary git:(master) git pull origin master

fatal: Couldn’t find remote ref master

(1)git分支管理 – SegmentFault

Github常见操作和常见错误! – 简书

git pull fatal: Couldn’t find remote ref master

git pull displays “fatal: Couldn’t find remote ref refs/heads/xxxx” and hangs up – Stack Overflow

➜  htttp_summary git:(master) cat .git/config

[core]

    repositoryformatversion = 0

    filemode = true

    bare = false

    logallrefupdates = true

    ignorecase = true

    precomposeunicode = true

[user]

[remote “origin”]

    url = https://github.com/crifan/http_summary.git

    fetch = +refs/heads/*:refs/remotes/origin/*

git 本地 同步到 远程

3.2 保持同步 · geeeeeeeeek/git-recipes Wiki

Git – 远程仓库的使用

远程仓库 | Harttle Land

添加远程库 – 廖雪峰的官方网站

直接去试试:

git push origin master

➜  htttp_summary git:(master) git push origin master

Counting objects: 748, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (736/736), done.

Writing objects: 100% (748/748), 3.98 MiB | 129.00 KiB/s, done.

Total 748 (delta 443), reused 0 (delta 0)

remote: Resolving deltas: 100% (443/443), done.

To https://github.com/crifan/http_summary.git

* [new branch]      master -> master

➜  htttp_summary git:(master) git status

On branch master

nothing to commit, working tree clean

再去在线仓库看看:

【总结】

所以此处对于:

本地git仓库已有很多内容

在线新建了git仓库,基本上是没有README.md后者是README.md只有一点点内容,其他都是空的

所以要把本地内容上传到远程仓库,其实只需要很简单的:

git remote add origin https://github.com/crifan/http_summary.git

git push origin master

就可以了。

转载请注明:在路上 » 【已解决】把本地的git仓库合并上传到远程某个git仓库

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
57 queries in 0.267 seconds, using 12.02MB memory