首页 > 解决方案 > Git推送提交而不更新远程分支参考

问题描述

我正在处理一些提交,如果我的硬盘崩溃,我不想丢失它们。但是,我需要在完成之前更改它们(rebase、squash、reorder 等),所以我不能只将分支推送到远程,因为这需要我稍后强制推送,而我不想这样做做。

所以......我可以只上传提交而不更新远程分支标签。这样,我就可以下载 repo,找到我的提交,并在我的硬盘崩溃时继续工作。

问题是,我不知道该怎么做。我已阅读refspec 参数规范,它只是显示了指定要更新哪个 ref 的不同方法,但我不想更新任何 ref。谷歌搜索也没有帮助,我看到的只是关于“樱桃采摘”之类的教程,我不想要。

从分离头推也不起作用,它说使用git push origin HEAD:<name-of-remote-branch>,但我不想指定任何分支名称。

有没有一种(简单的)方法可以只上传提交而不更新任何参考?(不,无论如何推入一个分支,然后立即强制将该分支推回刚才的位置并不是一个好主意。)

标签: gitgit-push

解决方案


恕我直言,最简单的方法是使用@flyingdutchman 的回答:推送到另一个分支。

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.


推荐阅读