Skip to main content

Pushing to a differently named branch on your remote

Posted in Development and Git

When working with Git, it’s pretty normal to work on a branch locally and push it to an identically named branch on your remote (in GitHub/GitLab/Bitbucket/etc.).

git push origin my-great-feature

This command tells Git to push the work from your local feature branch to the remote repository, using the same branch name (my-great-feature) to either:

  • create the my-great-feature branch if it doesn’t already exist up there
  • add the work since your last push to the my-great-feature branch you already created

But sometimes you might want to push to a branch that’s not called the same thing as your local branch.

Admittedly, it’s not very often I have to do that, but here are a couple of recent examples:

  • I needed to demo some functionality for a client on a central staging URL that was linked to a staging branch
  • I wanted to test a redirect on a remote server

In both cases, I could create a staging branch locally, merge the work from the feature branch to it, then push, but that feels like extra steps when there’s a simpler way:

git push origin my-great-feature:staging

This tells git to push the work from your local my-great-feature branch to a remote branch named staging.

You might run into conflicts if you’ve used the staging branch for experimental things before, in which case just force your branch to overwrite what’s on your remove with the -force (or -f) flag:

git push -f origin my-great-feature:staging

Even better, if you’ve set up automatic deployments to your staging branch with something like Netlify, the work will appear on your staging site automatically!

Get them delivered!

If you enjoyed this and want all the latest articles delivered to your inbox every month, pop your email in the form below.

I don’t collect any data on when, where or if people open the emails I send them. Your email will only be used to send you newsletters and will never be passed on. You can unsubscribe at any time.

More posts

Here are a couple more posts for you to enjoy. If that's not enough, have a look at the full list.

  1. Setting an upstream Git branch

    There’s no such thing as syncing in Git, but setting an upstream branch is about as close as it gets.

  2. How to rename the ‘master’ branch on GitHub

    So renaming master to main is a good idea, but how do we do it? Fortunately, it’s really straightforward if your repository lives on GitHub.