6

Locally I can commit without any branch active, i.e. after checking out to a commit but not a branch. Is it possible to push this commit which is not in any branch to remote?

EDIT: I'm just wondering theoretically how would git handle pushing a "no-branch" or is pushing only possible with branches.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
epp
  • 161
  • 2
  • 8
  • 2
    Possible duplicate of [making a git push from a detached head](https://stackoverflow.com/questions/35736116/making-a-git-push-from-a-detached-head) – Obsidian Age Mar 12 '19 at 20:42
  • You can push *tags*, which I believe do not necessarily have to be attached to a branch. – merlin2011 Mar 12 '19 at 20:42
  • 1
    @ObsidianAge - I don't think that's a duplicate as this question is explicitly asking if you can push the commit _without_ creating a branch. – JDB Mar 12 '19 at 20:44
  • 1
    Even if you could push a commit like that, note that it would be automatically deleted in about 30 days (depending on the server's configuration) because it'd be an orphaned commit. *Something* must reference a commit otherwise it's cleaned up after some period of time. – JDB Mar 12 '19 at 20:45

1 Answers1

6

When you do

git push <remote> <source>:<destination>

The <source> can be a commit, yes.

The <destination>, however, is a bit more tricky. Take it from the doc :

It’s possible to push any type of object to any namespace outside of refs/{tags,heads}/. In the case of tags and commits, these will be treated as if they were the commits inside refs/heads/ for the purposes of whether the update is allowed.

So basically, you mostly push only to remote branches, but yes you can push commits, as long as moving from their current ref to the one you're pushing is a fast-forward merge.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • Also from the doc: "*The is often the name of the branch you would want to push, but it can be any arbitrary "SHA-1 expression", such as master~4 or HEAD*" – JDB Mar 12 '19 at 20:51
  • Yes, I forgot to quote for the first part of my answer and it sounded like an assertion. Thanks for the addition :-) – Romain Valeri Mar 12 '19 at 20:52
  • 2
    It's also worth mentioning that whatever name you issue, when you run `git push :`, that name is given to the other Git to decide whether to allow it. GitHub allows `refs/heads/` and `refs/tags/` names but not `refs/pull/` names, for instance. Other receivers will have different sets of rules they enforce. – torek Mar 12 '19 at 20:57
  • 1
    Did `git push origin d8c1e3` and got `fatal: d8c1e3 cannot be resolved to branch.`, did `git push origin d8c1e3:d8c1e3` and got `error: unable to push to unqualified destination: d8c1e3 The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref.` – epp Aug 21 '19 at 07:37
  • @epp why are you trying to push commit hashes? – Romain Valeri Aug 21 '19 at 07:56
  • 1
    @RomainValeri to verify your answer – epp Aug 21 '19 at 08:43
  • @epp Wow sorry, it's been a while since this thread and I had forgotten part of it, ok nevermind. – Romain Valeri Aug 21 '19 at 09:19