git guess-upstream

File under: git.

A small script you can use to quickly make your branch track the upstream branch with the same name.

Names don’t mean anything for git, but they do for me and my collegues. Very often, if not always, the remote branch names correspond to the local branch that I want to work on. So that’s what this script is for.

Life is too short to type git branch --set-upstream-to origin/something! 😄

The complete source code

$ cat ~/bin/git-guess-upstream

#!/usr/bin/env ruby

current_branch = `git rev-parse --abbrev-ref HEAD`.strip
STDERR.puts "    git branch --set-upstream-to \"origin/#{current_branch}\""
`git branch --set-upstream-to "origin/#{current_branch}"`

Maybe I should rewrite it in Bash or so.


Copy the script to a file, make it executable and put it in a directory in your path. I recommend you use ~/bin or /usr/local/bin.