git rebase and git merge pull from the remote to handle the merge situation

git rebase handles the merge

No conflict

First configure the git pull command to default to git pull --rebase.

git config pull.rebase true

The remote warehouse looks like this: 

The local warehouse looks like this:

It can be seen by comparison:

c7c56a7 is their common commit-id, but after this node, both the remote warehouse and the local have made completely different modifications to the hello.txt file. And after modifying this file locally, other commits (including the commit to the file hello.txt) are continued.

If'git config pull.rebase true' is not configured and git pull is directly used, merge will be used to process the merge (automatic merge or manual merge), but now we have configured it.

Pull it:

[root@master GitTest]# git pull
Successfully rebased and updated refs/heads/master.

Although the remote and local files have been modified differently, there is no conflict during the rebase merge. Why is this? (Because the different places of the same file are modified, the respective contexts have not changed, so there is no conflict)

Conflict situation

So far, the version structure of the remote and local warehouses is as follows (the local leads the remote 3 commits):

commit c3797b4cddb2bd43e609cefe8921c24e85fdca95 (HEAD -> master)
Author: looking <[email protected]>
Date:   Tue Dec 22 09:55:31 2020 +0800

    delete first line of hello.txt

commit e36200f61e20551724bf87a2afac7d338322a212
Author: looking <[email protected]>
Date:   Tue Dec 22 09:54:39 2020 +0800

    add hello world in world.txt

commit 8cad71d41f4fb040ad9685a4a6aa039005c82cea
Author: looking <[email protected]>
Date:   Tue Dec 22 09:51:15 2020 +0800

    add nice in hello.txt

commit fb26af6981e369f1c5e29b8b8b21b1f93d3233da (origin/master)
Author: looking <[email protected]>
Date:   Tue Dec 22 09:49:23 2020 +0800

    delete nice in hello.txt

commit c7c36a796d7a6ea5bf3c429fba88c36a61661681
Author: looking <[email protected]>
Date:   Tue Dec 22 09:45:56 2020 +0800

    readme.txt

In order to make the remote warehouse and the local warehouse inevitably conflict, I submitted a commit remotely (empty the content of hello.txt - it seems that I accidentally added a blank line, but it is not harmful, and the goal is achieved):

Git pull again (this time finally got the conflict):

[root@master GitTest]# git pull
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/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), 865 bytes | 216.00 KiB/s, done.
From github.com:2392863668/GitTest
   fb26af6..eced426  master     -> origin/master
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
error: could not apply 8cad71d... add nice in hello.txt
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 8cad71d... add nice in hello.txt

But don't panic, let's use git status to see what the current situation is:

[root@master GitTest]# git status
interactive rebase in progress; onto eced426
Last command done (1 command done):
   pick 8cad71d add nice in hello.txt
Next commands to do (2 remaining commands):
   pick e36200f add hello world in world.txt
   pick c3797b4 delete first line of hello.txt
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on 'eced426'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
	both modified:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

Sure enough, the hello.txt file conflicted (after all, the remote end emptied this file. If there is no conflict, it would be really weird).

In that case, take a look at vim hello.txt (and deal with conflicts by the way).

[root@master GitTest]# vim hello.txt
<<<<<<< HEAD

=======
hello world
nice
hello world
hello world. I am Looking
nice to meet you too
>>>>>>> 8cad71d... add nice in hello.txt

Modify it after you choose (the local version I keep).

[root@master GitTest]# vim hello.txt
hello world
nice
hello world
hello world. I am Looking
nice to meet you too

After modification, of course, you have to git add hello.txt:

[root@master GitTest]# git add hello.txt

Now git status tells us what to do after all conflicts are fixed (and even tells you the commit-id and title of the subsequent commits of the current conflict node):

[root@master GitTest]# git status
interactive rebase in progress; onto eced426
Last command done (1 command done):
   pick 8cad71d add nice in hello.txt
Next commands to do (2 remaining commands):
   pick e36200f add hello world in world.txt
   pick c3797b4 delete first line of hello.txt
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on 'eced426'.
  (all conflicts fixed: run "git rebase --continue")

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   hello.txt

In this case, let's do it accordingly:

[root@master GitTest]# git rebase --continue
[detached HEAD b6fdaf6] add nice in hello.txt
 1 file changed, 5 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.

In this way, you are done (the commit part after the conflict node is still retained --- this is exactly what I want, I don't want to rewrite the commit after the conflict every time the conflict is resolved):

commit 5f7be5dbb1ebe2d662efd03b7d177c2da860e0a9 (HEAD -> master)
Author: looking <[email protected]>
Date:   Tue Dec 22 09:55:31 2020 +0800

    delete first line of hello.txt

commit 63bb8efabbcc3759a2bd2e53d195afdaee950d62
Author: looking <[email protected]>
Date:   Tue Dec 22 09:54:39 2020 +0800

    add hello world in world.txt

commit b6fdaf6d53a6e298ad8ab73f903e051ecd4c81e6
Author: looking <[email protected]>
Date:   Tue Dec 22 09:51:15 2020 +0800

    add nice in hello.txt

commit eced426098acd286cb45cb7191f83ddee9cc228c (origin/master)
Author: Looking <[email protected]>
Date:   Tue Dec 22 10:25:57 2020 +0800

    clear hello.txt

Now that the conflicts are resolved, of course you have to push to the remote end:

[root@master GitTest]# git push
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 891 bytes | 297.00 KiB/s, done.
Total 9 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
To github.com:2392863668/GitTest.git
   eced426..5f7be5d  master -> master

Unlike merge, rebase does not produce a diamond box when dealing with conflicts (you can see that git trees are linear --- this is one of the reasons why git rebase is very popular):

git merge handles the merge

No conflict

Disable the automatic rebase of git pull first:

[root@master GitTest]# git config pull.rebase false

On the basis of the above code, roll back the local version with a commit:

[root@master GitTest]# git reset HEAD~
Unstaged changes after reset:
M	hello.txt

Re-modify the changed files of the rollback version:

[root@master GitTest]# git diff
diff --git a/hello.txt b/hello.txt
index f4f41f3..83585f1 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,4 +1,3 @@
-hello world
 nice
 hello world
[root@master GitTest]# vim hello.txt 
[root@master GitTest]# git diff
diff --git a/hello.txt b/hello.txt
index f4f41f3..95696e0 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,5 +1,5 @@
-hello world
 nice
 hello world
 hello world. I am Looking
 nice to meet you too
+hello world

Submit the commit again locally:

[root@master GitTest]# git add hello.txt 
[root@master GitTest]# git commit -m "delete first line and add after last line for hello.txt"
[master 085adcf] delete first line and add after last line for hello.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

Pull it from the remote: 

[root@master GitTest]# git pull
Auto-merging hello.txt
Merge made by the 'recursive' strategy.

Although it is also a modification to the same file, the changes are different, so it can be merged automatically.

[root@master GitTest]# git log
commit b2a17f4ece64a2adff17adfb0f9cfbfef24a0db7 (HEAD -> master)
Merge: 085adcf 5f7be5d
Author: looking <[email protected]>
Date:   Wed Dec 23 14:50:06 2020 +0800

    Merge branch 'master' of github.com:2392863668/GitTest into master

commit 085adcf8ee93c38b43f394f5aa8054e5478b148e
Author: looking <[email protected]>
Date:   Wed Dec 23 14:49:34 2020 +0800

    delete first line and add after last line for hello.txt

commit 5f7be5dbb1ebe2d662efd03b7d177c2da860e0a9 (origin/master)
Author: looking <[email protected]>
Date:   Tue Dec 22 09:55:31 2020 +0800

    delete first line of hello.txt

The difference from rebase is that there will be an extra commit when merging here and there will be an obvious diamond-shaped merge record. 

[root@master GitTest]# git log --oneline --decorate --color --graph 
*   b2a17f4 (HEAD -> master) Merge branch 'master' of github.com:2392863668/GitTest into master
|\  
| * 5f7be5d (origin/master) delete first line of hello.txt
* | 085adcf delete first line and add after last line for hello.txt
|/  
* 63bb8ef add hello world in world.txt
...

Conflict situation

We return the local version to the previous version of the remote version again:

[root@master GitTest]# git reset --hard HEAD^1
HEAD is now at 085adcf delete first line and add after last line for hello.txt
[root@master GitTest]# git reset --hard HEAD^1
HEAD is now at 63bb8ef add hello world in world.txt
[root@master GitTest]# git log
commit 63bb8efabbcc3759a2bd2e53d195afdaee950d62 (HEAD -> master)
Author: looking <[email protected]>
Date:   Tue Dec 22 09:54:39 2020 +0800

    add hello world in world.txt

commit b6fdaf6d53a6e298ad8ab73f903e051ecd4c81e6
Author: looking <[email protected]>
Date:   Tue Dec 22 09:51:15 2020 +0800

    add nice in hello.txt
...

The remote version deletes the first line on this basis. In order to make it conflict, we modify the first line of the local hello.txt file:

[root@master GitTest]# git diff
diff --git a/hello.txt b/hello.txt
index f4f41f3..25f7272 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,4 +1,4 @@
-hello world
+hello
 nice
 hello world
 hello world. I am Looking

Then modify the commit:

[root@master GitTest]# git add hello.txt 
[root@master GitTest]# git commit -m 'delete world in first line of hello.txt'
[master ba8b2b1] delete world in first line of hello.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

[root@master GitTest]# git log
commit ba8b2b1189e9a755282c5b267f2dedd76197db80 (HEAD -> master)
Author: looking <[email protected]>
Date:   Wed Dec 23 15:24:02 2020 +0800

    delete world in first line of hello.txt

commit 63bb8efabbcc3759a2bd2e53d195afdaee950d62
Author: looking <[email protected]>
Date:   Tue Dec 22 09:54:39 2020 +0800

    add hello world in world.txt

commit b6fdaf6d53a6e298ad8ab73f903e051ecd4c81e6
Author: looking <[email protected]>
Date:   Tue Dec 22 09:51:15 2020 +0800

    add nice in hello.txt
...

Then pull from the remote:

[root@master GitTest]# git pull
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

As you can see, the automatic merge failed this time (after all, both the remote and the local have made different changes to the same location of the file, and the conflict needs to be handled manually).

[root@master GitTest]# vim hello.txt
<<<<<<< HEAD
hello
=======
>>>>>>> 5f7be5dbb1ebe2d662efd03b7d177c2da860e0a9
nice
hello world
hello world. I am Looking
nice to meet you too

Check the status after processing:

[root@master GitTest]# git add hello.txt 
[root@master GitTest]# git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   hello.txt

Save the conflict resolution result:

[root@master GitTest]# git add hello.txt
[root@master GitTest]# git commit -m 'deal merge conflict in hello.txt'
[master 1912baa] deal merge conflict in hello.txt

You're done, view and push to the remote:

commit 1912baa1b9457add393f48c41ffd35957aa8b569 (HEAD -> master)
Merge: ba8b2b1 5f7be5d
Author: looking <[email protected]>
Date:   Wed Dec 23 15:34:02 2020 +0800

    deal merge conflict in hello.txt

commit ba8b2b1189e9a755282c5b267f2dedd76197db80
Author: looking <[email protected]>
Date:   Wed Dec 23 15:24:02 2020 +0800

    delete world in first line of hello.txt

commit 5f7be5dbb1ebe2d662efd03b7d177c2da860e0a9 (origin/master)
Author: looking <[email protected]>
Date:   Tue Dec 22 09:55:31 2020 +0800

    delete first line of hello.txt
...
[root@master GitTest]# git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 508 bytes | 254.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:2392863668/GitTest.git
   5f7be5d..1912baa  master -> master

Of course, there is still a diamond box in the merge process:

[root@master GitTest]# git log --oneline --decorate --color --graph 
*   1912baa (HEAD -> master, origin/master) deal merge conflict in hello.txt
|\  
| * 5f7be5d delete first line of hello.txt
* | ba8b2b1 delete world in first line of hello.txt
|/  
* 63bb8ef add hello world in world.txt
...

 

Guess you like

Origin blog.csdn.net/TomorrowAndTuture/article/details/111505639