将本地的 git 仓库连接到远程仓库,可以有两种做法:
D:\work>git clone [email protected]:sword2000/testJava.git
Cloning into 'testJava'...
remote: Enumerating objects: 2174, done.
remote: Counting objects: 100% (2174/2174), done.
remote: Compressing objects: 100% (1612/1612), done.
remote: Total 2174 (delta 479), reused 2171 (delta 479)iB/s
Receiving objects: 100% (2174/2174), 100.31 MiB | 1.88 MiB/s, done.
Resolving deltas: 100% (479/479), done.
Checking out files: 100% (2220/2220), done.
此时自动建立好了本地的 git 仓库,并与 clone 的远程仓库自动建立了连接。
D:\work\testJava>git remote -v
origin [email protected]:sword2000/testJava.git (fetch)
origin [email protected]:sword2000/testJava.git (push)
然后,再将本地的文件复制到 clone 得到的目录下,就可以继续开发工作了。
如果已经在本地创建了本地仓库,并做过提交:
D:\work\test>git init
Initialized empty Git repository in D:/work/test/.git/
D:\work\test>git add *
D:\work\test>git commit -m "initial commit"
[master (root-commit) f717e3e] initial commit
2219 files changed, 801730 insertions(+)
create mode 100644 .gradle/3.3/taskArtifacts/taskArtifacts.lock
create mode 100644 bin/memorydbmanager.html
.......
然后,关联到远程仓库 [email protected]:sword2000/testJava.git
D:\work\test>git remote add origin [email protected]:sword2000/testJava.git
D:\work\test>git remote -v
origin [email protected]:sword2000/testJava.git (fetch)
origin [email protected]:sword2000/testJava.git (push)
然后,push 本地仓库到远程仓库,出现问题1:
D:\work\test>git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
直接 push 是不行的,因为本地仓库还没有设置过 push 的时候,push从本地的哪个分支到远程的哪个分支,按提示用 --set-upstream 设置一下,但这时出现问题2:
D:\work\test>git push --set-upstream origin master
To gitee.com:sword2000/testJava.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:sword2000/testJava.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
设置本地分支 master 到远程分支 origin 的时候,远程仓库任务两者不一致,不能直接提交,否则会覆盖掉远程分支,需要首先将远程仓库分支 pull 一下,这是出现问题 3:
D:\work\test>git pull origin master
From gitee.com:sword2000/testJava
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
远程仓库认为两个仓库没有任何关系,不接受 pull。当然,两个仓库是分别各自创建的,的确各不相干,因此,在 pull 的时候,加一个参数:–allow-unrelated-histories 即使是两者不相干,我们也允许 pull 到本地来:
D:\work\test>git pull origin master --allow-unrelated-histories
From gitee.com:sword2000/testJava
* branch master -> FETCH_HEAD
Merge made by the 'recursive' strategy.
.gitignore | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
create mode 100644 .gitignore
现在,可以 push 了:
D:\work\test>git push -u origin master
Enumerating objects: 2172, done.
Counting objects: 100% (2172/2172), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2089/2089), done.
Writing objects: 100% (2171/2171), 100.31 MiB | 1.31 MiB/s, done.
Total 2171 (delta 479), reused 0 (delta 0)
remote: Resolving deltas: 100% (479/479), done.
remote: Powered By Gitee.com
To gitee.com:sword2000/testJava.git
4ff0d63..331a223 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
这时候,看 git log:
D:\work\test>git log
commit 331a2239dbc2fede08e1ddaacc31621ab2aed068 (HEAD -> master, origin/master)
Merge: f717e3e 4ff0d63
Author: haojc
Date: Sun Aug 18 17:32:03 2019 +0800
Merge branch 'master' of gitee.com:sword2000/testJava
commit f717e3ee7f6fa6d46f54491d931f55e319ee3346
Author: haojc
Date: Sun Aug 18 17:10:29 2019 +0800
initial commit
commit 4ff0d63621a3b72b8d7fdaac8cbf1c4b1525a01f
Author: sword2000
Date: Sun Aug 18 11:58:31 2019 +0800
Initial commit
可以看到有两次 initial commit,是因为有一次是远程仓库做的,另一次是本地仓库做的。另外就是本次 commit 的内容,两个仓库的 log 合并在了一起。