How does git compare the differences of different branches

Time:2021-5-7

Two days ago, Liang Xu encountered a trouble when doing integration. The thing is, a colleague of Liangxu accidentally merges a wrong dev branch to the master branch, which leads to the compilation failure of Liangxu. Therefore, we need to return the version to the state before merge.

It’s easy to deal with the following state:

How does git compare the differences of different branches

In this case, we only need onegit resetCommand:

git reset --hard HEAD~

However, the situation is not so simple if:

How does git compare the differences of different branches

In this case, it cannot be used simplygit resetThe order was given. This is the problem that Liang Xu encountered this time. In order to solve this problem, we need to find out the difference between the merged branch and the pre merged branch, and then back the version. In this case, the version fallback can not be used alonegit resetIt’s about cooperationgit revertAfter that, we will introduce how to roll back the version gracefully.

Back to the point, how can we find out the difference between the merged branch and the original branch? Here we need to use thegit logThe order was given. First, we simulate the submission of these two branches

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

    [dev] version 6

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

    [dev] version 5

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

    [both] version 3

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

    [both] version 2

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

    [dev] version 4

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

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

    [Master] version 7

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

    [both] version 3

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

    [both] version 2

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

    [both] version 1

In these submissions[both]Indicates that both branches have a commit,[dev]It means that only when the dev branch is committed,[master]Represents a commit only in the master branch

1. Check the commit that dev has but the master branch does not

  • Method 1

Command:

git log dev ^master

result:

[[email protected]_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] version 6

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

    [dev] version 5

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

    [dev] version 4

Obviously, version 4, 5 and 6 are only submitted in the dev branch.

On the contrary, if you want to seemasterBranches have, anddevIf the branch does not have a submission, you can use the following command:

git log master ^dev

result:

[[email protected]_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] version 7
  • Method 2

Use the following command:

git log master..dev

If you want to view the submission only in the master branch but not in 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 pasted repeatedly.

2. How to view the difference between the two branches when the submission of the two branches is unknown in advance?

In this case, if you run through all the above commands, you can actually get a general idea. But is there an easier way? Git also brings this problem to your mind and provides a solution:

git log master...dev

result:

[[email protected]_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] version 7

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

    [dev] version 6

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

    [dev] version 5

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

    [dev] version 4

Note that there are three points between master and dev, which are different from the previous command (two points). Moreover, the execution result of this command is similar to that of other commandsgit log dev...masterIt’s exactly the same.

We are here for the convenience of demonstration, added[master][dev][both]But in the actual development process, this is rarely done. So we’re doing itgit log master...devHow do you know if the difference submission is in the master branch or the dev branch? We just need to add--left-rightYou can choose this option.

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

result:

[[email protected]_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] version 7

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

    [dev] version 6

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

    [dev] version 5

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

    [dev] version 4

We will find that there are more than one difference between commit and hash value<or>Of which<Represents a submission only in the master branch,>Represents a commit only in the dev branch.


Finally, recently a lot of small partners come to me for helpLinux learning RoadmapAccording to my own experience, I stayed up late for a month in my spare time and organized an e-book. Whether you are interviewing or self-improvement, I believe it will help you!

Free to you, just ask you to give me a praise!

E-book | Linux development and learning Roadmap

Also hope that a small partner can join me to make this e-book more perfect!

What’s the gain? I hope the old fellow will take three strikes to show this article to more people.

Recommended reading: