Sobes.tech
Junior

How many ways are there to create a new branch in a version control system?

sobes.tech AI

Answer from AI

Two main ways:

  1. Using the command git branch <branch_name>:

    # Creates a new branch named "feature-x" based on the current branch.
    git branch feature-x
    

    This command creates a branch but does not switch to it. The current branch remains the one from which the command was executed.

  2. Using the command git checkout -b <branch_name> or git switch -c <branch_name>:

    # Creates a new branch named "feature-y" and immediately switches to it.
    git checkout -b feature-y
    
    # Alternative method in Git 2.23+
    # Creates a new branch named "feature-z" and immediately switches to it.
    git switch -c feature-z
    

    These commands create a new branch and immediately switch to it, making it the current branch.

You can also create a branch based on a specific commit or another branch:

  • Using git branch <branch_name> <commit_sha>:
    # Creates a branch "bugfix-A" based on the specified commit.
    git branch bugfix-A a1b2c3d4e5f678901234567890abcdef12345678
    
  • Using git checkout -b <new_branch_name> <existing_branch_name>:
    # Creates a branch "development-copy" based on the "develop" branch and switches to it.
    git checkout -b development-copy develop
    
  • Using git switch -c <new_branch_name> <existing_branch_name>:
    # Alternative method in Git 2.23+
    # Creates a branch "another-feature" based on the "main" branch and switches to it.
    git switch -c another-feature main