远程库的使用

远程库的具体使用方法可以查看2.5 Git 基础 - 远程仓库的使用

使用GitHub作为远程库

情景

我在本地使用Git管理了一个项目,现在想将它托管到GitHub上。
所以只要在GitHub上新建一个库,然后将其作为远程库提交就可以了。

这里需要注意的是,在GitHub上新建的库必须是一个完全空的库,即不能有任何内容 (不能在建库的时候让其自动生成Readme.md等文件)

如果新建的库不是一个完全空的库,则无法将本地的库和远程库进行git merge操作, 也就是说无法将本地库同步到GitHub

假定添加的GitHub上的远程库地址为github_url.git

Spike (master) ProjectCode $ git remote add origin github_url.git
Spike (master) ProjectCode $ git push -u origin master
To github_url.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'github_url.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.

直接git push操作失败,所以先git fetch操作

Spike (master) ProjectCode $ git fetch origin master
warning: no common commits
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
From github_url
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

再手动git merge

Spike (master) ProjectCode $ git merge master origin/master
fatal: refusing to merge unrelated histories

从上面的输出结果可以看到,本地库和远程库被认为是完全不同的库,所以无法git merge

解决

删除远程库,在GitHub上重新建一个完全空的库,按正常流程重新添加即可