create a temporary branch name with git

▼魔方 西西 提交于 2019-12-23 10:59:20

问题


Slightly related to this question I'd like to work with a temporary branch in a shell script.

somewhat along the lines of :

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

Now, I'm not sure what this will do if the branch temp already exists, in any case I don't want the result on my_remote/publication_branch to depend on that. And I also don't want to modify the branch temp (assuming I have it for something unrelated). At best, I would also do a cleanup at the end

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

So what I'm looking for is a way to create a temporary branch name that doesn't exist yet, similar to mktemp? Is there a git command which can create a temporary branch name?


回答1:


For this specific task you can use split without -b, by using this (from its manual):

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.

So

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


来源:https://stackoverflow.com/questions/46152210/create-a-temporary-branch-name-with-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!