Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does git compare the differences between different branches

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how git compares the differences between different branches". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how git compares the differences between different branches".

Two days ago, I encountered a disturbing incident when I was doing the integration. The thing is, a colleague of Liangxu accidentally merge a wrong dev branch to the master branch, causing the Liangxu compiler to fail. Therefore, we need to roll back the version to the state it was before merge.

If it is the following state, it is easy to deal with:

In this case, all we need is a git reset command:

Git reset-hard HEAD~

However, if you do the following, the situation is not that simple:

In this case, you can't simply use the git reset command. This is the problem that Liangxu 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 make a version fallback. In this case, version fallback can not be used only with git reset, but with git revert. We will explain how to do version fallback elegantly later.

Back to the point, how do we find out the difference between the merged branch and the pre-merged branch? Here we need to use the git log command. Let's first simulate the submission of these two branches:

Dev Branch submission:

[alvin@VM_0_16_centos git-log] $git log dev commit b191410906ae20a865fde3f163bb01fd6cfc1f11 Author: Liangxu Date: Sat Dec 8 21:03:13 2018 + 0800 [dev] version 6 commit 793c9582ab0a45c4f8f548be36c06bc5ca427c62 Author: Liangxu Date: Sat Dec 8 21:02:30 2018 + 0800 [dev] version 5 commit dbe54166608772486408c1dea05304de45dba430 Author: Liangxu Date: Sat Dec 8 21:00:27 2018 + 0800 [both] version 3 commit 31894364b1396b00d2935373387397ef930416e4 Author: Liangxu Date: Sat Dec 8 20:59:26 2018 + 0800 [ Both] version 2 commit 4872f653a8fd7c8541abb4a292d628dc7625884b Author: Liangxu Date: Sat Dec 8 20:58:05 2018 + 0800 [dev] version 4 commit fac6c60ed28c5acfcd01284336d4201cc55ee2e7 Author: Liangxu Date: Sat Dec 8 20:57:01 2018 + 0800 [both] version 1

Master Branch submission:

[alvin@VM_0_16_centos git-log] $git log master commit c690054c67b833b22dce4120899526743b20d36d Author: Liangxu Date: Sun Dec 9 07:31:47 2018 + 0800 [master] version 7 commit dbe54166608772486408c1dea05304de45dba430 Author: Liangxu Date: Sat Dec 8 21:00:27 2018 + 0800 [both] version 3 commit 31894364b1396b00d2935373387397ef930416e4 Author: Liangxu Date: Sat Dec 8 20:59:26 2018 + 0800 [both] version 2 commit fac6c60ed28c5acfcd01284336d4201cc55ee2e7 Author: Liangxu Date: Sat Dec 8 20:57:01 2018 + 0800 [both] version 1

In these submissions, [both] indicates a submission of both branches, [dev] indicates a submission only in the dev branch, and [master] indicates a submission only in the master branch.

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

Method one

Command:

Git log dev ^ master

Results:

[alvin@VM_0_16_centos git-log] $git log dev ^ master commit b191410906ae20a865fde3f163bb01fd6cfc1f11 Author: Liangxu Date: Sat Dec 8 21:03:13 2018 + 0800 [dev] version 6 commit 793c9582ab0a45c4f8f548be36c06bc5ca427c62 Author: Liangxu Date: Sat Dec 8 21:02:30 2018 + 0800 [dev] version 5 commit 4872f653a8fd7c8541abb4a292d628dc7625884b Author: Liangxu Date: Sat Dec 8 20:58:05 2018 + 0800 [dev] version 4

It is obvious that the three submissions are only in the dev branch.

Instead, if you want to see a commit that the master branch has, but the dev branch does not, you can use the following command:

Git log master ^ dev

Results:

[alvin@VM_0_16_centos git-log] $git log master ^ dev commit c690054c67b833b22dce4120899526743b20d36dAuthor: Liangxu Date: Sun Dec 9 07:31:47 2018 + 0800 [master] version 7

Method two

Use the following command:

Git log master..dev

If you want to view submissions that are only in the master branch, but not in the dev branch, you can switch master to dev:

Git log dev..master

The execution results of these two commands are the same as above, so we will not repeat the results.

two。 If the submission of two branches is unknown in advance, how can I see the difference between the two branches?

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

Git log master...dev

Results:

[alvin@VM_0_16_centos git-log] $git log master...dev commit c690054c67b833b22dce4120899526743b20d36d Author: Liangxu Date: Sun Dec 9 07:31:47 2018 + 0800 [master] version 7 commit b191410906ae20a865fde3f163bb01fd6cfc1f11 Author: Liangxu Date: Sat Dec 8 21:03:13 2018 + 0800 [dev] version 6 commit 793c9582ab0a45c4f8f548be36c06bc5ca427c62 Author: Liangxu Date: Sat Dec 8 21:02:30 2018 + 0800 [dev] version 5 commit 4872f653a8fd7c8541abb4a292d628dc7625884b Author: Liangxu Date: Sat Dec 8 20:58:05 2018 + 0800 [dev] version 4

Notice that here, 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 exactly the same as that of git log dev...master.

We have added tokens such as [master], [dev], [both] for demonstration purposes, but this is rarely done in the actual development process. So when we execute git log master...dev, how do we know whether the differential commit is in the master branch or the dev branch? We just need to add the-- left-right option.

Git log-left-right master...dev

Results:

[alvin@VM_0_16_centos git-log] $git log-- left-right master...dev commit

< c690054c67b833b22dce4120899526743b20d36d Author: Liangxu Date: Sun Dec 9 07:31:47 2018 +0800 [master] 版本7 commit >

B191410906ae20a865fde3f163bb01fd6cfc1f11 Author: Liangxu Date: Sat Dec 8 21:03:13 2018 + 0800 [dev] version 6 commit > 793c9582ab0a45c4f8f548be36c06bc5ca427c62 Author: Liangxu Date: Sat Dec 8 21:02:30 2018 + 0800 [dev] version 5 commit > 4872f653a8fd7c8541abb4a292d628dc7625884b Author: Liangxu Date: Sat Dec 8 20:58:05 2018 + 0800 [dev] version 4

We will find that there is an extra value between the commit and the hash

< 或 >

, of which

< 表示只在 master 分支的提交, >

Represents a submission only in the dev branch.

Thank you for your reading, the above is the content of "how git compares the differences between different branches". After the study of this article, I believe you have a deeper understanding of how git compares the differences between different branches, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report