# Git Create a New Branch from an Existing Branch

> Use git checkout -b or the newer git switch -c to create a branch from the current or a specified base branch, then push it upstream with git push -u

**URL:** https://sentry.io/answers/create-a-new-git-branch-from-an-existing-branch/

---

## The Problem

How can I create a new branch in my Git repository based on an existing branch?

## The Solution

When creating a new branch, Git will automatically use the currently checked-out branch as a base. Let's say our repository has two branches: `main` and `dev`. We want to create a `feature` branch based on the `dev` branch.

If we're already on the `dev` branch, we can go ahead and create a new branch with the following [`git checkout`](https://git-scm.com/docs/git-checkout) command:

```bash
git checkout -b feature
```

The `-b` flag creates the new branch `feature`, which is now our repository's current branch.

If we're on the `main` branch, we will need to modify our command to specify `dev` as the base for our new branch:

```bash
git checkout -b feature dev
```

Regardless of the branch we started on, our repository will now be on the `feature` branch, which will have an identical history to the `dev` branch. We can now do our work on this branch and commit to it without affecting `dev`.

If we want to push this branch to a remote, we can use the following [`git push`](https://git-scm.com/docs/git-push) command:

```bash
git push -u origin feature
```

The [`-u` flag](https://git-scm.com/docs/git-push#Documentation/git-push.txt--u) ensures that `feature` is created in our remote repository and linked to our local `feature` branch.

Once we've completed the work we need to do on the `feature` branch, we can switch back to `dev` and [merge](https://git-scm.com/docs/git-merge) it back in with these commands:

```bash
git checkout dev
git merge feature
```

If no new commits have been made to `dev` since the creation of `feature`, Git will add all of the commits from `feature` to `dev` (this is called [fast-forwarding](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging)). If commits have been made, `git merge` will create a new merge commit joining the branches – in some instances, we may need to [resolve merge conflicts](https://sentry.io/answers/resolve-merge-conflicts-in-git/) first.

### Using `git switch`

[`git switch`](https://git-scm.com/docs/git-switch) is a newer command dedicated to changing and creating branches, split off from `git checkout`'s broader set of responsibilities. It creates a branch from an existing one in a single step with the `-c` flag:

```bash
git switch -c feature dev
```

This is equivalent to the `git checkout -b feature dev` command above. `git switch` has existed since Git 2.23 (2019), but it carried an "experimental, behavior may change" warning in Git's own documentation for its first six years — that warning was only removed in Git 2.51 (August 2025), making it official as a stable command. `git checkout -b` remains the more broadly documented and widely used option, but `git switch -c` is now a solid, stable alternative.

---

*Source: [sentry.io/answers/create-a-new-git-branch-from-an-existing-branch/](https://sentry.io/answers/create-a-new-git-branch-from-an-existing-branch/)*
