Data report"State of code review 2024" is now liveRead the full report

How to change a branch's remote

Greg Foster
Greg Foster
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 change the remote associated with a branch, create new branches, push them to different remotes, and ensure they track the correct upstream branches. This is essential for managing a project’s various development streams.

Branches in Git allow you to diverge from the main line of development and continue to work independently without affecting the main line. Branches are independent collections of commits that have their own discrete histories. Remotes are versions of your project that are hosted on the network or internet, typically on platforms like GitHub, GitLab, or Bitbucket.

To start, you might need to create a local branch and then push it to a remote repository. This is useful when starting a new feature or fix.

  1. Create a new branch from your current branch:

    Terminal
    git checkout -b new-feature
  2. Push the new branch to remote:

    Terminal
    git push -u origin new-feature

    The -u flag sets the upstream for your branch, linking it to a remote branch. After running this command, your local branch new-feature will track origin/new-feature.

Sometimes you may need to change the remote that your local branch is tracking, either to push to a different repository or to switch the branch the remote is associated with. For example, you would need to change remotes if the current remote branch your local branch is tracking has been deleted upstream.

  1. Check the current upstream branch:

    Terminal
    git branch -vv

    This command shows all local branches along with their corresponding remotes.

  2. Change the upstream remote:

    Terminal
    git branch --set-upstream-to=origin/another-branch

    Replace origin/another-branch with the new remote branch you want to track.

  • Creating and pushing a branch in one command: You can use git push -u origin new-feature to both create the remote branch and set your local branch to track it in one command.

  • Handling non-existent remotes: If you try to push to a remote that doesn't exist, Git will return an error. Ensure you have the correct remote added with git remote -v, and add a new remote using git remote add <name> <url> if necessary.

  • Switching remotes: If your project moves to a new remote URL (such as after changing hosting services), update your remote’s URL with git remote set-url origin <new-url>.

For further reading on changing remotes see the official Git documentation on branching.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2