Handy Git script February 7, 2019


If you're working with Git, especially with the command-line interface directly, you probably find yourself executing the same series of commands fairly frequently. Whether it's rebasing a current branch, starting a new one on top of the latest version of the code or cleaning-up a local repository, developers usually learn to view these mundane tasks as a necessary evil. I was no exception.

When working on projects, I usually work in my own fork, especially when other people are involved. Therefore, when I want to create a fresh new branch, I have to go through some git commands: git checkout master, git fetch --all, git rebase upstream/master, git push (I like to keep my fork up-to-date) and git checkout -b <new branch name>. Now, neither of these is particularly cumbersome. The whole process rarely even takes a minute. Still, it is very repetitive and a bit of a chore. One day, as I was waiting for an especially large change set to get fetched, I reflected on how ludicrous it was for me to still be doing this process "by hand". That's how I came to automate my way out of it:

Download script

Of course, as this is a fairly common problem, there are a number of similar scripts available online. Look around for the one that will make your life the easiest. Or maybe you have one of your own, or maybe you'll try your hand at it after reading this! Either way, it never hurts to look around for inspiration and seize the chance to learn something new in the process.


This is a .bat script, so Windows users only! After downloading it, place it in a folder that's listed in your global PATH variable to be able to use it everywhere. The script is best suited for people working in forks and using a different branch for everything. It also uses rebase instead of merge. Here's the full manual (also available in the script itself through the man command):

usage: git.bat <command> [-mlb <main local branch>] [-b <local branch>] [-r <remote>] [-rb <remote branch>] [-mb <main branch>]

# Commands
    checkout    stash, fetch --all, checkout <-mlb>, rebase <-r>/<-rb>, push, checkout -b <-b>
    rebase      stash, fetch --all, rebase <-r>/<-rb>
    cleanup     gc --quiet, remote prune origin, branch -D <pruned remote branches>, git branch -a

# Flags
    -mlb  Main local branch, defaults to "master"
    -b    Local branch, usually to be created
    -r    Remote from which to update, defaults to "upstream"
    -rb   Branch on the remote from which to update, defaults to "master"
    -mb   Main branch, sets the value of both the "-mlb" and "-rb" flags

# Examples
    git.bat checkout -b <new local branch>    
    git.bat rebase
    git.bat cleanup

Last updated on February 7, 2019

— Etienne Lamoureux