kajacx
kajacx

Reputation: 12959

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.)

Upvotes: 2

Views: 1209

Answers (4)

H D
H D

Reputation: 191

Disclaimer. I am new to GitHub -- but I think I know the answer to your question.

I assume your local branch name matches a remote branch name. If you would like the branch name to be kept unchanged during the push, the process will result in two branches on the remote with the exact same name -- which is impossible. If you would like the branch name to change during the push, you will be back to the solution suggested above, i.e., pushing to a new branch.

Upvotes: 0

LeGEC
LeGEC

Reputation: 52196

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.

Upvotes: 4

choroba
choroba

Reputation: 242363

Create a different remote, push whatever you like to it. On GitHub, that's usually accomplished by forking the original repo. If you want to keep it private, create it on a USB stick (use git clone --bare ... to create a clone without a working copy).

Upvotes: 1

flyingdutchman
flyingdutchman

Reputation: 1419

First idea: Maybe I missed this potential solution in your question. If not, push to a different branch as described in this SO question.

2nd idea: Create a tag for your current remote state, push the tag or create the tag remotely via Gitlab/Github, after that force push your new state. The tag ensures that your previous commits are kept.

Upvotes: 2

Related Questions