How to Simplify Pushing New Branches in Git

Tired of the lengthy git push --set-upstream command for new branches? Learn how a simple configuration tweak or a custom alias can let you use git push for everything, saving you time on every feature you build.

How to Simplify Pushing New Branches in Git

If you're a developer using Git, you know the routine. You create a new branch, commit your awesome changes, and then it's time to push. But for that very first push, you have to type out the long command: git push --set-upstream origin my-new-feature. It's a mouthful, and it's easy to forget.

What if you could just type git push every single time, even for new branches? Good news: you can! Let's explore two simple ways to streamline your Git workflow and say goodbye to that lengthy command forever.


Method 1: Configure Your Default Push Behavior

The easiest way to solve this is by changing a single Git configuration setting called push.default. This setting tells Git what to do when you type git push without any extra arguments.

By default, modern Git versions use the simple setting, which is cautious and requires you to explicitly link your new local branch to a remote one. We can change this to current, which tells Git to push the branch you're currently on to a remote branch with the same name.

To make this change globally (for all your projects), open your terminal and run this command:

git config --global push.default current

That's it! 🎉 From now on, when you're on a new branch called my-new-feature and you run git push, Git will automatically create a branch named my-new-feature on the origin remote and set it up for tracking.

A Quick Look at Other push.default Values:

  • simple: The default. Pushes to the upstream branch, but fails if the upstream isn't set.
  • current: Pushes to a remote branch with the same name. This is the magic setting we just used.
  • matching: Pushes all local branches to remote branches with the same name. Use with caution, as it can push unfinished work.
  • nothing: Disables git push unless you specify exactly which branch to push.

Method 2: Create a Custom Git Alias

What if you don't want to change the default behavior for all your work? Maybe you like the safety of the simple setting but still want a shortcut. This is the perfect use case for a Git alias. An alias is just a custom shortcut you create for a longer command.

We can create an alias, let's call it publish, that runs the full command for us. The special word HEAD tells Git to use whatever branch you are currently on.

Run this command to create your new publish alias:

git config --global alias.publish 'push --set-upstream origin HEAD'

Now, when you want to push a new branch for the first time, you can simply type:

git publish

This command will push your current branch to the remote, set up the tracking relationship, and then you can go back to using the standard git push for all future updates on that branch.

You can name your alias anything you like! Some other great options are:

  • pu (for push upstream)
  • ship
  • share

Which Method is Right for You? 🤔

So, which option should you choose?

  • Configure push.default: This is the most seamless solution. If you're a solo developer or work on a small team where everyone is comfortable with this workflow, setting push.default to current is a fantastic "set it and forget it" choice.

  • Create a Git Alias: This is a great alternative if you want to be more explicit. It keeps the default Git behavior intact while still giving you a powerful shortcut when you need it. This can be safer in larger teams or complex projects with strict branching conventions.

Whichever path you choose, taking a moment to set this up will save you countless keystrokes and a little bit of mental overhead every time you start a new feature. Happy coding! ✨