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. An introduction to HTML attributes

    An HTML attribute lives on the opening tag of an element and gives that element powers it might not otherwise have had.

  2. A handy Git shortcut to fetch and prune

    I’m still writing my Git commands long-hand. Turns out a fetch and prune can be more concise than I’ve previously suggested, all without aliases.