Cost Check Now
Programming·Git

How to create a new branch in Git and switch to it (2026)

Verified from sources

Quick Answer

In Git, create and switch to a new branch from your current branch with `git switch -c branch-name`. On older setups, use `git checkout -b branch-name` instead. Confirm it worked with `git branch --show-current` or `git status`, which should show the new branch name as the current branch.

Overview

Creating a branch in Git gives you a separate line of work so you can make changes without affecting the main branch straight away. The usual order is: make sure you are in the correct repository, check which branch you are currently on and whether you have uncommitted changes, create the new branch from the right starting point, switch to it, then verify that Git now shows that branch as active. If you skip the checks at the start, you can easily branch from the wrong place or carry unwanted local changes into the new branch. For most modern Git installations, `git switch -c` is the clearest command because it both creates the branch and moves you onto it in one step. If you need compatibility with older documentation or older Git versions, `git checkout -b` does the same job. After switching, it is good practice to verify the active branch before you start editing files or committing. If you also want the branch on a remote such as GitHub or GitLab, that is a separate step: you create it locally first, then push it and set the upstream tracking branch. This guide focuses on doing the local branch creation correctly and knowing at each stage that it has worked.

Who this is for

Developers, learners and teams using Git from the command line who want the correct, safe way to create a branch and start working on it.

What you’ll need

  • Git installed
  • A local Git repository
  • Terminal or command prompt access
  • Basic access to the branch you want to branch from

Before you start

Open a terminal in your project folder and confirm it is a Git repository with `git status`. Check whether you have uncommitted changes, because those changes may carry over to the new branch. Also make sure you are currently on the branch and commit you actually want to branch from, often `main`, `master` or a feature branch.

Step-by-step

  1. 1

    Go to the repository and check its state

    In your terminal, move into the project directory and run `git status`. If Git reports that this is not a repository, you are in the wrong folder. If it shows modified or untracked files, decide whether to commit, stash or leave them as they are before creating the branch.

    Why: Branch creation happens inside a specific repository, and your current working state affects what you see after switching.

  2. 2

    Confirm the branch you want to branch from

    Run `git branch --show-current` to see your current branch. If needed, switch to the correct starting branch first with `git switch branch-name` or, on older setups, `git checkout branch-name`. If you need the latest remote changes on that branch, fetch and update it according to your team workflow before branching.

    Why: A new branch starts from your current commit. If you are on the wrong branch or an out-of-date commit, the new branch will start in the wrong place.

  3. 3

    Create the new branch and switch to it

    Run `git switch -c new-branch-name`. If `git switch` is not available, use `git checkout -b new-branch-name`. Choose a clear branch name that matches your team’s naming convention if one exists.

    Why: This creates the branch reference and immediately makes it your active branch, so your next commits go there instead of the previous branch.

  4. 4

    Verify that you are now on the new branch

    Run `git branch --show-current` or `git status`. The current branch shown should match the name you just created. You can also run `git branch` and look for the asterisk next to the active branch.

    Why: Verification catches mistakes early, especially if you mistyped the branch name or the command failed because the branch already exists.

  5. 5

    Start your work and make a commit

    Edit files as needed, then use `git status` to review changes. Stage and commit them with your usual workflow, for example `git add` followed by `git commit`.

    Why: A branch is most useful once it has its own commits. Until then, it simply points to the same commit as the branch you created it from.

  6. 6

    Push the branch to the remote if needed

    If you want the branch available on a remote repository, push it after your first commit, typically with `git push -u origin new-branch-name`. After that, future pushes and pulls can usually omit the remote and branch name because tracking is set.

    Why: Creating a branch locally does not automatically create it on the remote. Pushing publishes it for backup, collaboration and pull requests.

Why this works

Git branches are lightweight references to commits. Creating a branch adds a new reference at your current commit, and switching changes where future commits will advance. That is why the exact commit and branch you start from matter.

Common mistakes to avoid

  • Creating the branch while on the wrong starting branch
  • Assuming a local branch automatically exists on the remote
  • Forgetting that uncommitted changes can follow you when you switch branches
  • Using a branch name that already exists and not noticing the command failed
  • Starting work without checking which branch is currently active

Troubleshooting

Git says the branch already exists

List branches with `git branch` or `git branch -a`. If the branch already exists locally, switch to it with `git switch branch-name` instead of creating it again.

The `git switch` command is not recognised

Use `git checkout -b branch-name` to create and switch in one command, or update Git if your environment allows it.

You created the branch from the wrong place

If you have not made commits yet, switch back, delete the mistaken branch with `git branch -d branch-name`, move to the correct starting branch or commit, and create it again. If you have already committed work, consider rebasing or cherry-picking according to your team workflow.

You cannot switch branches because of local changes

Commit the changes, stash them with `git stash`, or discard them if that is safe. Then switch branches and, if needed, reapply the stash.

The branch exists locally but not on GitHub or another remote

Push it explicitly with upstream tracking, for example `git push -u origin branch-name`.

Compare your options

`git switch -c new-branch-name`

Best for: Modern Git use and clearer branch-focused commands

Pros: More explicit, easier to read, designed for switching branches

Cons: Not available in older Git versions

`git checkout -b new-branch-name`

Best for: Older Git installations and compatibility with older tutorials

Pros: Widely supported, combines creation and switching

Cons: Less clear because `checkout` also handles other tasks

Alternatives

  • Create the branch without switching using `git branch new-branch-name`, then switch with `git switch new-branch-name`
  • Create and switch branches in a Git GUI or IDE such as GitHub Desktop, Visual Studio Code or JetBrains tools

Pro tips

  • Use short, descriptive branch names such as `fix-login-bug` or `feature/user-profile` if your team allows that style
  • Run `git log --oneline --decorate --graph -n 10` if you want to confirm the commit history before branching
  • If the branch is for shared work, push it soon after creating it so others can find it
  • Check your team’s branch naming and branching strategy before you start

Safety notes

  • Do not delete branches with unmerged work unless you are sure the commits are no longer needed
  • Review uncommitted changes before switching so you do not accidentally carry unrelated edits into the new branch
  • Be careful with force-pushes after rewriting branch history, especially on shared branches

What this guide does not cover: This guide covers creating and switching to a branch from the command line. It does not go deep into branching strategies, conflict resolution, rebasing, pull request workflows or IDE-specific interfaces.

Cost considerations

Creating local Git branches has no direct cost. Costs may arise only from paid remote hosting, managed developer platforms or organisational tooling.

Frequently asked questions

What is the difference between `git switch -c` and `git checkout -b`?+

They both create a new branch and switch to it. `git switch -c` is the newer, clearer command, while `git checkout -b` works on older Git versions.

Does creating a branch copy all the files?+

Not in the sense of making a separate full duplicate of the repository. Git creates a new branch reference pointing to the current commit, and your working tree then reflects that branch.

Can I create a branch if I have uncommitted changes?+

Yes, but those local changes may still be present after switching. That can be useful or confusing, so check `git status` first and decide whether to commit or stash them.

How do I know the branch was created successfully?+

Run `git branch --show-current` or `git status`. If Git shows the new branch as the current branch, creation and switching worked.

How do I create a branch from a different branch or commit?+

First switch to that branch or commit, or use syntax that names the start point explicitly, such as creating the branch from a specified start point. Check the result with `git log` or `git status` before you begin work.

Sources & references

Guidance on this page is traced to documented sources. Last checked 24 September 2026.

Core Git branch commands are stable, though newer commands such as `git switch` may be preferred over older patterns over time.

Related guides

Legal Disclaimer: The information provided on Cost Check Now is for general informational and educational purposes only. It does not constitute financial, legal, professional, or any other form of advice. Cost Check Now makes no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability, or availability of any information, products, services, or related graphics contained on this website. Any reliance you place on such information is strictly at your own risk. In no event will Cost Check Now, its owners, operators, contributors, or affiliates be liable for any loss or damage including without limitation, indirect or consequential loss or damage, or any loss or damage whatsoever arising from loss of data or profits arising out of, or in connection with, the use of this website. Always seek independent professional advice before making financial or purchasing decisions.