使用 git 创建一个临时分支名称

create a temporary branch name with git

this question 稍微相关我想在 shell 脚本中使用临时分支。

有点像:

cd $(git rev-parse --show-toplevel) &&
git subtree split --prefix=some_subfolder -b temp &&
git push my_remote temp:publication_branch -f

现在,如果分支 temp 已经存在,我不确定这会做什么,无论如何我不希望 my_remote/publication_branch 上的结果依赖于此。而且我也不想修改分支 temp (假设我有它是为了一些无关的东西)。充其量,我也会在最后进行清理

cd $(git rev-parse --show-toplevel) &&
git subtree split --prefix=some_subfolder -b temp &&
git push my_remote temp:publication_branch -f
git branch -D temp

所以我正在寻找一种方法来创建一个尚不存在的临时分支名称,类似于 mktemp?有没有可以创建临时分支名称的git命令?

对于这个特定的任务,您可以在没有 -b 的情况下使用拆分,方法是使用这个(来自其手册):

After splitting successfully, a single commit id is printed to stdout. This corresponds to the HEAD of the newly created tree, which you can manipulate however you want.

所以

split_head=`git subtree split --prefix=some_subfolder`
git push my_remote "$split_head":publication_branch -f