2 多版本库/分支间交互
git push
和
git pull
分别向目标版本仓库推送来拉回版本。其中
git pull
是
git fetch
和
git merge
的简写。
场景设置
保持前文单人场景下的增量模式完成时的版本库,记为Repo1,
git clone
复制Repo1,生成Repo2。在Repo2中修改使得Repo2的结构为:
$cat readme
This line will be the first commit.
This line will be the second commit.
This line will be commit and push to another repository.
This line will be staged but not commit in the downstream.
This line will not be staged in the downstream.
$git status
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: readme
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme
$git diff readme
diff --git a/readme b/readme
index 6deb4b7..d83d8ae 100644
--- a/readme
+++ b/readme
@@ -2,3 +2,4 @@ This line will be the first commit.
This line will be the second commit.
This line will be commit and push to another repository.
This line will be staged but not commit in the downstream.
+This line will not be staged in the downstream.
$git diff HEAD readme
diff --git a/readme b/readme
index e089083..d83d8ae 100644
--- a/readme
+++ b/readme
@@ -1,3 +1,5 @@
This line will be the first commit.
This line will be the second commit.
This line will be commit and push to another repository.
+This line will be staged but not commit in the downstream.
+This line will not be staged in the downstream.
向目标版本库递交
向目标版本库递交当前版本库成功的前提是当前版本库的commit列表必须包含目标版本库中的所有commit并领先于目标版本库。本地被提交版本库需要更改接收权限。
$git config receive.denyCurrentBranch warn
$git push --set-upstream /home/USER/git_learn/copyed_original_repository/virtual_project master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 564 bytes | 564.00 KiB/s, done.
Total 6 (delta 1), reused 0 (delta 0)
remote: warning: updating the current branch
To /home/USER/git_learn/copyed_original_repository/virtual_project
c03032e..2a89d14 master -> master
Branch 'master' set up to track remote branch 'master' from '/home/USER/git_learn/copyed_original_repository/virtual_project'.
此时Repo1的结构为:
$cat readme
This line will be the first commit.
This line will be the second commit.
This line will be staged but not commited.
This line just modified in the workong directory.
$git status readme
On branch master
Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: readme
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme
$git diff readme
diff --git a/readme b/readme
index 7d4a151..7010dbf 100644
--- a/readme
+++ b/readme
@@ -1,3 +1,4 @@
This line will be the first commit.
This line will be the second commit.
This line will be staged but not commited.
+This line just modified in the workong directory.
$git diff HEAD readme
diff --git a/readme b/readme
index e089083..7010dbf 100644
--- a/readme
+++ b/readme
@@ -1,3 +1,4 @@
This line will be the first commit.
This line will be the second commit.
-This line will be commit and push to another repository.
+This line will be staged but not commited.
+This line just modified in the workong directory.
可以看到Repo1在接收到Repo2的push后,工作区和暂存区的内容未发生变化,维持原状;但HEAD指向的版本库已经发生了变化,此时的版本库中的内容有Repo2中的三行。即是说push操作仅发生在发生相互作用的两个版本库之间,两者工作区和暂存区的文件不受影响。当被提交的版本库的工作区和暂存区为干净状态时,push仍为该种表现形式。
从目标版本库拉回
恢复场景设置,现在尝试Repo1从Repo2 pull拉取修改。pull成功实现的前提是当前分支Repo1的工作区和暂存区干净,当工作区和暂存区存在信息时如下报错:
$git pull /home/USER/git_learn/original_repository/virtual_project
From /home/USER/git_learn/original_repository/virtual_project
* branch HEAD -> FETCH_HEAD
error: Your local changes to the following files would be overwritten by merge:
readme
Please commit your changes or stash them before you merge.
Aborting
对Repo1进行处理,暂存区信息向前提交,工作区信息回退,此时Repo1领先Repo2有一个版本点的差异,产生冲突时的行为如下:
$git commit -m "commmit for staged"
$git restore readme
重新执行拉取操作
$git pull /home/USER/git_learn/original_repository/virtual_project
From /home/USER/git_learn/original_repository/virtual_project
* branch HEAD -> FETCH_HEAD
Auto-merging readme
CONFLICT (content): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
显示冲突,此时Repo1中各文件的状态为:
$git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: readme
no changes added to commit (use "git add" and/or "git commit -a")
$cat readme
This line will be the first commit.
This line will be the second commit.
<<<<<=======
This line will be commit and push to another repository.
>>>>>>> 2a89d1499b585614d7660f007fba48a1f2eb5b3b
$git diff readme
diff --cc readme
index 7d4a151,e089083..0000000
--- a/readme
+++ b/readme
@@@ -1,3 -1,3 +1,7 @@@
This line will be the first commit.
This line will be the second commit.
++<<<<<< +This line will be staged but not commited.
++=======
+ This line will be commit and push to another repository.
++>>>>>>> 2a89d1499b585614d7660f007fba48a1f2eb5b3b
$git diff HEAD readme
diff --git a/readme b/readme
index 7d4a151..fc82f78 100644
--- a/readme
+++ b/readme
@@ -1,3 +1,7 @@
This line will be the first commit.
This line will be the second commit.
+<<<<<< This line will be staged but not commited.
+=======
+This line will be commit and push to another repository.
+>>>>>>> 2a89d1499b585614d7660f007fba48a1f2eb5b3b
从中可以看到:
-
工作区的文件发生了变化,增加了git对当前分支来远程分支差异的描述
-
-
暂存区的内容发生了变化,暂存区中现在只包含Repo1和Repo2的版本库中的公共部分,而不包含冲突部分
虽然此时Repo1的暂存区相对于版本库不是干净的,但处理被保护状态,是无法执行commit操作的:
$git commit -m "aaaa"
U readme
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm '
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
在工作区中做修改后暂存提交:
$cat readme
This line will be the first commit.
This line will be the second commit.
This line will be staged but not commited.
This line will be commit and push to another repository.
$git log
commit bfb7391b4c89e5bf9e79ac6d7ddfe60e23a0edc6 (HEAD -> master)
Merge: c0c3f64 2a89d14
Author: wyj1
Date: Wed Dec 4 11:02:51 2019 +0800
commit after pull from Repo2
commit c0c3f64e05477656383490a2d9dabff0c9480391
Author: wyj1
Date: Tue Dec 3 17:29:47 2019 +0800
commmit for staged
commit 2a89d1499b585614d7660f007fba48a1f2eb5b3b
Merge: 2ebca46 c03032e
Author: wyj1
Date: Fri Nov 29 09:42:06 2019 +0800
merge from upstream
commit 2ebca46b25718eb5f2aba6466ae4704233666904
Author: wyj1
Date: Thu Nov 28 19:01:19 2019 +0800
commit for push
commit c03032e5403cf4b96e30d217e0c39d19d9e92ad1
Author: wyj1
Date: Thu Nov 28 16:07:28 2019 +0800
second commit
commit 417709fbbc5b3d1f70d24efa2b54edc7f6d3cdbb
此时log记录中包含了repo2中所做的改动提交,流程完成。
当Repo1 不领先Repo2时,拉取操作可直接完成,Repo1中的工作区、暂存区和版本库同时改变,Repo2中的提交记录被合并了进来。
$cat readme
This line will be the first commit.
This line will be the second commit.
$git status
On branch master
nothing to commit, working tree clean
$git pull /home/USER/git_learn/original_repository/virtual_project
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.