Search code examples
gitgit-mergebranching-and-merging

Git - Unmerge a deleted branch


Scenario:

  • Created a Feature Branch From Master(Blue Color Branch)
  • Made commits to it
  • Later Merged with the Master and Deleted the Branch
  • In between, I have created other Feature Branches and made commits to them.

Question: How can I get back the branch I deleted and unmerge so that the master looks clean without the Feature Branch (Blue Color)? I may need to add commits to that Feature Branch in future also.

I have looked at the following resources: Git undo local branch delete Git unmerge a branch

Do I need to do both of the above to get the desired result? Or do I want to create a new branch and revert the commits added in the Feature Branch and merge it?

I am completely confused and please redirect me to the right path. A sample Git Graph for the use-case is given below.

Note: There is no merge in between the Feature Branch (Blue).

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


Solution

  • You could undo the merge commit itself using git revert --mainline 1 <commit-id>, where <commit-id> is for Release Feature#1 from your picture above. And the --mainline 1 is needed for merge commit reverts and indicates which side of the merge should be reverted to, usually it's first parent, as you usually merge feature branches into master.

    You could also restore the deleted feature branch as a branch starting from right before the merge, following the second parent git branch feature-1 <commit-id>^2 (same commit-id as above).

    However, as the branch was already merged, you will not able to merge it again later. Easiest thing would be rebasing the branch, which would create copies of the feature branch commits on the top of the latest master.

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