39

I need to load a shell script from a raw gist but I can't find a way to get raw URL.

curl -L address-to-raw-gist.sh | bash
jbr
  • 6,198
  • 3
  • 30
  • 42
dee
  • 1,848
  • 1
  • 22
  • 34
  • 1
    Note: gist raw url just changed (February 2014). See [my answer below](http://stackoverflow.com/a/21984072/6309) – VonC Feb 24 '14 at 09:49
  • 1
    Does this answer your question? [Is there a permalink to the latest version of gist files?](https://stackoverflow.com/questions/46073096/is-there-a-permalink-to-the-latest-version-of-gist-files) – Mohammad Ayoub Khan Mar 24 '21 at 00:49

4 Answers4

75

And yet there is, look for the raw button (on the top-right of the source code).

The raw URL should look like this:

https://gist.githubusercontent.com/{user}/{gist_hash}/raw/{commit_hash}/{file}

Note: it is possible to get the latest version by omitting the {commit_hash} part, as shown below:

https://gist.githubusercontent.com/{user}/{gist_hash}/raw/{file}
aymericbeaumet
  • 6,853
  • 2
  • 37
  • 50
  • 21
    While that link does get the raw , it is hard coded to that specific commit. – spuder Jan 07 '14 at 01:52
  • 2
    is there a possibility to get the *latest* raw version? If you omit the commit hash, it doesn't work neither... – nerdoc Mar 11 '18 at 16:29
  • 2
    @nerdoc Just omit the commit hash in the URL. e.g., https://gist.github.com/aymericbeaumet/940d6360efe5a4618302c291c7b2d051/#file-useful-es6-js would be accessed in a raw version as https://gist.githubusercontent.com/aymericbeaumet/940d6360efe5a4618302c291c7b2d051/raw/useful-es6.js – aymericbeaumet Mar 11 '18 at 23:04
  • I thought that too, as it seems obvious. but it did not work. Tried, but did not work for me. Either there was a small (cache?) delay? I moved to [GitLab snippets](https://docs.gitlab.com/ee/user/snippets.html), there it works as expected. – nerdoc Mar 17 '18 at 11:22
  • It should work, do you have a public gist to reproduce the issue? – aymericbeaumet Mar 22 '18 at 13:25
  • [here](https://gist.github.com/nerdoc/b6a202fd8b06bae681a244df6298af72) is one. [raw second-last version with direct link](https://gist.githubusercontent.com/nerdoc/b6a202fd8b06bae681a244df6298af72/raw/d895749ccf88c6dc6707b9bf700a2f082fc4daa5/test.txt) - and the [latest version with a direct link](https://gist.githubusercontent.com/nerdoc/b6a202fd8b06bae681a244df6298af72/raw/66e6fc42e693904b54d86bbaf277d6368b32b429/test.txt) - these are 2 raw versions. but how do I get the latest **raw** version? – nerdoc Mar 24 '18 at 10:59
  • 3
    AAAARRRGGGHHH! https://gist.githubusercontent.com/nerdoc/b6a202fd8b06bae681a244df6298af72/raw/test.txt - I have to omit the commit hash, **but provide the filename afterwords**. So easy, but drove me nuts. Thanks. – nerdoc Mar 24 '18 at 11:02
30

February 2014: the raw url just changed.
See "Gist raw file URI change":

The raw host for all Gist files is changing immediately.
This change was made to further isolate user content from trusted GitHub applications.

The new host is

 https://gist.githubusercontent.com. 

Existing URIs will redirect to the new host.

Before it was https://gist.github.com/<username>/<gist-id>/raw/...

Now it is https://gist.githubusercontent.com/<username>/<gist-id>/raw/...

For instance:

https://gist.githubusercontent.com/VonC/9184693/raw/30d74d258442c7c65512eafab474568dd706c430/testNewGist

KrisWebDev adds in the comments:

If you want the last version of a Gist document, just remove the <commit>/ from URL

https://gist.githubusercontent.com/VonC/9184693/raw/testNewGist

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    @KrisWebDev good point. I have included in the answer for more visibility, along with an example. – VonC Aug 03 '14 at 14:00
  • 3
    `The raw host for all Gist files is changing immediately.` But I found it not immediately – Gank Jan 08 '15 at 08:16
  • 2
    A better way would be also to pay attention to the API: https://api.github.com/gists/9184693 You can get from there the raw_url and use that. This answere is correct, no doubt, but the API will (never) be outdated, it could be possible they change it again like in FEB 2014. – Michael Schneider Jan 08 '15 at 08:52
3

One can simply use the github api.

https://api.github.com/gists/$GIST_ID

Reference: https://miguelpiedrafita.com/github-gists

Sheetal gupta
  • 191
  • 1
  • 10
  • This API returns JSON data, from which you can get the raw URL as follows: `url = json_data["files"][filename]["raw_url"]` where `filename` is the name of a specific file within the gist. – jkdev Nov 18 '21 at 21:34
2

Gitlab snippets provide short concise urls, are easy to create and goes well with the command line.

Sample example: Enable bash completion by patching /etc/bash.bashrc

sudo su -
(curl -s https://gitlab.com/snippets/21846/raw && echo) | patch -s /etc/bash.bashrc
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Sandeep
  • 28,307
  • 3
  • 32
  • 24