Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

branch

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)
like image 786
Ionică Bizău Avatar asked Nov 16 '14 19:11

Ionică Bizău


People also ask

Which option will create a new branch if it doesn't exist already while doing git checkout?

The command checkout -b creates a new branch and then checks out to that branch.

How do I switch to another branch without committing?

you can do git checkout -m <branch-name> to merge conflicts and checkout to the branch and resolve conflicts yourself, or git checkout -f <branch-name> to ignore changes.

What is the option when moving to a branch to create the branch it if it does not exist?

The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

How do you checkout a branch that already exists?

First, make sure that the target branch exists by running the “git branch” command. Now that you made sure that your branch exists, you can switch from the master branch to the “feature” branch by executing the “git checkout” command. That's it!


1 Answers

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:

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

Note the name of the alias: switchoc (for "switch or create").
As jar pointed out in the comments:

Anyone trying this in 2021, note that you cannot shadow existing git commands with aliases.
Since git switch is a git command, this alias (named "switch") won't work. You must create your unique name for the alias, like "switchit" or something.


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} 

like image 51
VonC Avatar answered Oct 13 '22 06:10

VonC