Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which branch do commits from a deleted branch belong to?

Tags:

git

git-branch

If I merge branch A into branch B and then delete A, which branch do commits from branch A (now deleted) belong to?

like image 339
Maladec VampaYa Avatar asked Mar 25 '15 13:03

Maladec VampaYa


People also ask

What happens to commits when branch is deleted?

What Happens If I Delete a Git Branch? When you delete a branch in Git, you don't delete the commits themselves. That's right: The commits are still there, and you might be able to recover them.

Do commits belong to a branch?

In Git, a commit has ancestors, but a "branch" is really just the current head of some line of development. In other words, a commit is a snapshot of the working tree at some point in time, and can belong to any number of branches at once.

How can I find a deleted branch?

A deleted Git branch can be restored at any time, regardless of when it was deleted. Open your repo on the web and select the Branches view. Search for the exact branch name using the Search all branches box in the upper right. Click the link to Search for exact match in deleted branches.


1 Answers

Git branches are mere pointers to commits. Asking

Which branch does this commit belong to?

doesn't really make sense (at least, not in the general case) because commits may very well be reachable from multiple branches (or even from none at all!).

Consider the following example:

enter image description here

Commit F is currently only reachable from the bugfix branch; at this point, it makes sense to say that commit F "belongs" to the bugfix branch. However, if you then merge bugfix into master, by running

git checkout master
git merge bugfix

then commit F becomes reachable from both of those branches:

enter image description here

Commit F can no longer be said to belong to bugfix more than to master. If you then delete bugfix, commit F will again be reachable from only one branch, master this time, in which case it will make sense to say that commit F "belongs" to master.


In summary, a commit cannot, in general, be thought of as exclusively belonging to any one branch. However, a question that always does make sense is

From which branches (if any) is this commit reachable?

like image 61
jub0bs Avatar answered Sep 29 '22 08:09

jub0bs