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
    for u in $placesToTry ; do
        response=$(curl --write-out %{http_code} --silent --output "$result_out" $url)
        [ "$response" = "200" ] && break

    if [[ "$response" = "200" ]] ; then
        echo "Found Gitignore. Appending to gitignore in directory"
        cat $result_out >> .gitignore
        echo "Failed to find requested gitignore on github" 
    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.

Written by Henry J Schmale on 2018 May 23
Hit Counter

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

Share this on → Twitter LinkedIn Reddit