Search code examples
gitgit-push

Git push commits without updating remote branch ref


I have some commits I am working on and I don't want to lose them if my hard drive crashes. However, I will need to alter them (rebase, squash, reorder, etc) before I am done, so I can't just push the branch to remote, as that would require me to force push later which I don't want to do.

So ... I could just upload the commits without updating the remote branch tag. That way, I can just download the repo, find my commits, and continue working if my harddrive crashes.

Problem is, I don't know how to do that. I have read the refspec parameter specification, it just shows different ways to specify which ref I want to update, but I don't want to update any ref. Googling this is also not helpful, all I see is tutorials on "cherry-pick" and such, which I don't want.

Pushing from detached head doesn't work either, it says to use git push origin HEAD:<name-of-remote-branch>, but I don't want to specify any branch name.

Is there an (easy) way to only upload the commits and not update any refs? (And no, pushing into a branch anyway and then immidietely force-pushing that branch back to where it was a moment ago is not a good idea.)


Solution

  • IMHO, the easiest way is to go with @flyingdutchman's answer : push to another branch.

    You may choose any name you want, e.g : kajacx/dontreadthis/backups/20201202, or anything that suits you, and clearly indicates "this branch is mine, and is temporary".


    As far as refspecs go : if you provide a name that is actually a complete ref name (starting with with refs/...), then you can push to something that will not be listed along with regular branches (branches are just refs that start with refs/heads/...).

    You can try :

    git push origin HEAD:refs/kajacx/backups/20201202
    

    The server may have settings to reject pushes to such references, though, so YMMV.

    If the push is accepted : this reference would be listed in git ls-remote origin.

    You would also have to clean up your "private refs" once they aren't needed anymore, too.