Custom Git Commands in 3 Steps

I’m lazy and so I seek ways to reduce repetitious activities. For instance, I’ve spent a lot of time in a terminal typing Git commands. A few of the more common commands, I’ve aliased. If I want to see a list of branches, I used to type:

Listing Git branches

$> git branch -v -a

But after adding an alias to my bash profile, I simply type gb. I’ve done this for a few commands like git commit, which is gc and gca for the -a flag.

Occasionally, aliases aren’t enough and when it comes to Git, you can create custom commands that can be referenced like so:

Your custom Git command

$> git my-command

To create a custom command, you first need to create a file named git-my-command; second, you must place the resultant file on your path. Finally, you need to make the file executable. You can write this file in Bash, Ruby, or Python – it doesn’t matter.

For example, I tend to find myself stashing some uncommitted changes and then later popping those stashed changes onto a new branch. I end up executing the following steps:

A simple Git flow

$> git stash
$> git stash branch some_branch

The key step I want to simplify is the last one – I’m lazy and I’d rather not type 4 words. I’d rather type git unstash some_branch because it saves me one word.

Following the three simple steps I mentioned above, I’ll first create a file in my ~/bin directory called git-unstash. The ~/bin directory is in my path because my .bashrc has this line: PATH=$PATH:$HOME/bin.

My git-unstash script will be simple – it takes an argument (the branch name, i.e. $1); therefore, the script does a simple check to ensure the branch name is provided.

Custom Git command: unstash


((!$#)) && echo No branch name, command ignored! && exit 1

git stash branch $1

After I’m done writing it, I’ll do a quick chomd +x and all three steps are accomplished.

Now my new flow is this:

A simple Git flow

$> git stash
$> git unstash some_branch

Custom Git commands are that simple to invent – first, create a file named git-my-command. Next, place it on your path; and, finally, make it executable. Be lazy and carry on, baby!

Reference: Custom Git Commands in 3 Steps from our JCG partner Andrew Glover at the The Disco Blog blog.
Related Whitepaper:

Software Architecture

This guide will introduce you to the world of Software Architecture!

This 162 page guide will cover topics within the field of software architecture including: software architecture as a solution balancing the concerns of different stakeholders, quality assurance, methods to describe and evaluate architectures, the influence of architecture on reuse, and the life cycle of a system and its architecture. This guide concludes with a comparison between the professions of software architect and software engineer.

Get it Now!  

2 Responses to "Custom Git Commands in 3 Steps"

  1. Mirko Friedenhagen says:

    Hello Andrew,

    thanks for your article :-). Another way for aliases is built in in git itself:
    After executing:
    git config –global –add “branch -v -a”
    you may use:
    git gb
    The best thing is that with bash-completion “git ” will show you aliases defined in this way as well, these are my aliases:
    git config -l | grep ^alias
    alias.log1=log –pretty=format:”%h %ai %an %s” –stage
    alias.logst=log –stat
    alias.pullfo=pull –ff-only
    alias.last=log -1 HEAD
    alias.fetchap=fetch –all –prune -v -a

    Coming from svn st, ci and co were very useful. As I like rebasing local changes better than automatically merging, “git pull –ff-only” or “git pullfo” are used by me quite often as well.

    • Mirko Friedenhagen says:

      Editor add my tab in lt and gt :-):
      … The best thing is that with bash-completion “git TAB” will show you aliases defined in this way as well, …

Leave a Reply

seven × = 35

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books