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

Git - Unmerge a deleted branch

场景:

问题: 我怎样才能取回我删除并取消合并的分支,以便在没有功能分支(蓝色)的情况下母版看起来很干净?将来我可能还需要向该功能分支添加提交。

我查看了以下资源: Git undo local branch delete

我是否需要执行以上两项操作才能获得所需的结果?或者我想创建一个新分支并还原在功能分支中添加的提交并合并它?

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

注意:功能分支(蓝色)之间没有合并。

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 rebasemore about that here)将您所做的单个提交移动到另一个分支


参考文献:

  • 另一个有用的线程 - removing a single commit

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

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

但是,由于该分支已经合并,您以后将无法再次合并它。最简单的事情是重新定位分支,这将在最新主控的顶部创建功能分支提交的副本。

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