26

I am trying to create links in my README.md that link to example code using a relative path, as opposed to the absolute path I am using now. For example, in the markdown file, if I use this:

[someLabel](https://github.com/username/repoName/somePathTo/myExampleCode)

It works no problem. But I would like to link it up using something like this:

[someLabel](username/repoName/somePathTo/myExampleCode)

This does not link up, as it gives me the ol' 404 not found error. Is there a way to link my examples on Github to my README using a relative path?

Houdini
  • 3,442
  • 9
  • 31
  • 47

5 Answers5

32

They seem to have changed this since Aug 8 '12, so now all I had to do to reference my pdf file inside the Documentation folder (which is in my project's root) is this:

[file.pdf](Documentation/file.pdf)

So, no need to add blog/master part anymore. Hope it helps new people browsing this question.

Nikola
  • 14,888
  • 21
  • 101
  • 165
  • If I create a readme directory with a README.md and a Documentation directory with a file.pdf this will work perfectly but if the README.md is at the root of my GitHub project then it displays the source code instead. :( – Dave Mar 21 '21 at 12:24
6

Try leaving off the username/ part. A relative link to repoName/blob/master/whatever on a page at /username/repoName should take you to /username/repoName/blob/master/whatever, which is what you want, I think.

John MacFarlane
  • 8,511
  • 39
  • 33
  • Thanks for the response. Still havent gotten it working, I am not sure there is a way to do it. I tried `repoName/tree/master/pathto/mypage` (copied from the browser, just removed username) but still get that not found code. – Houdini Aug 09 '12 at 14:13
  • 1
    When you get the 404 page, what URL shows up in your browser? (Alternatively, what is the URL that shows up in your status bar when you hover the mouse over the link?) – John MacFarlane Aug 13 '12 at 01:11
  • Just realized I never updated this, but yes John that got it working. Also those reading see Nikola's update. – Houdini Jul 29 '13 at 21:31
6

Here is a nuance. A leading slash changes the meaning of a link.

[someLabel](/repository/relative/link.txt)

[someLabel](directory/relative/link.txt)
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
0

I recently found out that the way you can use relative path in README.md is:

[file.pdf](/Documentation/file.pdf)

where Documentation is a folder at the root of my repository

Nin
  • 99
  • 1
  • 8
0

I have search in google about your question and found your question and after reading all answers, I found another way we can add relative path in README.md file by this way,

[someLabel](./somePathTo/myExampleCode)

Add the leading ./ character when the path is pointing to a parent folder. It will not give '404 not found error' in Github.

Chetan Nada
  • 86
  • 1
  • 4