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

六眼飞鱼酱① 提交于 2019-11-27 02:01:54

问题


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

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)

回答1:


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

git switch -c aBranch 

You would need a similar alias though:

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

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

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

git switch abranch

Original answer (2014) You can try:

git checkout -B foo

From git checkout man page:

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

$ 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 state with:

git reset HEAD@{1}




回答2:


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

git checkout foo || git checkout -b foo

That works for me :)

EDIT

Without error output iff foo not exists

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



回答3:


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

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>

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/\"$1\" || echo '-b') \"$1\"; }; f"



回答4:


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

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.




回答5:


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

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


来源:https://stackoverflow.com/questions/26961371/switch-on-another-branch-create-if-not-exists-without-checking-if-already-exi

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