简体   繁体   English

还原当前分支中的git提交

[英]Revert a git commit not in current branch

I have 2 branches: master and feature. 我有2个分支:主人和功能。

• master
• feature

I make two commits on feature branch 我在功能分支上做了两次提交

• master
• C1 • C2 • feature

Now, I want to merge feature in master. 现在,我想合并master中的feature I use git merge --squash feature to merge. 我使用git merge --squash feature进行合并。

• C3 • master     // C3 is the squashed commit
• C1 • C2 • feature

At this moment, is there a way to revert C1 from master? 此刻,有没有办法从主人那里恢复C1

One option is to revert C1 on feature, and squash merge feature again in master. 一种选择是在主要上恢复C1功能,并再次压缩合并功能。

Yes, you can revert some commit even though it was included in the aggregate, using merge --squash . 是的,你可以恢复一些提交, 即使它包含在聚合中,使用merge --squash git-revert works by identifying the changes that were introduced in the commit that you identified, and then creating a new change that undoes ("reverts") those changes. git-revert工作原理是识别您标识的提交中引入的更改,然后创建一个撤消(“恢复”)这些更改的新更改。

It uses the three-way merge algorithm to do this - using the commit to revert as the base, and then comparing against that commit's ancestor and the current commit ( HEAD ). 它使用三向合并算法来执行此操作 - 使用提交恢复为基础,然后与该提交的祖先和当前提交( HEAD )进行比较。 This will isolate the changes that were introduced only in that commit. 这将隔离仅在该提交中引入的更改。

To look at a very contrived example, imagine that you had some file that had three changes ( C0 , C1 and C2 ) and these are the contents of that file at each version: 要查看一个非常人为的示例,假设您有一些文件有三个更改( C0C1C2 ),这些是每个版本的文件内容:

| C0    | C1    | C2    |
|-------|-------|-------|
| one   | one   | one   |
| two   | 2     | 2     |
| three | three | three |
| four  | four  | FOUR  |

Now, if you want to revert C1 , we set up a three-way merge with it as the base, and C0 and C2 as each side to take changes from. 现在,如果你想恢复C1 ,我们建立一个以它为基础的三向合并,并将C0C2作为每一侧进行更改。

In a three-way merge algorithm, you look at each side, compared to the base. 在三向合并算法中,与基数相比,您可以查看每一侧。 If all three lines are equal , you take that line into the result unmodified. 如果所有三条线都相等 ,则将该线放入未修改的结果中。 If one side has made a change, you take the changed line into the result. 如果一方已经做出了改变,你需要改变的线到结果。 (If both sides have made a change on the same line, you mark that line as a conflict.) (如果双方已就同一线路变化时,你标志着该行的冲突。)

Setting up a revert gives you: 设置还原可以为您提供:

base     sides     result
----     -----     ------
         one
       / two   \
      /  three  \
one  /   four    \ one
2                  two
three              three
four \   one     / FOUR
      \  2      /
       \ three /
         FOUR

You can see that the result (on the right) that has undone the changes that were introduced in C1 , because one of the sides ( C0 , in this case) was unique, so its changes were kept in the result. 您可以看到已撤消 C1中引入的更改的结果(右侧),因为其中一个边(在本例中为C0 )是唯一的,因此其更改保留在结果中。 This has the logical effect of undoing ("reverting") the changes introduced therein. 这具有撤消(“恢复”)其中引入的变化的逻辑效果。

This would be true even if you had done a squash merge - in this case, it's looking at the repository's contents. 即使您已经进行了壁球合并,这也是正确的 - 在这种情况下,它正在查看存储库的内容。 It doesn't matter that CM doesn't actually have C1 as a common ancestor. CM实际上没有将C1作为共同的祖先并不重要。

You can prove this to yourself in a simple repository with these contents. 您可以在包含这些内容的简单存储库中向自己证明这一点。 Even after a squash merge: 即使在南瓜合并之后:

commit 9e7497c7ae34aa35cdb7d7b965a00d56bf0b9dfa
Author: Edward Thomson <ethomson@edwardthomson.com>
Date:   Thu Nov 9 10:20:31 2017 +0000

    Squashed commit of the following:

    commit 8a8a9e73e62e21683e15269d89e1fbfbbf35cfa1
    Author: Edward Thomson <ethomson@edwardthomson.com>
    Date:   Thu Nov 9 10:20:18 2017 +0000

        C2

    commit d984b27140e48c5faa8968364c415d29dcd7034c
    Author: Edward Thomson <ethomson@edwardthomson.com>
    Date:   Thu Nov 9 10:20:08 2017 +0000

        C1

You can still revert one of the components correctly. 您仍然可以正确还原其中一个组件。 In this case, I'll revert C1 : 在这种情况下,我将还原C1

> git revert d984b27
[master 405f108] Revert "C1"
 1 file changed, 1 insertion(+), 1 deletion(-)

> git show HEAD
commit 405f1080e24504fa418d423a0755a2123b85ecd8 (HEAD -> master)
Author: Edward Thomson <ethomson@edwardthomson.com>
Date:   Thu Nov 9 10:20:42 2017 +0000

    Revert "C1"

    This reverts commit d984b27140e48c5faa8968364c415d29dcd7034c.

diff --git a/hello.txt b/hello.txt
index 9d980ae..14cf0bc 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,5 +1,5 @@
 Hello, world!
 one
-2
+two
 three
 four

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM