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
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    Printing MooTools Accordion Items

    Sometimes we're presented with unforeseen problems when it comes to our JavaScript effects. In this case, I'm talking about printing jQuery and MooTools accordions. Each "closed" accordion content element has its height set to 0 which means it will be hidden when the...

  • By
    Facebook Sliders With Mootools and CSS

    One of the great parts of being a developer that uses Facebook is that I can get some great ideas for progressive website enhancement. Facebook incorporates many advanced JavaScript and AJAX features: photo loads by left and right arrow, dropdown menus, modal windows, and...

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!