Automatically Create Remote Branches From Local

Updated:

Alright, so it's been a minute.

I've been a bit busy building my agency, and as I'm sure you can tell from my absence it's going quite well.

I've got a quick one here that is absolutely amazing (and honestly has already saved me 6 minutes all up in just one day)

The current issue

This is the current situation.

You're working on some code, and realise you're on the develop branch.

Oh shit, let's move this code onto our own branch

git checkout -b feature/fix-all-the-bugs

You keep coding, and when you're finished, you go to run

git push

and you get...

fatal: The current branch feature/fix-all-the-bugs has no upstream branch.
To push the current branch and set the remote as upstream, use
 
git push --set-upstream origin feature/fix-all-the-bugs

And then you have to type ANOTHER command:

git push --set-upstream origin feature/fix-all-the-bugs

And you finally get the OK:

branch 'feature/fix-all-the-bugs' set up to track 'origin/feature/fix-all-the-bugs'.

Introducing git 2.37 - push.autoSetupRemote

Read more about it here

But basically:

If set to "true" assume --set-upstream on default push when no upstream tracking exists for the current branch; this option takes effect with push.default options simple, upstream, and current. It is useful if by default you want new branches to be pushed to the default remote (like the behavior of push.default=current) and you also want the upstream tracking to be set. Workflows most likely to benefit from this option are simple central workflows where all branches are expected to have the same name on the remote.

Alright, let's get it.

Check what version of git you're currently running by typing:

git --version

If you're 2.37 or higher, you're good.

If not, you can download the latest version from https://git-scm.com/ or via the command line if you're using homebrew:

brew install git

Now run this:

git config --global push.autoSetupRemote true

And boom, the magic:

gc -b feature/super-bug-fixes
Switched to a new branch 'feature/super-bug-fixes'
 
git push
branch 'feature/super-bug-fixes' set up to track 'origin/feature/super-bug-fixes'.

Now all your new branches will automatically be pushed upstream, how cool! 🔥