Read Anthropic’s case study about Graphite Reviewer

How to create a Git branch from a remote

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide will cover how to create local git branches from existing remote branches, ensuring you can manage and contribute to collaborative projects efficiently.

Branches in Git are essentially pointers to collections of commits. When you create a branch, you create an independent line of development, which you can switch between, and make code changes in isolation before merging these changes back into your central codebase.

In Git, local branches reside on your own local machine, allowing you to work privately on changes without affecting others. Remote branches, on the other hand, are stored on a server like GitHub, making them accessible to other team members. When you push changes from a local branch, you update the corresponding remote branch, ensuring one central point of truth for your codebase. Conversely, pulling changes from a remote branch updates your local branch with the latest changes from the remote server.

  1. Fetch the latest changes from remote

    Before creating a new branch, ensure your local repository is up to date with the remote repository:

    Terminal
    git fetch origin

    This command updates your local copy of the remote branches without merging any changes into your local branches.

  2. Create a local branch from a remote branch

    To create a local branch from a remote branch, you first need to know which remote branch you want to base your new local branch on.

    To see all of the remote branches you have access to, run the command:

    Terminal
    git branch -r

    From this list, select the remote branch you wish to make a local copy of then run:

    Terminal
    git checkout -b <new-branch-name> origin/<remote-branch-name>

    Replace <new-branch-name> with your desired branch name and <remote-branch-name> with the remote branch you want to base your new branch on.

    Example:

    Terminal
    git checkout -b feature-branch origin/main

    This command creates a new branch named feature-branch based on origin/main and checks it out immediately.

  • Create a new branch from a specific commit or tag

    If you need to base your branch not just on another branch but a specific commit or a tag, use:

    Terminal
    git checkout -b <new-branch-name> <commit-or-tag>

    Just like the above steps, this will create a new branch locally that you can make changes to, then add those changes, commit them, and push them up to the remote repository.

For further reading on creating branches from a remote, see the official Git documentation.

0 min read
0
Share
Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2