104

Are we allowed to link files directly from Github ?

<link rel="stylesheet" href="https://raw.github.com/username/project/master/style.css"/>
<script src="https://raw.github.com/username/project/master/script.js"></script>

I know this is allowed on Google Code. This way I don't have to worry about updating a local file.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
anjanesh
  • 3,771
  • 7
  • 44
  • 58
  • 4
    You can see question analog with good answers here: http://stackoverflow.com/questions/17341122/how-to-link-and-execute-external-javascript-file-from-github – AuthorProxy Sep 23 '13 at 18:23
  • 5
    Possible duplicate of [Link and execute external JavaScript file hosted on GitHub](http://stackoverflow.com/questions/17341122/link-and-execute-external-javascript-file-hosted-on-github) – Troy Alford Feb 11 '17 at 06:15
  • Another link format (with double-slash) for raw content is "https://github.com/OWNER/REPO/raw/main//README.md" – Joe Bowbeer Aug 24 '22 at 19:02
  • This redirects to https://raw.githubusercontent.com/OWNER/REPO/raw/main/README.md – anjanesh Aug 25 '22 at 02:45

11 Answers11

81

The great service RawGit was already mentioned, but I'll throw another into the ring: GitCDN.link

Benefits:

  • Lets you link to specific commits, as well as auto-get the latest (aka master)
  • Incurs no damage from high traffic volumes; RawGit asks that it's dev.rawgit.com links be only used during development, where as GitCDN give you access to the latest version, without the danger of the servers exploding
  • Give you the option of auto minifying your HTML, CSS and JavaScript, or serving it as written (https://min.gitcdn.link).
  • Adds compression (GZip)
  • Adds all the correct headers (Content-Type, cache-control, e-tag, etc)

Full disclosure, I'm a project maintainer at GitCDN.link

This service has been shuttered due to Heroku's policy change to remove all free tiers.

Shane Gadsby
  • 1,260
  • 1
  • 13
  • 19
38

You can use external server rawgithub.com. Just remove a dot between words 'raw' and 'github' https://raw.github.com/.. => https://rawgithub.com/ and use it. More info you find in this question.

However, according to the rawgithub website it will be shutting down at the end of October 2019.

Raymond
  • 396
  • 4
  • 21
AuthorProxy
  • 7,946
  • 3
  • 27
  • 40
26

For those who ended up in this post and just want to get the raw link from an image in GitHub:

If it is the case of an image, you can just add '?raw=true' at the end of the link to the file. E.g. Original link: https://github.com/githubusername/repo_name/blob/master/20160309_212617-1.png

Raw link: https://github.com/githubusername/repo_name/blob/master/20160309_212617-1.png?raw=true

jvel07
  • 1,239
  • 11
  • 15
  • There are no Access-Control-Allow-Origin headers though.... so it's not very useful. – Douglas Gaskell Nov 12 '21 at 01:27
  • If you navigate to that url in a browser it re-directs to the `raw.githubusercontent` link now – theZ3r0CooL Mar 17 '23 at 01:42
  • @DouglasGaskell That might've been true then, but now, the header is present as: `Access-Control-Allow-Origin: *`. – tony19 May 11 '23 at 19:17
  • @theZ3r0CooL That's an advantage to me. Appending `?raw=true` is easier and less error prone than modifying the leading path (assuming this is all done manually). – tony19 May 11 '23 at 19:19
18

GitHub Pages: https://yourusername.github.io/script.js
GitHub repo raw files: https://github.com/yourusername/yourusername.github.io/blob/master/script.js

Use GitHub Pages, DO NOT use raw files.

Reason: GitHub Pages are based on CDN, raw files are not. Accessing raw files will directly hit on GitHub servers and increase server load.

Sista Fiolen
  • 621
  • 7
  • 9
16

You can link directly to raw files, but it's best not to do it since the raw files always get sent with a plain/text header and can cause loading problems.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Benjamin Intal
  • 2,748
  • 1
  • 25
  • 26
16

Add a branch your project using the name "gh-pages" and then you'll (shortly after branching) be able to use a direct URL such as https://username.github.io/project/master/style.css (using your URL, and assuming "style.css" is a file in the "master" folder in the root of your "project" repository...and that your Github account is "username").

Raymond
  • 396
  • 4
  • 21
jekewa
  • 281
  • 3
  • 5
  • the URL pattern may have changed since 2013: you no longer have to add "/master" to the URL, [https://username.github.io/project/file-path](https://username.github.io/project/file-path) is sufficient now – Andreas Rozek Apr 02 '23 at 08:31
14

You need carry out the following steps

  1. Get the raw url of the file from github. Which is something like https://raw.githubusercontent.com/username/folder/example.css

  2. Visit http://rawgit.com/. Paste the git url above in the input box. It will generate two url's, one for development and other for production purpose.

  3. Copy any one of them and you are done.

The file will act as a CDN. You can also use gist urls.

Nitesh
  • 1,241
  • 4
  • 16
  • 25
12

Use jsdelivr.com

Copied directly from https://www.jsdelivr.com/?docs=gh:

load any GitHub release, commit, or branch
note: we recommend using npm for projects that support it

https://cdn.jsdelivr.net/gh/user/repo@version/file

load jQuery v3.2.1

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/dist/jquery.min.js

use a version range instead of a specific version

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2/dist/jquery.min.js

https://cdn.jsdelivr.net/gh/jquery/jquery@3/dist/jquery.min.js

omit the version completely to get the latest one

you should NOT use this in production

https://cdn.jsdelivr.net/gh/jquery/jquery/dist/jquery.min.js

add ".min" to any JS/CSS file to get a minified version

if one doesn't exist, we'll generate it for you

https://cdn.jsdelivr.net/gh/jquery/jquery@3.2.1/src/core.min.js

add / at the end to get a directory listing

https://cdn.jsdelivr.net/gh/jquery/jquery/

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Farhad Sakhaei
  • 894
  • 10
  • 28
1

After searching for this same functionality, I ended up writing my own PHP script to act as a proxy. The trouble I kept running into is even when you get the RAW version/link from Github and link to it in your own page, the header sent over was 'text/plain' and Chrome was not executing my JavaScript file from Github. I also didn't like the other links posted for using third party services because of the obvious security/tampering issues possible.

So using this script, I can pass over the RAW link from Github, have the script set the correct headers, and then output the file as if it were coming from my own server. This script can also be used with a secure application to pull in non-secure scripts without throwing SSL errors warning of "Non-secure links used".

Linking:

<script src="proxy.php?link=https://raw.githubusercontent.com/UserName/repo/master/my_script.js"></script>

proxy.php

<?php
###################################################################################################################
# 
# This script can take two URL variables
# 
# "type"
#   OPTIONAL
#   STRING
#   Sets the type of file that is output
# 
# "link"
#   REQUIRED
#   STRING
#   The link to grab and output through this proxy script
# 
###################################################################################################################



# First we need to set the headers for the output file
# So check to see if the type is specified first and if so, then set according to what is being requested
if(isset($_GET['type']) && $_GET['type'] != ''){
    switch($_GET['type']){
        case 'css':
            header('Content-Type: text/css');
            break;

        case 'js':
            header('Content-Type: text/javascript');
            break;

        case 'json':
            header('Content-Type: application/json');
            break;

        case 'rss':
            header('Content-Type: application/rss+xml; charset=ISO-8859-1');
            break;

        case 'xml':
            header('Content-Type: text/xml');
            break;

        default:
            header('Content-Type: text/plain');
            break;
    }

# Otherwise, try and determine what file type should be output by the file extension from the link
}else{
    # See if we can find a file type in the link specified and set the headers accordingly

    # If css file extension is found, then set the headers to css format
    if(strstr($_GET['link'], '.css') != FALSE){
        header('Content-Type: text/css');

    # If javascript file extension is found, then set the headers to javascript format
    }elseif(strstr($_GET['link'], '.js') != FALSE){
        header('Content-Type: text/javascript');

    # If json file extension is found, then set the headers to json format
    }elseif(strstr($_GET['link'], '.json') != FALSE){
        header('Content-Type: application/json');

    # If rss file extension is found, then set the headers to rss format
    }elseif(strstr($_GET['link'], '.rss') != FALSE){
        header('Content-Type: application/rss+xml; charset=ISO-8859-1');

    # If css xml extension is found, then set the headers to xml format
    }elseif(strstr($_GET['link'], '.xml') != FALSE){
        header('Content-Type: text/xml');

    # If we still haven't found a suitable file extension, then just set the headers to plain text format
    }else{
        header('Content-Type: text/plain');
    }
}

# Now get the contents of our page we're wanting
$contents = file_get_contents($_GET['link']);

# And finally, spit everything out
echo $contents;
?>
  • LOL kinda hacky but its the solution I went with LOL... was wanting to use https://cdn.jsdelivr.net/ BUT its not great for development when you're making a bunch of changes because of the CDN caching ... anyway thanks for the quick script! – Banning Apr 13 '20 at 14:42
-1

If your webserver has active allow_url_include, GitHub serving the files as raw plain/text is not a problem since you can include the file first in a PHP script and modify its Headers to the proper MIME type.

-3

This 2023, I create a tool called "GIT-RAWify"

GIT-RAWify streamlines the entire process of obtaining raw GitHub links. No more struggling with complex manual conversions or dealing with formatting issues.

To get started, visit the GIT-RAWify website at git-rawify.vercel.app

: https://git-rawify.vercel.app/