184

Let's say I have a branch named feature/1. And also issue #1. I want to link that branch to that issue.

Is there a way to link that branch to that issue from the issue? Without making a commit.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Nerian
  • 15,901
  • 13
  • 66
  • 96
  • Is there any update on this since Github allows "Creating a branch to work on an issue"? Can I link an branch later? – Lars Flieger Aug 08 '22 at 05:52

6 Answers6

115

Directly from GitHub:

References

Certain references are auto-linked:

  • SHA: be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2
  • User@SHA ref: mojombo@be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2
  • User/Project@SHA: mojombo/god@be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2
  • #Num: #1
  • User/#Num: mojombo#1
  • User/Project#Num: mojombo/god#1

It seems that directly (as in user/repo/branch) is not possible, but maybe by using the id of the tree?

Community
  • 1
  • 1
fuzzyalej
  • 5,903
  • 2
  • 31
  • 49
  • 98
    I contacted Github's support and they told me it is not possible right now. – Nerian Nov 04 '11 at 22:38
  • 8
    Really wish this was possible. A work around this would be to use pull requests (instead of directly pushing) and reference the issue from within the pull request. – Olivier Lalonde Nov 17 '12 at 07:34
  • 4
    Hmm, makes sense as branch names aren't static but commits are. – jonasfj Feb 09 '14 at 07:39
  • 6
    @jonasfj The Github URL for a branch is static though - it's always `/user/repo/tree/branch-name` (or `/user/repo/commits/branch-name` depending on what you want to view) – GMA Sep 30 '15 at 13:39
  • It is possible, as shown in [my answer](/a/29798733/543738) to this question. – Mr. Lance E Sloan Jul 10 '16 at 06:33
112

As mentioned in another answer, GitHub automatically makes links to various things, including other GH repositories, but not to branches within those repositories. When I want to do this, I manually make the link like this:

[a link to a branch](/_user_/_project_/tree/_branch_)

Where _user_, _project_, and _branch_ should be replaced with the parts of the branch's URL. For example, a branch in GitHub's "linguist" project:

[api-changes branch in github/linguist](/github/linguist/tree/api-changes)

Important:

  • GitHub's Markdown processor creates links using the exact URL value enclosed within parentheses. You must specify the URL considering how a browser would handle that link. If the URL specified is only the path portion (as in this example), browsers will treat the path as relative to the current page's URL. If that relative path begins with a slash ("/", also as in this example), then it will be relative to the root of the server of the current page's URL. Otherwise, paths that don't begin with slash will be treated as relative to the parent of the current page's URL. (This is basic HTML link specification.)
  • Do not forget to include the tree part of the URL when referring to specific branches of projects.
Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50
  • 13
    I currently get my link converted to `/user/proj/blob/branch` even if I wrote `tree` and not `blob`. Edit: working: `../../tree/branch` – vault Feb 24 '17 at 19:47
  • @vault Can you post a link here to the Markdown document containing an example of this? – Mr. Lance E Sloan Jun 07 '17 at 14:34
  • @LS if I remember correctly, I needed this information to write [this README](https://github.com/seguri/django-polls-tutorial). – vault Jun 08 '17 at 13:46
  • 4
    @vault I see! Apparently there're different rules for links from MD repo files vs. from MD issue text. The orig. Q and my A are about links in issue text, but I'll update my answer to include MD repo files. I [forked your repo](https://github.com/sloanlance/django-polls-tutorial) and added a link to `README.md`. GH seems to strip leading "/" from the link and append it to the current URL. That may be a bug. I'll check on it. Anyway, relative URLs with ".." make more sense in repo files. E.g., if you merged my fork, my new link in `README.md` would refer back to my fork. – Mr. Lance E Sloan Jun 08 '17 at 20:32
21

Since Sept. 2022, you can link a branch from an issue.

Nov. 2022, since gh 2.19.0 and the gh issue develop command:

$ gh issue develop 123 --name "my-branch" --base my-feature # create a branch for issue 123 based on the my-feature branch

$ gh issue develop 123 --checkout # fetch and checkout the branch for issue 123 after creating it

$ gh issue develop --list 123 # list branches for issue 123
$ gh issue develop --list --issue-repo "github/cli" 123 # list branches for issue 123 in repo "github/cli"
$ gh issue develop --list https://github.com/github/cli/issues/123 # list branches for issue 123 in repo "github/cli"

But, as noted by NotX in the comments (in Dec. 2022):

The new linking feature won't work for branches in other repos though.

So if you have a dedicated issues repo holding the issues from all the other repos, you've still go via mentioning.

But, as noted by L0tad in the comments, this is again true in July 2023:

Issues can be linked to branches from other repos.
I just did so myself for this exact use case, where the issue was located in a dedicated issues repo, and development took place elsewhere.


Note that from April 2013 ("Branch and Tag Labels For Commit Pages"):

  • Any commit can mention the branch it is part of:

branch part of commit

If the commit is not on the default branch, the indicator will show the branches which contain the commit. If the commit is part of an unmerged pull request, a link will be shown.

Link to pull request

That means referencing a commit from the issue will allow the user to see the branch (by looking at the commit), and even to see a link back to the issue (still by looking at the commit).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The new linking feature won't work for branches in other repos though. So if you have a dedicated `issues` repo holding the issues from all the other repos, you've still go via mentioning. – NotX Dec 20 '22 at 17:56
  • 1
    @NotX Thank you for the feedback. I have included your comment in the answer for more visibility. – VonC Dec 20 '22 at 18:31
  • @NotX this is not true (at least any more, as of July 2023). Issues *can* be linked to branches from other repos. I just did so myself for this exact use case, where the issue was located in a dedicated issues repo, and development took place elsewhere. – L0tad Jul 12 '23 at 16:32
  • @L0tad Thank you for your feedback. I will include it in the answer [as soon as I am able to](https://meta.stackoverflow.com/q/425430/6309). – VonC Jul 12 '23 at 17:49
  • 1
    @L0tad Yeah, you're right. Seems like they've added it in the recent moth! :) – NotX Jul 12 '23 at 20:05
  • 1
    @L0tad This morning, the edits are working?! I have added your comment to the answer. – VonC Jul 13 '23 at 05:07
11

You can't reference the branch directly.

But you can make a reference to a branch compared to another branch. If you enter this:

https://github.com/user1/repo/compare/branch1...branch2

it will render as

branch1...branch2

Also, you can compare branches across forks. If user2 has forked repo, this works:

https://github.com/user1/repo/compare/branch1...user2:branch2
Lars Brinkhoff
  • 13,542
  • 2
  • 28
  • 48
0

Probably the easiest way is the create the issue first and create the branch in Github.

There is a "Development" section in the issue details, where you can create it automatically with a proper naming or link an existing branch.

milpataki
  • 471
  • 4
  • 4
-1

I was looking for the same possibility in Git, but nothing was available so I decided to directly refer the brach with a markdown link by using the Hash reference.

# Issue02

## Commit Hash _<hash_number>_

...Rest of comments in the issue...

So, now in my organization we always do that reference when opening or closing an issue. It must be refered to a certain Hash and indirectly it is refered to the corresponding branch.

I guess you already now, but if not, to get the hash you will use git log

Note: It is not referencing to a certaing branch but a commit,

With some hours of work I think it is possible to automatically do this and create a command line tool,