How to add images to README.md on GitHub?

1,535,316

Solution 1

Try this markdown:

![alt text](http://url/to/img.png)

I think you can link directly to the raw version of an image if it's stored in your repository. i.e.

![alt text](https://github.com/[username]/[reponame]/blob/[branch]/image.jpg?raw=true)

Solution 2

You can also use relative paths like

![Alt text](relative/path/to/img.jpg?raw=true "Title")

Also try the following with the desired .fileExtention:

![plot](./directory_1/directory_2/.../directory_n/plot.png)

Solution 3

  • You can create a New Issue
  • upload(drag & drop) images to it
  • Copy the images URL and paste it into your README.md file.

here is a detailed youTube video explained this in detail:

https://www.youtube.com/watch?v=nvPOUdz5PL4

Solution 4

You can also add images with simple HTML tags:

<p align="center">
  <img src="your_relative_path_here" width="350" title="hover text">
  <img src="your_relative_path_here_number_2_large_name" width="350" alt="accessibility text">
</p>

Solution 5

Many of the posted solutions are incomplete or not to my taste.

  • An external CDN like imgur adds another tool to the chain. Meh.
  • Creating a dummy issue in the issue tracker is a hack. It creates clutter and confuses users. It's a pain to migrate this solution to a fork, or off GitHub.
  • Using the gh-pages branch makes the URLs brittle. Another person working on the project maintaining the gh-page may not know something external depends on the path to these images. The gh-pages branch has a particular behavior on GitHub which is not necessary for hosting CDN images.
  • Tracking assets in version control is a good thing. As a project grows and changes it's a more sustainable way to manage and track changes by multiple users.
  • If an image applies to a specific revision of the software, it may be preferable to link an immutable image. That way, if the image is later updated to reflect changes to the software, anyone reading that revision's readme will find the correct image.

My preferred solution, inspired by this gist, is to use an assets branch with permalinks to specific revisions.

git checkout --orphan assets
git reset --hard
cp /path/to/cat.png .
git add .
git commit -m 'Added cat picture'
git push -u origin assets
git rev-parse HEAD  # Print the SHA, which is optional, you'll see below.

Construct a "permalink" to this revision of the image, and wrap it in Markdown.

Looking up the commit SHA by hand is inconvenient, however, so as a shortcut press Y to a permalink to a file in a specific commit as this help.github page says.

To always show the latest image on the assets branch, use the blob URL:

https://github.com/github/{repository}/blob/assets/cat.png 

(From the same GitHub help page File views show the latest version on a branch)

Share:
1,535,316
Midhun MP
Author by

Midhun MP

Coding is my passion. Working as a mobile app developer and specifically focused on ios + android + flutter + node.js I ❤️ to learn new technologies.

Updated on July 12, 2022

Comments

  • Midhun MP
    Midhun MP almost 2 years

    Recently I joined GitHub. I hosted some projects there.

    I need to include some images in my README File. I don't know how to do that.

    I searched about this, but all I got was some links which tell me to "host images on web and specify the image path in README.md file".

    Is there any way to do this without hosting the images on any third-party web hosting services?

  • mgalgs
    mgalgs over 10 years
    You should also consider using relative links
  • Lee Crossley
    Lee Crossley about 10 years
    Relative links are not a great use here, imagine your readme is also displayed on npm that does not host the image in this way - it needs to link to GitHub. Image srcs should be on the github.com domain, not the raw.github.com subdomain and not the raw.githubusercontent.com domain.
  • Colin Keenan
    Colin Keenan over 9 years
    I just tried it by right-clicking on an image in my project, copying the link, and putting it in the README.md. That gave me the github.com domain as you suggest, but although it displayed fine on github, it did not display fine on my local copy. On the other hand, using raw.github.com and deleting "blob/" from the path displayed fine on my local computer. So, I would say the accepted answer here is correct as is.
  • Colin Keenan
    Colin Keenan over 9 years
    I actually ended up going with the relative paths (which for me is just the name of the image file because I have everything in root). I had been maintaining 2 copies of the README.md, one for local installation in /usr/share/projectname/docs, and one for github. Now, I can just use the same README.md for both since the image filenames work fine in both cases. If I want to post a copy of parts of my README somewhere else, I'll either have to host the images somewhere else or put in the raw.github.com url.
  • Jack O'Connor
    Jack O'Connor over 9 years
    GitHub themselves recommend relative paths: help.github.com/articles/relative-links-in-readmes One major downside of absolute paths is that, if the image gets moved in master, other branches that still point to the old URL will break.
  • android developer
    android developer about 9 years
    How would you create a clickable image , which goes to a certain URL ?
  • X.Creates
    X.Creates about 8 years
    When you using HTML tag(s) inside you markdown file. It will be ignored by Pandoc, a universal document converter.
  • Cerin
    Cerin almost 8 years
    Note, Github's default markdown interpreter wraps the image inside an anchor tag, which may not be what you always want.
  • peterh
    peterh over 7 years
    As far as I understand you do not really need to save the issue. This way you do not really need those dummy tickets in the Issue Tracker. Question is if this is a "safe" method, meaning if GitHub will detect (after some time) that the image is orphaned and therefore delete it from the GitHub CDN ??
  • Rich
    Rich over 7 years
    Yes. This is by far the easiest way unless you're worried about performance.I'd note that this is relative to the directory not the repo, so if you have 'myimage.png' in the same dir as 'about_pics.md' then the markup is:![What is this](myimage.png)
  • Régis B.
    Régis B. about 7 years
    This is an awesome solution because 1) it works 2) images are also shown in a local viewer, with no need of internet access
  • williaster
    williaster almost 7 years
    I think that relative links break on npm package descriptions, which pull from README.mds
  • Joe Hill
    Joe Hill almost 7 years
    This is the easiest way to add them in my opinion. Simply drag them into the box, copy the address and paste it into your readme with captions below. Boom, you're done.
  • Lea Hayes
    Lea Hayes almost 7 years
    @Rich can you expand on your comment regarding performance. What performance issues are there with the relative path approach?
  • Rich
    Rich almost 7 years
    Oh, I just thought that since this retrieves the image from the repo, it's intrinsically going to do more work than having the images on a CDN or whatever.
  • Andrea Bergonzo
    Andrea Bergonzo almost 7 years
    What is the differente between [[link]] and ![alt-text](link)
  • Ryan
    Ryan over 6 years
    I found that I have to use the address listed when going to that image in Github which is different from what people are saying here. ![title](https://github.com/username/reponame/blob/master/su‌​bfolders.../filename‌​.jpg) Notice the "blob" term in the filename. The answers here didn't work for me in 2017.
  • mohessaid
    mohessaid over 6 years
    You can use relative links to show images in your readme file on Github. Check this example.
  • Cerin
    Cerin about 6 years
    This auto-wraps the image in <p><a>...</a></p> tags, which isn't ideal. Is there any way to disable that?
  • Francislainy Campos
    Francislainy Campos about 6 years
    This answer worked perfectly for me and didn't need to save the new issue within the repo. I've found a video on Youtube that explains that in details: youtube.com/watch?v=nvPOUdz5PL4.
  • Alexis Wilke
    Alexis Wilke about 6 years
    This is best so it can be centered or placed on one side (for smaller images, at least.)
  • Dims
    Dims almost 6 years
    This path is relative to what?
  • iGatiTech
    iGatiTech almost 6 years
    @captainclam I have implemented your solution and it's worked like a charm. But I am facing one issue, gif is playing only once. While in my local machine it is playing on repeat mode. I don't know why GitHub plays it only once. Any Help is appreciated! Thank you
  • resultsway
    resultsway almost 6 years
    Your github folder is the root so its relative to the root of the github folder. if you have $HOME/mygithub_folder/doc/img.jpg? then just use doc/img.jpg in your $HOME/mygithub_folder/README.md
  • friederbluemle
    friederbluemle almost 6 years
    How do you specify the relative path? It's not relative to README.md (in all cases?).
  • Slavik Shynkarenko
    Slavik Shynkarenko almost 6 years
    Currently suggested domain doesn’t seem to work and the link should be like this: https://github.com/[username]/[reponame]/blob/[branch]/image‌​.jpg?raw=true
  • Dan
    Dan almost 6 years
    Love this tool!
  • dojuba
    dojuba over 5 years
    This is a good complement to the accepted answer. Assets are tracked, images are not in master, no clutter. Just be careful about the git reset --hard; make sure changes were committed.
  • xiaodai
    xiaodai over 5 years
    relative to what?
  • Dims
    Dims over 5 years
    @resultsway so relative/path/to/img.jpg is the same as /relative/path/to/img.jpg?
  • Sabuncu
    Sabuncu over 5 years
    This answer works, and requires little effort. The issue can be discarded, but the URL for the image persists. The image URL is in the form of: user-images.githubusercontent.com...
  • Johnny Oshika
    Johnny Oshika over 5 years
    This is a great trick! But does anyone know the answer to @peterh's question? Will GitHub periodically purge images that are orphaned (i.e. doesn't have an issue attached to it)?
  • paulmelnikow
    paulmelnikow over 5 years
    In the wild on your machine. You can point it to wherever the file happens to be (~/Downloads, /tmp, etc.).
  • Biswajit Das
    Biswajit Das over 5 years
    Perfect and crisp.
  • KillerRabbit
    KillerRabbit over 5 years
    The ?raw=true was the secret sauce that finally got this to work for me.
  • amc
    amc over 5 years
    In the event that you do not need a caption you can leave it blank. An example may be easier to understand in this case: ![](https://url/figure.png)
  • yourbuddypal
    yourbuddypal over 5 years
    @JohnnyOshika I doubt anyone outside of github could definitively answer that. It might be dependent on what "user-images.githubusercontent.com" is used for. If it is only for images uploaded to comments, then it is plausible that they could scan for the missing data relation and remove the image. If its more general, it may be difficult to definitely determine that there are no references to the file, making it either computationally expensive or challenging to determine that it can be deleted without issues.
  • kingthorin
    kingthorin over 5 years
    Has anyone figured out how to get this working in a preview or PR?
  • AlienKevin
    AlienKevin over 5 years
    As pointed out by Lee Crossley, you should avoid relative links because they break when your readme is displayed on npm. Instead, link to image src on github.com domain with "?sanitize=true" flag if the image format is something like SVG. See more details at this SO answer: stackoverflow.com/a/16462143/6798201
  • mhenry1384
    mhenry1384 about 5 years
    It didn't work for me until I removed the query string.
  • cdsaenz
    cdsaenz almost 5 years
    This has worked for me. Having an image in a folder "apps" under the README.md I've used: ![](images/ss_apps.png)
  • cdsaenz
    cdsaenz almost 5 years
    It's relative to the README.md position. I have that readme in a docs folder and then images below with my screenshots, so I do ![Image](images/ss_apps.png)
  • Raja
    Raja almost 5 years
    The fact that this is the easiest way should be an embarrassment to GitHub. After 15 minutes messing about with Wiki, gh-pages and github.io, I'm just going to use this solution (again).
  • mhucka
    mhucka almost 5 years
    The use of tables is irrelevant to the topic of the OP's question.
  • JChat
    JChat almost 5 years
    Awesome. I found this to be the easiest too.
  • Danila Polevshchikov
    Danila Polevshchikov almost 5 years
    At least for .png and raw.githubusercontent.com sanitize=true doesn't work and should be removed. raw=true works fine. github.com/sphinxy/AppDynamics.Playground/blob/master/README‌​.md
  • Dhiren Hamal
    Dhiren Hamal over 4 years
    Relative path worked for me in github repository ![alt text](/resources/images/roadmap.png)
  • UpaJah
    UpaJah over 4 years
    adding screenshots of an app can be done this way nicely, thanks
  • Harsh Patel
    Harsh Patel over 4 years
    You can Upload image to Repo and then go to image, click on download and get the link and add it to the readme file: ![alt text](link to download image)
  • Luk Aron
    Luk Aron over 4 years
    it works, Actually, I think this answer should be accepted
  • Luk Aron
    Luk Aron over 4 years
    I just would like to say that, it is not safe even for the hosting service in this way, you don't even know which day suddenly they will stop hosting your image. Simply put the image in the repo and follow this. stackoverflow.com/a/53771381/10189759
  • Tammi
    Tammi over 4 years
    I am surprised that only Aditya mentioned that spaces are a problem, and in a rather stupid way too - no errors, but your syntax is simply displayed like no such feature would even exist. Upvote from me for mentioning this.
  • Shridutt Kothari
    Shridutt Kothari over 4 years
    I too love this tool, this should be Git's default feature!!
  • Welcor
    Welcor over 4 years
    @LukAron you think, or you know it is not save?
  • Luk Aron
    Luk Aron over 4 years
    @Blechdose Just by my first intuition, the possibility of losing that host image is considerable.
  • jeffmcneill
    jeffmcneill over 4 years
    Use CSS instead of Tables, it will rock your world.
  • Thomas Browne
    Thomas Browne about 4 years
    This is the only one that worked flawlessly for me. No confusion about paths, whether "master" refers to a directory or a branch, where the root start is, etc. Only caveat is Firefox (72) does not allow you to open image in separate tab so this is Chrome only for now I think.
  • Rory O'Kane
    Rory O'Kane about 4 years
    rawgit.com shut down in 2018 and should not be used any more; you should remove it from this answer. raw.githubusercontent.com, which you also mention, is the closest alternative.
  • Shadoninja
    Shadoninja about 4 years
    I swear I remember there being a directory that you can make that doesn't show up on Github. Until I can track that down, this post seems like the next best thing to use. Thanks!
  • phi
    phi almost 4 years
    Thanks, a relative path of images/file.png worked for me. Just to point out: I had to add ?raw=true to the URL. Otherwise the image was not displayed (but a broken icon).
  • Abhinav Dobhal
    Abhinav Dobhal almost 4 years
    Here (screenshot.png) is relative path from readme.md file. e.g., I had (images/landing_page.png)
  • Samega 7Cattac
    Samega 7Cattac almost 4 years
    uploading to an issue isn't needed, upload to ur repo (recommended) or use an image hosting provider.
  • Kavindu Gayantha
    Kavindu Gayantha over 3 years
    adding more images like this is not worked for me. Is there any other way to do that?
  • Mattwmaster58
    Mattwmaster58 over 3 years
    strongly suggest adding alt-text to make things more accessible for people using screenreaders
  • Ashish Kamble
    Ashish Kamble over 3 years
    Raw version comes with?token=TOKEN it stops working after a while
  • Ari K
    Ari K over 3 years
    This is awesome!
  • ArtuX
    ArtuX about 3 years
    I don't get what is the advantage of creating new branch for assets. Why not just keep the assets in the same branch, with files that use those assets?
  • carloswm85
    carloswm85 about 3 years
    There's a permalink option for images in the repos. Go to the image, and you'll find it among the options of it.
  • Joonas
    Joonas about 3 years
    I've used this for image hosting and have had the images disappear without warning. Highly recommend hosting within a repo over this
  • paulmelnikow
    paulmelnikow about 3 years
    There's less clutter that way.
  • Leherenn
    Leherenn over 2 years
    Also works for Gitlab.
  • Andreea
    Andreea over 2 years
    This is amazing!
  • user75648
    user75648 over 2 years
    Really the most useful answer. Even I was searching for this, but most of the answers I got involved uploading the image somewhere like in issues/repo etc. & providing a link to it. This one really helps!
  • gmdev
    gmdev over 2 years
    This might be obvious to others, but make sure you're editing the README file on the GitHub repository on the web. It won't work in your local IDE, you have to manually edit the README file on the repo and then drag and drop. Thanks for the answer!
  • udorb b
    udorb b about 2 years
    this best way edit read me > copy pic > paste > save
  • The Dembinski
    The Dembinski about 2 years
    I can confirm this worked - the editor in github handled cmd+v very cleanly and uploads the image
  • Bidisha Das
    Bidisha Das about 2 years
    This is the most important answer imo