您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
34 不同人修改了不同文件如何...
发布时间:2020-07-26 17:01:00编辑:雪饮阅读()
这次模拟下不同人修改了同一个分支的不同问件的处理方式。
创建测试分支
我们需要在github上之前的仓库上建立一个测试分支,比如test34
在这里输入后回车即可创建,接下来我们需要建立两个客户端,我这里假定两个客户端分别是window和linux,而第一个是window第二个是linux,由于window之前就一直用着,所以配置公私钥方面等就都忽略过去了。两个客户端靠的是公私钥来区分的。
建立第二个客户端
生成公钥
[root@localhost ~]# ssh-keygen -t rsa -b 4096 -C "1509272976@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
53:00:9d:52:65:ed:d1:1f:b0:30:1f:7d:94:66:df:bf 1509272976@qq.com
The key's randomart image is:
+--[ RSA 4096]----+
| .+ooo+ +o.o|
| . oo * +*.|
| . .. +o.=|
| . . +|
| S .|
| . .|
| .|
| E |
| |
+-----------------+
[root@localhost ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCyykH4ipThPcwoyf3aeWBAoScsFPXjbilRwn9sFqqVliYAmLoKzBlV010VEq3 6W2elL9Uy9I5It1I6nJ8QDhJApcnyvOM8JcP67jx+3mjsMTWM5R+ESj5LqV4E35ePnMCiKqTyKagaGdckDVoWyReWQ0p1IG4lXJ Zn57U+SyJoT8GlGrR3gfxBRvFBOZ7AYMeenc3uQIvVdovSMVjBGdSVjOZLqbEfBTZWF6U/uW1CWRGHdLUulVye+TTxbgQTrKrFT MrTkKmEfZgwE1bMAQt5FC/PB8XxxpdUTDbavFXdc9rN+ztRhNAFuqPmO0UrDz8IgJ5guryGs2a6YH1f0IOhYaxMzQCVovHGXMqF HquMHbk1LI6rm54727Cis46bA/1uShgJVOm/OVPL+hOueOKrr1G1Wq2Hu2+1PUO8Jg+p6twrrrf/IFKpaEz7Sl2LnpxCw73sJYB UhWK08ZrLfyAvkKyiDzUjeZiGNTrbAq2zJgiDybO9NU2vZ37rkpFLsO58OyeGdUYv0FNmGo5Yg6sD7rqtDkEBccIPa0klmphJkY vGFXoMzDeRGx6XXVjGHSBbxO6ZccKH86k5U1mfLm9uAJXR86D0JG3eh8xOu1WFuYwLqShqCttkQmwg0OC1HlvAXBbur5kJIhKUt ZiGDuVbmOgaK5fsP+8UxS3E4BkAIw== 1509272976@qq.com
[root@localhost ~]# git clone git@github.com:xueyin220807/moral.git moral2
Cloning into 'moral2'...
remote: Enumerating objects: 71, done.
remote: Counting objects: 100% (71/71), done.
remote: Compressing objects: 100% (49/49), done.
remote: Total 71 (delta 12), reused 67 (delta 11), pack-reused 0
Receiving objects: 100% (71/71), 316.12 KiB | 7.00 KiB/s, done.
Resolving deltas: 100% (12/12), done.
配置
[root@localhost ~]# cd moral2
[root@localhost moral2]# git config --add --local user.name 'linux220807'
[root@localhost moral2]# git config --add --local user.email 'linux220807@qq.com'
检出并创建本地分支并切换到该本地分支
[root@localhost moral2]# git checkout -b test34 origin/test34
Branch test34 set up to track remote branch test34 from origin.
Switched to a new branch 'test34'
增加文件提交
[root@localhost moral2]# git branch
master
* test34
[root@localhost moral2]# touch 1.txt
[root@localhost moral2]# git add 1.txt
[root@localhost moral2]# git commit -m "1.txt"
[test34 8865ff2] 1.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 1.txt
添加远端并推送到远端
[root@localhost moral2]# git remote add moral2 git@github.com:xueyin220807/moral.git
[root@localhost moral2]# git push moral2
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 225 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:xueyin220807/moral.git
04781a6..8865ff2 test34 -> test34
我们发现我们刚才推上去的文件已经区分于之前的用户名了,所以第二个客户端就创建成功了
创建第一个客户端
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web
$ git clone git@github.com:xueyin220807/moral.git
Cloning into 'moral'...
remote: Enumerating objects: 73, done.
remote: Counting objects: 100% (73/73), done.
remote: Compressing objects: 100% (50/50), done.
remote: Total 73 (delta 13), reused 69 (delta 12), pack-reused 0
Receiving objects: 100% (73/73), 316.33 KiB | 3.00 KiB/s, done.
Resolving deltas: 100% (13/13), done.
配置
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (master)
$ git config user.name
雪饮
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (master)
$ git config user.email
1509272975@qq.com
检出并创建本地分支并切换到该本地分支
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (master)
$ git checkout -b test34 origin/test34
Switched to a new branch 'test34'
Branch 'test34' set up to track remote branch 'test34' from 'origin'.
在该分支建立文件添加到暂存区并提交并添加远端并push到该远端
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ touch index20200726
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git add index20200726
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git commit -m "index20200726"
[test34 b2ebd43] index20200726
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index20200726
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git remote add moral git@github.com:xueyin220807/moral.git
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git push moral
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 276 bytes | 276.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
8865ff2..b2ebd43 test34 -> test34
任务开始
通过上面的实践我们实现了一机多端,接下来我们来模拟下今天的目标
我们现在假定linux端要修改刚才建立Linux端时创建的1.txt后并push
则
[root@localhost moral2]# vi 1.txt
[root@localhost moral2]# git add 1.txt
[root@localhost moral2]# git commit -m "1.txt modify"
[test34 d45f0dd] 1.txt modify
1 file changed, 1 insertion(+)
[root@localhost moral2]# git push moral2
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
To git@github.com:xueyin220807/moral.git
! [rejected] test34 -> test34 (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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
这里是因为刚才linux端创建后又去创建了window端,而创建window端时有在test34这个分支新增文件,新增文件可以被看成是修改了区分于当前linux端的同分支下的文件,所以我们此时就是面临着变基,但是合并也能达到变基的效果,而且使用起来更方便点。
而要合并就得先拉出来最新的,上面的提示fast-forwards也是这个意思。
[root@localhost moral2]# git fetch moral2
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), done.
然后我们先看看分支状况
[root@localhost moral2]# git branch -v
master 04781a6 moral
* test34 d45f0dd [ahead 2] 1.txt modify
注意:这里显示超前两个版本是相比于我们上次本地的版本,由于上次本地的版本是不包含window端新增的,另外上次本地版本不包含本次修改的,所以就算2个超前版本。
由于我们这里修改文件并没有影响到其它客户端,所以我们可以直接合并
[root@localhost moral2]# git merge moral2/test34
Merge made by the 'recursive' strategy.
index20200726 | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 index20200726
那么接下来我们再次push就是ok的
[root@localhost moral2]# git push moral2
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 8, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 504 bytes | 0 bytes/s, done.
Total 5 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To git@github.com:xueyin220807/moral.git
b2ebd43..dc4e33d test34 -> test34
真正的不同人修改了不同文件的场景
那么接下来我们在window上拉取下最新的
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git fetch moral
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
Unpacking objects: 100% (5/5), 484 bytes | 17.00 KiB/s, done.
remote: Total 5 (delta 2), reused 5 (delta 2), pack-reused 0
From github.com:xueyin220807/moral
* [new branch] branch2 -> moral/branch2
* [new branch] branch3 -> moral/branch3
* [new branch] master -> moral/master
* [new branch] newBranch1 -> moral/newBranch1
* [new branch] temp -> moral/temp
b2ebd43..dc4e33d test34 -> moral/test34
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git merge moral/test34
Updating b2ebd43..dc4e33d
Fast-forward
1.txt | 1 +
1 file changed, 1 insertion(+)
然后window随便修改下刚才自己端创建时所新增的文件并提交但不push
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ vi index20200726
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git add index20200726
warning: LF will be replaced by CRLF in index20200726.
The file will have its original line endings in your working directory
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git commit -m "index20200726 modify"
[test34 34a96b0] index20200726 modify
1 file changed, 1 insertion(+)
此时linux端突然又在同分支下修改了之前的1.txt并且push了
[root@localhost moral2]# vi 1.txt
[root@localhost moral2]# git add 1.txt
[root@localhost moral2]# git commit -m "1.txt"
[test34 b319a28] 1.txt
1 file changed, 1 insertion(+), 1 deletion(-)
[root@localhost moral2]# git push moral2
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To git@github.com:xueyin220807/moral.git
dc4e33d..b319a28 test34 -> test34
那么现在对于window来说就是同一分支已经有另外一个人修改了另外一个文件并push了,此时window再来push试试
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git push moral
To github.com:xueyin220807/moral.git
! [rejected] test34 -> test34 (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.
这里会发现错误和上面场景是相同的,那么解决方案也是相同的
先拉
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git fetch moral
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 259 bytes | 14.00 KiB/s, done.
From github.com:xueyin220807/moral
eb4c6c3..e175efd test34 -> moral/test34
再合并与推送
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git merge moral/test34
Merge made by the 'recursive' strategy.
1.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
xy@DESKTOP-BG9HNHK MINGW64 ~/Desktop/web/moral (test34)
$ git push moral
Enumerating objects: 16, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 12 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (10/10), 1.04 KiB | 1.04 MiB/s, done.
Total 10 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To github.com:xueyin220807/moral.git
e175efd..d88647d test34 -> test34
关键字词:git,同分支,不同人,不同文件,merge,变基