Why is the Git function branch still visible after merging and deleting?

Steps taken

I merged the feature branch back to master and removed the feature branch. But he still appears in the tree.

Result

Visualizing a tree in SourceTree before deleting a function branch: Tree before deleting a function branch

Rendering a tree in SourceTree after deleting a function branch: Tree after deleting a function branch

The feature branch opens in the left list, as expected, and the label in the tree renderer also disappeared as expected.

Questions

But:

  • Why is the purple part still showing?
  • What Git commands do I need to run to no longer see the purple part? I probably myself answered this question in the two sections below.

I understand that the final commit in the screenshot above has two parents. But I do not understand why the violet commit that occurred on the feature branch is not in the final merged commit to master (which, I think, means that the violet branch should no longer be visible after it is removed).

Repeating steps from the command line (instead of SourceTree)

I played it on the command line (just to check if SourceTree did what I thought it did), and the last step was the git merge feature . The same situation:

Visualization of the tree on the command line

Trial with --squash

I disabled the last merge and tried this:

 git merge --squash feature git commit "Squashed merge" git delete -D feature # Note that -d did not work; it said "error: The branch 'feature' is not fully merged." 

And now it shows what I would expect in the first place. One straight line and the absence of signs of the feature branch that ever existed:

Straight tree line after --squash

Question

  1. How is this merger different from the previous merger?

I suggest that I sort of put together what happens to these mergers after all the trial and error above, but I would appreciate it if someone could really explain in detail what the semantic difference of the above steps is.

+7
git git-branch git-merge
source share
2 answers
  • You still see the purple part because you are using git merge , which creates a merge assembly that merges the two branches. Since the branches diverge, this merge is intransitive forward, so history will still show this tree.

  • You do not need the purple part? Use git rebase instead of git merge . In your case:

    • git checkout master
    • git rebase feature
    • git branch -d feature

This will intercept feature to master before the discrepancy. You will have a straight line in your history journal.

  1. You made git merge --squash , which is very similar to rebase, but you will crush all the commits of the source branch in one commit (in your case, you only have one commit, so it really doesn't appear)
+4
source share

When you delete a branch, you delete the commit pointer, not the commit. If there are no other references to the commit, then the commit can be garbage collected, but the merged commit creates a link to that commit (since this commit is its parent).

The initial merge workflow creates a merge with two parents, and the second parent commit still exists, even if the branch pointing to it does not.

When you do git merge --squash , you actually invoke rebase your commit on the target branch, which gives you a linear history instead of a two-parent history. For a distinction between this command and rebase , see this question .

+4
source share

All Articles