How git compares the differences between different branches

Two days ago, Liang Xu encountered a trouble when doing integration. This is what happened. A colleague of Liang Xu accidentally merged a wrong dev branch to the master branch, which caused Liang Xu to fail to compile. Therefore, we need to roll back the version to the state before the merge.

If it is the following state, it is easy to handle:

In this case, we only need a git resetcommand:

git reset --hard HEAD~

However, if the following state, the situation is not so simple:

In this case, you can not simply use the git resetcommands. This is the problem that Liang Xu encountered this time. In order to solve this problem, we need to find the difference between the merged branch and the branch before the merge, and then perform a version rollback. Version in this case the fallback, it is not alone git reset, but rather to tie git revert, and later we will detail how to be elegant version rollback.

Back to the topic, how do we find the difference between the merged branch and the pre-merged branch? Here we need to use git logcommands. We first simulate the submission of these two branches:

  • Dev branch submission status:
[alvin@VM_0_16_centos git-log]$ git log dev
commit b191410906ae20a865fde3f163bb01fd6cfc1f11
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:03:13 2018 +0800

    [dev] 版本6

commit 793c9582ab0a45c4f8f548be36c06bc5ca427c62
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:02:30 2018 +0800

    [dev] 版本5

commit dbe54166608772486408c1dea05304de45dba430
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:00:27 2018 +0800

    [both] 版本3

commit 31894364b1396b00d2935373387397ef930416e4
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:59:26 2018 +0800

    [both] 版本2

commit 4872f653a8fd7c8541abb4a292d628dc7625884b
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:58:05 2018 +0800

    [dev] 版本4

commit fac6c60ed28c5acfcd01284336d4201cc55ee2e7
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:57:01 2018 +0800

    [both] 版本1
  • Master branch submission status:
[alvin@VM_0_16_centos git-log]$ git log master
commit c690054c67b833b22dce4120899526743b20d36d
Author: Liangxu <[email protected]>
Date:   Sun Dec 9 07:31:47 2018 +0800

    [master] 版本7

commit dbe54166608772486408c1dea05304de45dba430
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:00:27 2018 +0800

    [both] 版本3

commit 31894364b1396b00d2935373387397ef930416e4
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:59:26 2018 +0800

    [both] 版本2

commit fac6c60ed28c5acfcd01284336d4201cc55ee2e7
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:57:01 2018 +0800

    [both] 版本1

Among these commits, [both] means that both branches have commits, [dev] means that only commits on the dev branch, and [master] means that only commits on the master branch

1. View the commits that the dev has but the master branch does not

  • method one

command:

git log dev ^master

result:

[alvin@VM_0_16_centos git-log]$ git log dev ^master
commit b191410906ae20a865fde3f163bb01fd6cfc1f11
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:03:13 2018 +0800

    [dev] 版本6

commit 793c9582ab0a45c4f8f548be36c06bc5ca427c62
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:02:30 2018 +0800

    [dev] 版本5

commit 4872f653a8fd7c8541abb4a292d628dc7625884b
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:58:05 2018 +0800

    [dev] 版本4

Obviously, the three commits of version 4, 5, and 6 are only in the dev branch.

On the contrary, if you want to see the commits that the master branch has but the dev branch does not, you can use the following command:

git log master ^dev

result:

[alvin@VM_0_16_centos git-log]$ git log master ^dev
commit c690054c67b833b22dce4120899526743b20d36d
Author: Liangxu <[email protected]>
Date:   Sun Dec 9 07:31:47 2018 +0800

    [master] 版本7
  • Method Two

Use the following command:

git log master..dev

If you want to view the commits that are only on the master branch, but not on the dev branch, you can swap master and dev:

git log dev..master

The execution results of these two commands are the same as above, so the results will not be posted again.

2. How can I check the differences between the two branches if I don’t know the status of the two branches in advance?

In this case, if you run the above commands once, you can actually get a general idea. But is there an easier way? git also thinks of this problem for you, and also provides a solution:

git log master...dev

result:

[alvin@VM_0_16_centos git-log]$ git log master...dev
commit c690054c67b833b22dce4120899526743b20d36d
Author: Liangxu <[email protected]>
Date:   Sun Dec 9 07:31:47 2018 +0800

    [master] 版本7

commit b191410906ae20a865fde3f163bb01fd6cfc1f11
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:03:13 2018 +0800

    [dev] 版本6

commit 793c9582ab0a45c4f8f548be36c06bc5ca427c62
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:02:30 2018 +0800

    [dev] 版本5

commit 4872f653a8fd7c8541abb4a292d628dc7625884b
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:58:05 2018 +0800

    [dev] 版本4

Please note that here, there are three dots between master and dev, which is different from the previous command (two dots). Moreover, the results of this command with git log dev...masterexactly the same.

We have added [master] , [dev] , [both] marks here for the convenience of demonstration, but in the actual development process, this operation is rarely done. That we are in the implementation git log master...dev, the how do you know there are differences in the submission is the master branch, the branch is still in dev? We just need to add --left-rightoption.

git log --left-right master...dev

result:

[alvin@VM_0_16_centos git-log]$ git log --left-right master...dev
commit < c690054c67b833b22dce4120899526743b20d36d
Author: Liangxu <[email protected]>
Date:   Sun Dec 9 07:31:47 2018 +0800

    [master] 版本7

commit > b191410906ae20a865fde3f163bb01fd6cfc1f11
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:03:13 2018 +0800

    [dev] 版本6

commit > 793c9582ab0a45c4f8f548be36c06bc5ca427c62
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 21:02:30 2018 +0800

    [dev] 版本5

commit > 4872f653a8fd7c8541abb4a292d628dc7625884b
Author: Liangxu <[email protected]>
Date:   Sat Dec 8 20:58:05 2018 +0800

    [dev] 版本4

We will find that there is an extra < or > between commit and hash value , where < means only commits on the master branch, > means only commits on the dev branch.

Finally, recently many friends asked me for the Linux learning roadmap , so based on my experience, I spent a month staying up late in my spare time and compiled an e-book. Whether you are in an interview or self-improvement, I believe it will help you! The directory is as follows:

Give it to everyone for free, just ask you to give me a thumbs up!

Ebook | Linux development learning roadmap

I also hope that some friends can join me to make this e-book more perfect!

Gain? I hope the old irons will have a three-strike combo so that more people can read this article

Recommended reading:

Guess you like

Origin blog.csdn.net/yychuyu/article/details/108425318