i would like to get off a branch (e.g. git switch HEAD -d
), make a commit, and push it to a remote (e.g. github) without creating a branch there.
use case is that sometimes i want to ask someone "is this what you mean?" and link to a commit, but i don't want github to prompt me to create a PR, or for this garbage to show up everywhere you can list branches.
how do i push a git commit without creating a branch?
CodePudding user response:
AFAIK you have to create a ref to it. Fortunately you can probably get close enough for practical purposes:
- Create a local branch with the commit.
- Push it.
- Immediately delete the remote branch.
- Provide the full 40 char hash to the user to fetch:
git fetch origin <full-hash>
Note the only reason you can even do the above steps is because garbage collection is not instantaneous and enables orphaned commits to stick around for a while. In the case of GitHub and many other tools, it may even stick around forever without further intervention. In this case the lifetime of the branch only needs to be for a few seconds just so you can push "something".
Side Note: you can skip step 1 if you wish and push directly to a branch name on the remote only:
# push commit <hash> to remote branch
git push origin <hash>:some-temp-branch-name
# delete remote branch
git push -d origin some-temp-branch-name