git 打开另一个分支(如果不存在则创建),而不检查是否已经存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26961371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:40:40  来源:igfitidea点击:

Switch on another branch (create if not exists), without checking if already exists?

gitbranch

提问by Ionic? Biz?u

git checkout -b fooswitches on foobranch (even if it doesn't exist, it is created), but if the foobranch already exists it throws an error like this:

git checkout -b foo打开foo分支(即使它不存在,它也会被创建),但是如果foo分支已经存在,它会抛出这样的错误:

fatal: A branch named 'foo' already exists.

What's the command that does the following check?

执行以下检查的命令是什么?

  • if the branch already exists, just switch on it (git checkout foo)
  • if the branch doesn't exist, create it and switch on it (git checkout -b foo)
  • 如果分支已经存在,只需打开它 ( git checkout foo)
  • 如果分支不存在,则创建它并打开它 ( git checkout -b foo)

回答by VonC

Update Q3 2019 (Git 2.23): there now actually is a git switchcommand!

2019 年第三季度更新(Git 2.23):现在实际上有一个git switch命令

git switch -c aBranch 

You would need a similar alias though:

不过,您需要一个类似的别名:

switch = "!f() { git switch  2>/dev/null || git switch -c ; }; f"


bgusach's alias mentioned below in the comment is safer (based on Ji?í Pavelka's answer):

下面评论中提到的bgusach的别名更安全(基于Ji?í Pavelka回答):

switch = "!f() { git checkout  2>/dev/null || git checkout -b ; }; f"

git switch abranch


Original answer (2014) You can try:

原始答案(2014)您可以尝试:

git checkout -B foo

From git checkoutman page:

git checkout手册页

If -Bis given, <new_branch>is created if it doesn't exist; otherwise, it is reset. This is the transactional equivalent of

如果-B给定,<new_branch>如果不存在则创建;否则,它被重置。这是交易等价物

$ git branch -f <branch> [<start point>]
$ git checkout <branch>

As mentioned below, use it with caution as it does reset the branch, which is not always desirable.
If you did reset the branch by mistake with this command, you can easily revert to its previous statewith:

如下所述,请谨慎使用它,因为它会重置分支,这并不总是可取的。
如果您确实使用此命令错误地重置了分支,则可以使用以下命令轻松恢复到其先前状态

git reset HEAD@{1}


回答by George Pavelka

Agreed with ssmith. Had the same problem and -Bdoes not solve it, coz of reset. His solution works, however my solution looks simpler:

同意铁匠。有同样的问题,-B没有解决,因为重置。他的解决方案有效,但我的解决方案看起来更简单:

git checkout foo || git checkout -b foo

That works for me :)

这对我行得通 :)

EDIT

编辑

Without error output iff foo not exists

如果 foo 不存在,则没有错误输出

git checkout foo 2>/dev/null || git checkout -b foo

回答by ssmith

Note the rather important fact that -Bwill resetan existing branch before checking it out, which I don't believe @Ionica wants according to his question.

请注意在检查之前-B重置现有分支的相当重要的事实,我不相信@Ionica 根据他的问题想要这样做。

I certainly didn't, so the best one-liner I could come up with is this:

我当然没有,所以我能想到的最好的单线是这样的:

git checkout $(git show-ref --verify --quiet refs/heads/<branch> || echo '-b') <branch>

git checkout $(git show-ref --verify --quiet refs/heads/<branch> || echo '-b') <branch>

This can be made into a handy alias like so:

这可以变成一个方便的别名,如下所示:

[alias]
  # git cob <branch>
  cob = "!f() { git checkout $(git show-ref --verify --quiet refs/heads/\"\" || echo '-b') \"\"; }; f"

回答by Praveen Kumar Purushothaman

The command checkout -bcreates a new branch and then checks out to that branch. So, if a branch already exists, it cannot create a new one.

该命令checkout -b创建一个新分支,然后检出到该分支。因此,如果分支已经存在,则无法创建新分支。

Instead you need to do:

相反,您需要执行以下操作:

git checkout -B <branchname>

The above command does in a context sensitive way. If there's a branch, it switches, if not, it creates and checkout.

上面的命令以上下文敏感的方式执行。如果有一个分支,它切换,如果没有,它创建和结帐。

回答by kishore

Not very different from what George Pavelka suggested, but instead of relying on status of "git checkout ", this checks for the presence and then decides the command to use

与 George Pavelka 的建议没什么不同,但它不是依赖于“git checkout”的状态,而是检查是否存在,然后决定要使用的命令

git show-branch <branch> &>/dev/null && git checkout <branch> || git checkout -b <branch>