6

Orphan commits are created when there is no tag or branch that contains them in its graph of parent commits. For example, if you make a branch foo, add commits a and b, then delete the branch (i.e. remove the reference foo from commit b), then both a and b will not be reachable unless you saved their hashes.

x-x-x-x   <- master
     \
      a-b  <- foo (reference then deleted or reset to somewhere on master)

Basic Git behavior on orphaned commits is to eventually garbage collect and delete them (I have heard the default is at least 30 days).

My question is this:

Will Git ever move orphan commits from one repo to another using the git clone, git fetch, or git push commands?

Or does Git effectively ignore these commits for any operation that does not directly call out an orphan's hash (such as checkout or cherry-pick)?

Community
  • 1
  • 1
LightCC
  • 9,804
  • 5
  • 52
  • 92
  • At least it will when you clone a repository on the local machine. For example, `git clone ~/foo.git -- ~/bar`. If the remote repository is on another server, it will not. But not sure. I guess it depends on the protocol. – ElpieKay Oct 14 '17 at 08:02
  • @ElpieKay is correct, it really depends on the underlying transfer protocol. If the sending Git chooses (for whatever reason) to send the objects inside a pack, the receiving Git gets them, and has to repack the pack to drop them. As a general rule, though, anything that gathers objects by starting from names won't gather those objects and hence won't send them. – torek Oct 14 '17 at 17:34

1 Answers1

7

During normal Git workflow, those commits are ignored. This includes git clone, git push, and git fetch. But depending on the Git repo hosting server, you might be able to pull them back in your repo, if that is needed.

That is what I described in "Does github remember commit IDs?": through the GitHub Event API, you can get back the SHA1 of a (for instance) deleted branch, and restore it that way in your own repo.

That compensates for the fact a git reflog is generally not accessible/public on the remote side.

LightCC
  • 9,804
  • 5
  • 52
  • 92
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks - but I'm not really _trying_ to pull them down, just trying to establish the behavior so I understand. There are cases where you might want orphan commits to just die and not be transmitted anywhere, and other cases where you might want to try to revive them. – LightCC Oct 14 '17 at 14:59
  • I understand. They are not transferred, unless, as I illustrate in the answer, you actively seek to reference them. – VonC Oct 14 '17 at 15:01
  • I edited your answer - please verify I understood correctly in my rewording. In the end it appears that there are some tricks to be able to find a commit that ends up orphaned on a remote, and you can always use reflog for this on a local, but in general orphaned commits are not transferred between repos with normal git commands, with or without special flags. – LightCC Oct 14 '17 at 15:33
  • Is there a way to delete an orphaned commit from a remote (say, Bitbucket)? I have managed to create one somehow using an initial commit and subsequent push of an existing project to that repository. – MadPhysicist Nov 02 '20 at 16:36
  • 1
    @MadPhysicist To be really sure an orphaned commit is deleted on the remote server side, I would contact the Git repositories hosting service support (here BitBucket support: https://support.atlassian.com/contact) and request for said commit to be deleted (probably through a `git gc` or the [more recent `git maintenance`](https://stackoverflow.com/a/64077241/6309)) – VonC Nov 02 '20 at 16:44
  • @VonC, wow. How did I end up in such a mess? :-) I simply created a new repo and was trying to move an existing project there using `git push --all` – MadPhysicist Nov 02 '20 at 17:41
  • 1
    @MadPhysicist Is that orphan commit problematic in any way? Usually, it should be ignored. – VonC Nov 02 '20 at 17:43
  • @VonC, nah, it is not problematic. Just a bit of an eye-sore when scrolling through the commits section. Is there a chance that it'll get garbage-collected automatically by the Bitbucket? – MadPhysicist Nov 02 '20 at 17:45
  • @MadPhysicist Yes, but the occurrences are not fixed or predictable, as illustrated by https://community.atlassian.com/t5/Bitbucket-questions/How-to-trigger-git-gc-on-the-server/qaq-p/1144439. Or https://jira.atlassian.com/browse/BCLOUD-11593 – VonC Nov 02 '20 at 17:46