1

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

Harish ST
  • 1,475
  • 9
  • 23
  • 1
    Thanks btw for the Git Graph, that really helped me understand your issue ... it would be awesome if you also included a picture of it ^^ – finnmglas May 24 '20 at 06:24
  • 1
    Added. Just found the Snippet is not Available while browsing in Mobile (Not Logged in). Thanks for the suggestion! – Harish ST May 24 '20 at 14:06

2 Answers2

1

Simply undo all commits on the master branch till you get back to the merge commit (with the deleted branch).

As merging two brances is a commit too, you then can just undo that.

Note: You can also use git rebase (more about that here) to move single commits you made to another branch if necessary


References:

finnmglas
  • 1,626
  • 4
  • 22
  • 37
  • 1
    Thank you. So undoing the commits till the merge will bring back the "Feature Branch". Right? – Harish ST May 24 '20 at 06:25
  • 1
    It should, however, if you try something new with git, always work on a copy of your repository - as you know the "no backup, no mercy" rule ^^ – finnmglas May 24 '20 at 06:29
  • 1
    I am a little confused. If I undo all commits including merge commit, then can I recommit those which was there after the merge? Can't we just undo the merge commit alone? – Harish ST May 24 '20 at 06:41
  • 1
    Maybe this thread is useful to you: [Removing a single commit](https://stackoverflow.com/q/2938301/12787264) - I'll include it in my answer – finnmglas May 24 '20 at 06:48
  • Looking into that! – Harish ST May 24 '20 at 06:58
  • I am trying in my local. But does not make it work as desired. – Harish ST May 24 '20 at 08:12
  • Thank you for taking the time to answer my question. I found Kan's answer easier to execute. But thanks for answering in the first place! – Harish ST May 24 '20 at 14:01
1

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
kan
  • 28,279
  • 7
  • 71
  • 101
  • Thank you! More clear now. Let me try these and get back. – Harish ST May 24 '20 at 08:38
  • Thank you @kan. I reverted the commit and working fine. But I have a question. When restoring the delete branch you mentioned `git branch feature-1 ^2`, so what `^2` means? 2 commit before the commit-id? Also now after doing `git rebase master`, if I want the changes done in old feature which is removed after reverting, should I `re-revert the revert-commit`? Please excuse me, if my questions are immature. – Harish ST May 24 '20 at 10:23
  • 1
    @HarishST `^2` means second parent of a commit. Merge commit is a commit with more than one parent. "Two commits before" would be `~2`. See more here https://git-scm.com/docs/gitrevisions – kan May 24 '20 at 11:34
  • 1
    Not sure if I understand the second question. `rebase` would create new commit copies (similar content, but different id). So, it is the same as a brand-new branch. A revert commit is a simple commit, just with opposite patch. And yes, you could revert revert commit too, no rebase needed in this case, if you don't need `do more amendments` then it's an option too. – kan May 24 '20 at 11:37
  • Yes. You got that right. Thanks for your efforts to answer my doubts! – Harish ST May 24 '20 at 13:57