[github] GitHub relative link in Markdown file

Is there a way to create a URL anchor, <a>, link from within a Markdown file, to another file within the same repository and branch (aka a link relative to the current branch)?

For example, in the master branch I have a README.md file, which I would like do something like:

# My Project
is really really cool. My Project has a subdir named myLib, see below.

## myLib documentation
see documentation [here](myLib/README.md)

This would allow me to link from one .md to another within the same branch and not have to worry about which branch I'm in (avoid having to do an absolute URL that includes the github.com branch name).

Here is a working example of what I mean:

  1. GOTO http://github.com/rynop/testRel, link does not work.
  2. GOTO http://github.com/rynop/testRel/blob/master/README.md, link works.

This is expected because at this point the starting URL is in the branch. Now how do I get it to pick up the current branch in the README.md at the root of the repository?

Update: I opened an issue against GitHub for this feature request.

This question is related to github markdown github-flavored-markdown

The answer is


This question is pretty old, but it still seems important, as it isn't easy to put relative references from readme.md to wiki pages on Github.

I played around a little bit and this relative link seems to work pretty well:

[Your wiki page](../../wiki/your-wiki-page)

The two ../ will remove /blob/master/ and use your base as a starting point. I haven't tried this on other repositories than Github, though (there may be compatibility issues).


You can link to file, but not to folders, and keep in mind that, Github will add /blob/master/ before your relative link(and folders lacks that part so they cannot be linked, neither with HTML <a> tags or Markdown link).

So, if we have a file in myrepo/src/Test.java, it will have a url like:

https://github.com/WesternGun/myrepo/blob/master/src/Test.java

And to link it in the readme file, we can use:

[This is a link](src/Test.java)

or: <a href="src/Test.java">This is a link</a>.

(I guess, master represents the master branch and it differs when the file is in another branch.)


For example, you have a repo like the following:

project/
    text.md
    subpro/
       subtext.md
       subsubpro/
           subsubtext.md
       subsubpro2/
           subsubtext2.md

The relative link to subtext.md in text.md might look like this:

[this subtext](subpro/subtext.md)

The relative link to subsubtext.md in text.md might look like this:

[this subsubtext](subpro/subsubpro/subsubtext.md)

The relative link to subtext.md in subsubtext.md might look like this:

[this subtext](../subtext.md)

The relative link to subsubtext2.md in subsubtext.md might look like this:

[this subsubtext2](../subsubpro2/subsubtext2.md)

The relative link to text.md in subsubtext.md might look like this:

[this text](../../text.md)

Just wanted to add this because none of the above solutions worked if target link is directory with spaces in it's name. If target link is a directory and it has space then even escaping space with \ doesn't render the link on Github. Only solution worked for me is using %20 for each space.

e.g.: if directory structure is this

Top_dir
|-----README.md
|-----Cur_dir1
      |----Dir A
           |----README.md
      |----Dir B
           |----README.md

To make link to Dir A in README.md present in Top_dir you can do this:

[Dir 1](Cur_dir1/Dir%20A)

I am not sure if I see this option here. You can just create a /folder in your repository and use it directly:

[a relative link](/folder/myrelativefile.md)

No blob or tree or repository name is needed, and it works like a charm.


You can use relative URLs from the root of your repo with <a href="">. Assuming your repo is named testRel, put the following in testRel/README.md:

# My Project
is really really cool. My Project has a subdir named myLib, see below.

## myLib docs
see documentation:
    * <a href="testRel/myLib">myLib/</a>
    * <a href="testRel/myLib/README.md">myLib/README.md</a>

As of January 31, 2013 Github markdown supports relative links to files.

[a relative link](markdown_file.md)

However, there are a few deficiencies that have been discussed in this comment thread.

As an alternative, you can use Gitdown to construct full URLs to the repository and even make them branch aware, e.g.

{"gitdown": "gitinfo", "name": "url"} // https://github.com/gajus/gitdown
{"gitdown": "gitinfo", "name": "branch"} // master

Gitdown is a GitHub markdown preprocessor. It streamlines common tasks associated with maintaining a documentation page for a GitHub repository, e.g. generating table of contents, including variables, generating URLs and getting information about the repository itself at the time of processing the input. Gitdown seamlessly integrates with your building scripts.

I am the author of the Gitdown library.


Just follow the format below.

[TEXT TO SHOW](actual URL to navigate)

GitHub could make this a lot better with minimal work. Here is a work-around.

I think you want something more like

[Your Title](your-project-name/tree/master/your-subfolder)

or to point to the README itself

[README](your-project-name/blob/master/your-subfolder/README.md)

If you want a relative link to your wiki page on GitHub, use this:

Read here: [Some other wiki page](path/to/some-other-wiki-page)

If you want a link to a file in the repository, let us say, to reference some header file, and the wiki page is at the root of the wiki, use this:

Read here: [myheader.h](../tree/master/path/to/myheader.h)

The rationale for the last is to skip the "/wiki" path with "../", and go to the master branch in the repository tree without specifying the repository name, that may change in the future.


Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to markdown

How to add empty spaces into MD markdown readme on GitHub? how to make a new line in a jupyter markdown cell How do I display local image in markdown? How to markdown nested list items in Bitbucket? How to apply color in Markdown? How to right-align and justify-align in Markdown? Is there a way to add a gif to a Markdown file? How to add new line in Markdown presentation? How link to any local file with markdown syntax? How to insert a line break <br> in markdown

Examples related to github-flavored-markdown

How to draw checkbox or tick mark in GitHub Markdown table? Is there a way to add a gif to a Markdown file? How to add footnotes to GitHub-flavoured Markdown? Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g How can one display images side by side in a GitHub README.md? Is there a way to get colored text in GitHubflavored Markdown? github markdown colspan Highlight Bash/shell code in Markdown files How to write lists inside a markdown table? How to add images to README.md on GitHub?