Skip to content

Instantly share code, notes, and snippets.

@domderen
Created December 3, 2016 12:41
Show Gist options
  • Save domderen/913c4144960aab1ec3329c62568ceed1 to your computer and use it in GitHub Desktop.
Save domderen/913c4144960aab1ec3329c62568ceed1 to your computer and use it in GitHub Desktop.
Returns the contents of a single file from a GitHub repository, relying on standard Git authentication method.

GitHub repositories don't support git archive command, which allows getting content of a single file. A different method of getting file contents from GitHub is to use GitHub API with an access token, but this requires a different authentication method, and would require creating a special account in an org that could be used for example in CI systems. I wanted a method that relies on the same auth method as git clone for example, and still just gets the content of a single file.

Example usage:

./get_github_file.sh facebook react docs/README.md
#!/bin/bash
GITHUB_REPO_OWNER=$1
GITHUB_REPO_NAME=$2
GITHUB_REPO_FILE_PATH=$3
REPO_CLONE_PATH=/tmp/temp_repo
git clone -n https://github.com/$GITHUB_REPO_OWNER/$GITHUB_REPO_NAME.git --depth 1 $REPO_CLONE_PATH &> /dev/null
cd $REPO_CLONE_PATH
git checkout HEAD $GITHUB_REPO_FILE_PATH &> /dev/null
FILE_CONTENTS=$(<$REPO_CLONE_PATH/$GITHUB_REPO_FILE_PATH)
echo "$FILE_CONTENTS"
rm -rf /tmp/temp_repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment