Git-取消合并已删除的分支

问题描述 投票:1回答:2

场景:

  • 从母版创建特征分支(蓝色分支)
  • 作出承诺
  • 后来与母版合并并删除了分支
  • 在这之间,我创建了其他功能分支并对其进行提交。

问题:如何找回已删除并取消合并的分支,以便使母版看起来很整洁而没有Feature分支(蓝色)?将来我可能还需要向该功能分支添加提交。

我查看了以下资源:Git undo local branch deleteGit unmerge a branch

我需要同时执行上述两项操作才能获得所需的结果吗?还是我要创建一个新分支并还原添加到功能分支中的提交并合并它?

我完全感到困惑,请将我重定向到正确的路径。下面给出了用例的示例Git图。

注意: Feature Branch(蓝色)之间没有合并。

var gitgraph = new GitGraph({
            template: "metro", // or blackarrow
            orientation: "horizontal",
            author: "John Doe",
            mode: "compact" // or compact if you don't want the messages
        });

        const master = gitgraph.branch("master");
        master.commit("Init the project");
        master.commit("Master Commit");
        master.tag("v1.0");

        const newFeature = gitgraph.branch("feature-1");
        newFeature.commit("Feature#1 Commit-1");
        master.commit("Hotfix Bug1");

        const development = master.branch("development");
        development.commit("Development Commit-1");
        development.commit("Development Commit-2");

        master.commit("HotFix Bug2");

        const anotherFeature = master.branch("feature-2");
        anotherFeature.commit("Feature#2 Commit-1");
        anotherFeature.commit("Feature#2 Commit-2");

        newFeature.commit("Feature#1 Commit-2");
        newFeature.commit("Feature#1 Commit-3");

        master.commit("HotFix Bug3");
        master.commit("HotFix Bug4");

        newFeature.merge(master, "Release Feature#1");
        master.tag("v2.0");

        master.commit("Additional Commit-1");

        development.merge(master, "Development Merge");
        
        master.commit("Additional Commit-2");

        master.tag("HEAD");
<script src="https://cdnjs.cloudflare.com/ajax/libs/gitgraph.js/1.11.4/gitgraph.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Git Graph</title>
</head>
  <body>

    <canvas id="gitGraph"></canvas>
    
  </body>
</html>

Git Graph Branching and Merging: Usecase

git git-merge branching-and-merging
2个回答
1
投票

您可以使用git revert --mainline 1 <commit-id>撤消合并提交本身,其中,上图中<commit-id>代表Release Feature#1--mainline 1是合并提交还原所必需的,它指示应将合并的哪一侧还原(通常是它的第一个父级,因为通常将要素分支合并到master中)。

您还可以将已删除的功能分支还原为从合并之前开始的分支,紧随第二个父级git branch feature-1 <commit-id>^2之后(与上述相同的commit-id)。

但是,由于分支已经合并,因此以后将无法再次合并。最简单的事情是重新分支,这将在最新的master顶部创建功能分支提交的副本。

git checkout feature-1
git rebase master
...resolve conflicts, do more amendments, etc
git checkout master
git merge feature-1

1
投票

简单地撤消所有提交在master分支上,直到返回到合并提交(具有已删除的分支)。

由于合并两个未婚夫也是一个提交,因此您可以撤消它。

注意:如果需要,您还可以使用git rebasemore about that here)将所做的单个提交移动到另一个分支


参考:

© www.soinside.com 2019 - 2024. All rights reserved.