1

In Git, I can use the following syntax to push a specific commit to a specific remote branch:

git push remote COMMIT:branchname

However, this ONLY works if there is a local branch which references COMMIT. Is there a way to do this push for a commit, even if there is no ref that refers to it?

Edward Z. Yang
  • 26,325
  • 16
  • 80
  • 110
  • 1
    What does *"local branch which references COMMIT"* mean? If a commit isn't referenced it doesn't exist in GIT – Liam Mar 19 '19 at 14:59
  • That's not quite correct, @Edward. Actually, the restriction was that the remote branch had to exist to be able to push.... but I'm not even sure this restriction is true anymore. – eftshift0 Mar 19 '19 at 15:00
  • @Liam, I guess he means that there has to be a branch pointing to the revision. – eftshift0 Mar 19 '19 at 15:00
  • Directly or indirectly? This sounds like your have an orphaned commit your trying to force into the graph. This questions needs **a lot** more context – Liam Mar 19 '19 at 15:01
  • Once you do `git init` you'll have a local branch called master.. There's no way to do commits without having a default branch.. – Baklap4 Mar 19 '19 at 15:01
  • @Baklap4 That's not relevant. In this scenario there are already local branches and local commits. – user229044 Mar 19 '19 at 15:04
  • Possible duplicate of [Is it possible to push a commit which is not in any branch in git?](https://stackoverflow.com/questions/55130311/is-it-possible-to-push-a-commit-which-is-not-in-any-branch-in-git) – phd Mar 19 '19 at 16:04
  • The answer on that question is worse though; the answer doesn't actually answer the question and you have to look at comments for the answer. – Edward Z. Yang Mar 19 '19 at 18:20
  • If you’re in a headless state just create a branch and push. Then delete it afterwards? – evolutionxbox Mar 20 '19 at 00:06

1 Answers1

4

The full spelling of a branch name is refs/heads/branchname. When you push, if the destination ref doesn't begin with refs/ Git figures out what prefix you meant by looking at what you're pushing. But if there's a bare commit there, it has nothing to go on, so you have to specify the full spelling of the destination ref explicitly:

git push origin 54adf:refs/heads/branchname
jthill
  • 55,082
  • 5
  • 77
  • 137