A Shell Function To Automatically Add A Gitignore
2018 May 23

I often need to download a gitignore when starting a project, so I made a function that will get a gitignore for a given project from GitHub’s collection.

This method supports exactly one arguement, and creates a .gitignore in your working directory. It pulls from GitHub’s collection of gitignore files.

# $1 gitignore to grab
get_git_ignore() {
    if [[ $# -eq 0 ]] ; then
        echo "Usage $0 <GIT_IGNORE>"
        return 1
    fi
    placesToTry=(
        https://raw.githubusercontent.com/github/gitignore/master/
        https://raw.githubusercontent.com/github/gitignore/master/Global/
    )
    result_out=$(mktemp)
    for u in $placesToTry ; do
        url=$u$1.gitignore
        response=$(curl --write-out %{http_code} --silent --output "$result_out" $url)
        [ "$response" = "200" ] && break
    done

    if [[ "$response" = "200" ]] ; then
        echo "Found Gitignore. Appending to gitignore in directory"
        cat $result_out >> .gitignore
    else
        echo "Failed to find requested gitignore on github" 
    fi
    rm $result_out
}

This script does not handle adding multiple of the same gitignore. It also assumes that you already working in the directory you want to place the retrived gitignore content.

To add this to your shell. Append it to either your .zshrc or .bashrc, depending on what shell you use.


Remember you can also subscribe using RSS at the top of the page!

Share this on → Mastodon Twitter LinkedIn Reddit

A selected list of related posts that you might enjoy:

*****
Written by Henry J Schmale on 2018 May 23
Hit Counter