简体   繁体   English

创建一个新分支并在一个Git命令中设置其上游

[英]Making a new branch and setting its upstream in one Git command

My usual workflow for making a new branch is 我创建新分支的常用工作流程是

git checkout -b My-New-Branch

and then after some local commits 然后经过一些本地提交

git push --set-upstream origin My-New-Branch

Looking at the documentation for git checkout it looks like I should be able to use --track to set the new upstream branch as I make the local branch, but I cannot seem to make it work: these all fail with the error given. 看一下git checkout的文档,看起来我应该能够使用--track来设置新的上游分支,因为我创建了本地分支,但我似乎无法使它工作:这些都失败并给出了错误。

git checkout --track origin/My-New-Branch
git checkout -b My-New-Branch --track origin/My-New-Branch
git checkout -b My-New-Branch --track origin My-New-Branch
git checkout -b --track origin My-New-Branch

fatal: Cannot update paths and switch to branch 'My-New-Branch' at the same time. 致命:无法同时更新路径并切换到分支“我的新分支”。
Did you intend to checkout 'My-New-Branch' which can not be resolved as commit? 您是否打算签出“My-New-Branch”,无法将其解析为提交?

Is it possible to set the upstream as I create a new branch in Git? 我可以设置上游,因为我在Git中创建了一个新分支吗?

You could define an alias to do that: 您可以定义一个别名来执行此操作:

git config --global alias.co-push '!f() { git checkout -b $1; git push --set-upstream origin $1; }; f'

This will allow you to do git co-push My-New-Branch , which will execute both commands you have been executing separate before. 这将允许你执行git co-push My-New-Branch ,它将执行你之前单独执行的两个命令。 If you need to push to different remotes, you can exchange "origin" with "$2" in the definition and then do git co-push My-New-Branch origin . 如果你需要推送到不同的遥控器,你可以在定义中用“$ 2”交换“origin”,然后执行git co-push My-New-Branch origin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM