epp
epp

Reputation: 321

Is it possible to push a commit which is not in any branch in git?

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.

Upvotes: 6

Views: 1501

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 21908

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.

Upvotes: 6

Related Questions