Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-push

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.

like image 541
epp Avatar asked Mar 12 '19 20:03

epp


People also ask

How do you push to a branch that does not exist?

Create a new branch with git checkout -b <branch_name> , and push it with git push -u origin <branch_name> . Then, everyone can see your branch, and if you need to make changes to it, it's as straightforward as changing to the branch, committing, and pushing it up.

Does git push push to all branches?

No, git push only pushes commits from current local branch to remote branch that you specified in command.

Can you push a specific commit in git?

Normally when one does a push, they push everything at once. However, git does provide a way to push only one commit at a time. The caveat is that the single commit you want to push must be directly above the tip of the remote branch (the oldest of your local commits).

Can you push a branch without commits?

No, you must make a commit before you can push. What is being pushed is the commit (or commits).


1 Answers

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.

like image 62
Romain Valeri Avatar answered Oct 12 '22 01:10

Romain Valeri