1

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

kajacx
  • 12,361
  • 5
  • 43
  • 70
  • If you don't have a named reference for them, there's no guarantee that the remote repository will keep them for long, as unreferenced commits can be garbage collected. Also, you might not be able to "download them" without adding a named reference. Why don't you want to use force push on your own branch? – Lasse V. Karlsen Dec 02 '20 at 13:22
  • "Why don't you want to use force push on your own branch?" Because someone can base their branch on it, which would be pretty bad when I force push later. – kajacx Dec 02 '20 at 13:33
  • 2
    On a team, nobody should base their new branch on top of other work-in-progress branches unless that has been decided on and the developers on those branches are communicating etc. So that **should** be safe. I assume you're not working directly against master or develop(ment), but against a feature branch, in which case it **should** be safe. If it isn't, you might want to have a discussion with your team to make sure it **becomes** safe. – Lasse V. Karlsen Dec 02 '20 at 13:36
  • 1
    To answer your question, instead of a branch, use a tag, and name it as something like your name and work-in-progress. If someone else on your team bases their work on it and isn't prepared to clean up when you finish your work, then you might want to have a serious discussion with them about how *not* to do things. You could, however, just use a branch with that kind of name instead though, make things easier for yourself so that you will still have a branch that moves forward when you push. – Lasse V. Karlsen Dec 02 '20 at 13:39

4 Answers4

3

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.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Thanks, this is probably the closest to what I wanted. But I have to agree that just making another branch named "feature-WIP" would probably be easier. – kajacx Dec 02 '20 at 14:11
2

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.

flyingdutchman
  • 1,197
  • 11
  • 17
  • "just push to a different branch" then that another branch would have the trash commits. Not ideal. "Create a tag for your current state and then force push." Never used tags before. What do you mean "and then force push"? You mean I should push into the branch anyway and just create a tag so I don't forget where it is? That has the same problem as before - I am updating the branch remote ref. – kajacx Dec 02 '20 at 13:31
  • But If I'm not mistaken your goal is to not loose data. Regarding an additional branch: Branches do not cost anything and you can delete them after you no longer need them. So why don't you like an additional branch? Regarding tags I think this [SO question](https://stackoverflow.com/questions/3143573/what-happens-in-git-to-a-tag-when-you-amend-the-commit-that-was-tagged) shows what I mean. The tag will point to your remote state. The 2nd idea requires you to use force push eventually. I understand that you don't want that. But it's actually risky at all if the others know about it. – flyingdutchman Dec 02 '20 at 13:36
1

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

choroba
  • 231,213
  • 25
  • 204
  • 289
0

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.

H D
  • 151
  • 6
  • Thank you for the tip. I think my answer satisfies the criterion you mentioned. – H D Jul 06 '22 at 00:07