Red Green Repeat Adventures of a Spec Driven Junkie

Configure git to create remote branch with local branch names

tl;dr: use $ git config --global push.default current to have git create remote branch with the local branch name.

Gong Xian - Landscapes with Poems

At one time, I configured git to create remote branches with the local branch name so when making a new remote branch is as easy as:

$ git checkout -b test
$ git push -u

Except I forgot what the configuration was! I (and my colleagues) encountered this situation:

$ git checkout -b test
bash-3.2$ git push -u
fatal: The current branch test has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin test

and the usual solution would be:

  1. take hand off home row
  2. copy the command: git push --set-upstream origin test that git automatically generated (“how convenient!”)
  3. paste the command
  4. get my keys onto home row
  5. press enter
  6. type: git push

That’s six steps, four if your hand was not on home row to begin with, but manual steps that aren’t really automatable.

When I started looking for what I did to configure git push -u to basically automate the above six steps, I could not… it’s difficult because results focus on just working with remote branches…

Until today.

The key configuration for git to automate the six steps when pushing a new local branch and have it created on the remote server is:

$ git config --global push.default current

All this does is push the current branch only. No simple or matching, just the current.

With this configuration, pushing a new local branch to github becomes:

bash-3.2$ git push -u
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 446 bytes | 446.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
remote:
remote: Create a pull request for 'test' on GitHub by visiting:
remote:      https://github.com/a-leung/tdd_indepth_divide/pull/new/test
remote:
To github.com:a-leung/tdd_indepth_divide.git
 * [new branch]      test -> test
Branch 'test' set up to track remote branch 'test' from 'origin'.

and now I can move my hands off home row (and keep them off!) to open a browser to github to create a pull request. :-)