Developer Tools

How do I set up a Git branch?

Updated 2026-08-14

✓

Quick answer

To set up a Git branch, use the command 'git branch <branch-name>' to create a new branch and 'git checkout <branch-name>' to switch to it.

This guide provides steps to create and switch to a new Git branch, along with caveats and FAQs.

Steps

  1. 1

    Open Terminal or Command Prompt

    Navigate to your Git repository using the terminal or command prompt.

  2. 2

    Create a New Branch

    Run the command 'git branch <branch-name>' to create a new branch.

  3. 3

    Switch to the New Branch

    Use 'git checkout <branch-name>' to switch to the newly created branch.

  4. 4

    Merge the Branch (Optional)

    To merge your branch into the main branch, switch back to the main branch and run 'git merge <branch-name>'.

Creating a Git Branch

You can create a new branch in your local repository using the command line.

Switching to a New Branch

After creating a branch, you can switch to it using Git commands to start working on it.

Merging Branches

Once your work is complete, you can merge your branch back into the main branch.

Watch out for

  • Ensure you have committed any changes before switching branches to avoid losing work.
  • Branch names must be unique within the repository.

FAQ

What is the difference between 'git branch' and 'git checkout'?

'git branch' creates a new branch, while 'git checkout' switches to an existing branch.

Can I create a branch from a specific commit?

Yes, you can create a branch from a specific commit using 'git branch <branch-name> <commit-hash>'.

How do I delete a branch?

You can delete a branch using 'git branch -d <branch-name>' if it's fully merged, or 'git branch -D <branch-name>' to force delete.