ChatGPT解决这个技术问题 Extra ChatGPT

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:

GOTO http://github.com/rynop/testRel, link does not work. 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.

Finally: relative links are now supported! I have updated my answer :)
You can find some solutions in this question: stackoverflow.com/questions/40422790/…

V
VonC

Update 30th, January 2013, 16 months later:

GitHub Blog Post Relative links in markup files:

Starting today, GitHub supports relative links in markup files. Now you can link directly between different documentation files, whether you view the documentation on GitHub itself, or locally, using a different markup renderer. You want examples of link definitions and how they work? Here's some Markdown for you. Instead of an absolute link: [a link](https://github.com/user/repo/blob/branch/other_file.md) …you can use a relative link: [a relative link](other_file.md) [a relative link](path%20with%20spaces/other_file.md) and we'll make sure it gets linked to user/repo/blob/branch/other_file.md. If you were using a workaround like [a workaround link](repo/blob/master/other_file.md), you'll have to update your documentation to use the new syntax. This also means your documentation can now easily stand on its own, without always pointing to GitHub.

Marcono1234 adds in the comments

Also useful: When the link starts with a /, it is relative to the root of the repository (regardless of whether the markdown file is nested in subdirectories)

Update December 20th, 2011:

The GitHub markup issue 84 is currently closed by technoweenie, with the comment:

We tried adding a tag for this, but it causes problems with other relative links on the site.

October 12th, 2011:

If you look at the raw source of the README.md of Markdown itself(!), relative paths don't seem to be supported.
You will find references like:

[r2h]: http://github.com/github/markup/tree/master/lib/github/commands/rest2html
[r2hc]: http://github.com/github/markup/tree/master/lib/github/markups.rb#L13

As noted in InvisibleWolf's answer, if the target link is a directory and it has space, then you need to use %20 for each space.


Hmm.. I can make a relative URL (try [link] someDir/README.md, it will make an anchor) - but I want a github branch aware relative link like: myRepo/tree/<$curBranch>/pathToMd.md. I'll probably just open a feature request with github.com
nope. See full example here github.com/rynop/testRel . I could do a relative link specifying the branch, but that defeats the purpose of what I'm trying to do in the first place. If i was linking from a .md in one subdir to another, i think it would handle relative link just fine. Problem occurs when linking from .md in root of repo.
Related: For linking from one wiki page to another, [Welcome](./wiki/Hello) works, where Hello is another wiki page in the same repo.
This works in regular wiki pages, but if you add a _Sidebar.md file there is a difference between how the links resolve on the wiki home page than the rest of the pages. I tried everything here with no luck. The only thing I have come up with so far is to use 2 different _Sidebar.md files and to organize the files into sub-directories.
Also useful: When the link starts with a /, it is relative to the root of the repository (regardless of whether the markdown file is nested in subdirectories).
m
mathsyouth

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)

One thing I've found, the links are case sensitive, which I did not expect.
What if there are spaces in my directories?
@Andy that's a Linux thing, where filenames are case sensitive
D
Dharman

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)

yep this is what i do today. I don't want to bake in the branch info into the link. It should be relative.
Is there a way to get at the subfolder using a relative link?
C
Community

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.


@Thomas Is this part of the Stack Overflow terms of use? If yes, please share a link. I am curious to know the motivation behind such a requirement.
"Post good, relevant answers, and if some (but not all) happen to be about your product or website, that’s okay. However, you must disclose your affiliation in your answers." stackoverflow.com/help/behavior
One would think this falls under the meta information that Stack Overflow is trying to avoid so much, i.e. that promotes an off-topic discussion.
k
kaushal agrawal

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)

W
WesternGun

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.)


A
Alex

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).


J
Jon Chase

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>

this not valid md syntax
this may not be valid md syntax, but this helped me other answers did not gave the desired results. kudos!!
P
Peter Mortensen

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 would think that would be the obvious way but it doesn't work for me.
P
Peter Mortensen

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.


C
Chamon Roy

Just follow the format below.

[TEXT TO SHOW](actual URL to navigate)

It doesn't answer the question, there's no any "relative link" syntax here.
M
Mahozad

You can also insert your links (relative or absolute) at the bottom to reduce clutter in text. It can also be a good option for repetitive links.

This is [my linked text].

  [my linked text]: relative/path/in/repo/to/file.txt "Optional title for mouse hover"

This is my linked text.


Thanks for the tip, it's useful inside arrays with many links !
W
Werner Diwischek

The behaviour in Github directory links in markdown files could create some headaches.

All files have an url are starting with blob,

blob/<branch>/<path>/<fileName>

whereas directories have an url starting with tree:

tree/<branch>/<path>

So no relative link to a directory should work. It points to an url that starts with a blob. Magically Github changes some relative path automatically to the tree path. Unfortunately not urls containing ".."

I investigate some time to examine the strange behaviour with Github directory links on https://github.com/fluentcodes/GitSamples-GIT/tree/github-readme-links.