Graphite Reviewer is now Diamond

How to change a branch's remote

Greg Foster
Greg Foster
Graphite software engineer
Try Graphite


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.

Graphite's CLI enhances Git workflows by automating tasks like creating pull requests, pushing branches, and ensuring proper branch stacking. The gt submit command simplifies managing your branches and remotes.

Here are key features and workflows with gt submit:

  • Push and validate branches: Automatically force-push all branches in a stack, ensuring each branch is validated and properly stacked before submission.
  • Conflict handling: Detects and resolves conflicts before branches are pushed.
  • Interactive PR creation: Opens a prompt to input or edit pull request titles, descriptions, and reviewers.
  • Force-push safeguards: Prevents overwriting remote branches that have changed since your last submission.

Example of submitting a branch stack:

Terminal
gt submit --stack --merge-when-ready --restack

This command:

  • Submits the current branch along with its stack.
  • Ensures branches are restacked to avoid conflicts.
  • Marks PRs as "merge when ready," so they are merged automatically once all checks pass.
  • Combining creation and push: Use gt submit to streamline workflows by combining branch creation, push, and PR creation in one command.
  • Switching remotes: If your project moves to a new remote URL, update it with git remote set-url origin <new-url> or configure it in Graphite's CLI using gt config.
  • Force-pushing safely: Use the default --force-with-lease behavior in Graphite to avoid accidentally overwriting branches that others have modified.

For more information, explore the Graphite CLI documentation or Git’s official documentation on branching.

Built for the world's fastest engineering teams, now available for everyone