Detect git Directory with Bash

By  on  

One interesting aspect of working at Mozilla is that Firefox lives in a mercurial repository while several other projects live on GitHub in a git repository. While most focus on either Firefox or another project, I switch between both, leaving me running git commands inside the mercurial repository and hg commands inside git repos. It's a frustration that I've lived with for a while so I sought to find a unified way of completing common tasks.

The first step was learning to detect git from command line:

if git rev-parse --git-dir > /dev/null 2>&1; then
  # git repo!
else
  # NOT a git repo!
fi

The if statement above detects a git repository, the else means the current directory is not inside a git repo.

One frequent task is checking out master and pulling the latest code from upstream, so I create an alias to do just that:

master() {
  if git rev-parse --git-dir > /dev/null 2>&1; then
    git checkout master && git pull upstream master
  else
    hg pull && hg checkout "last(public())"
  fi
}

This alias will save me time and frustration moving forward, and I'm sure I'll find other aliases to create based on git detection!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

Incredible Demos

  • By
    Disable Autocomplete, Autocapitalize, and Autocorrect

    Mobile and desktop browser vendors do their best to help us not look like idiots by providing us autocomplete, autocorrect, and autocapitalize features.  Unfortunately these features can sometimes get in the way;  we don't always want or need the help they provide.  Luckily most browsers allow...

  • By
    Create a Sexy Persistent Header with Opacity Using MooTools or jQuery

    I've been working with the Magento eCommerce solution a lot lately and I've taken a liking to a technique they use with the top bar within their administrative control panel. When the user scrolls below a specified threshold, the top bar becomes attached to the...

Discussion

  1. bryanYangGaoFei

    shell is powerful tool !

  2. Very true! Multiple projects on different repositories can be frustrating, especially if you keep messing up on the commands. Can you tell in more detail how to create an alias in Git bash? I haven’t used it a lot so I am not very familiar with it.

  3. Thanks a lot, I test this to fool it by creating empty folder/.git/config file and it complaint that not a git repository.

    I usually use

    -f .git/config

    .

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!