ChatGPT解决这个技术问题 Extra ChatGPT

Link to the issue number on GitHub within a commit message

Is it somehow possible to automatically have a link to GitHub issue number in the git commit message?


D
Dave Powers

Just include #xxx in your commit message to reference an issue without closing it.

With new GitHub issues 2.0 you can use these synonyms to reference an issue and close it (in your commit message):

fix #xxx

fixes #xxx

fixed #xxx

close #xxx

closes #xxx

closed #xxx

resolve #xxx

resolves #xxx

resolved #xxx

You can also substitute #xxx with gh-xxx.

Referencing and closing issues across repos also works:

fixes user/repo#xxx

Check out the documentation available in their Help section.


Fix issue #xxx doesn't work for me, any ideas? It references the issue, but doesn't close it.
@Dennis remove the word "issue"
@JamesTomasino that's possible - I've noticed that this hasn't been working for me when I'm working on a branch called dev.
i'm not going to be the person who moves this answer from 666 votes to 667, but this was VERY helpful.
Worked for me. The only thing I can say about this is when I see xxx, I assume it is a 3 digit number, but obviously fixes #9 for instance will work. It would be nice if the answer makes this explicitly clear that it is for any length number and you do not need to pad with zeros.
D
Dave Powers

If you want to link to a GitHub issue and close the issue, you can provide the following lines in your Git commit message:

Closes #1.
Closes GH-1.
Closes gh-1.

(Any of the three will work.) Note that this will link to the issue and also close it. You can find out more in this blog post (start watching the embedded video at about 1:40).

I'm not sure if a similar syntax will simply link to an issue without closing it.


You can just use the number of the issue (for example #456) it will link to the task without closing it.
I would choose "gh-1" over "#1" simply because you never know if the repository gets exported/mirrored to somewhere other than github. Then, the "#1" won't make much sense.
@mipadi: is the . after "Closes GH-1` necessary? Also, is it case-sensitive?
@Lekensteyn: I don't believe the period is necessary. Not sure about case-sensitivity.
message (closes GH-28) works for me, not sure if everything is case-insensitive.
H
Henrik Lindberg

github adds a reference to the commit if it contains #issuenbr (discovered this by chance).


just tested it, works like a charm, thanks... this is the one that should be marked as correct answer...
this should be part of the accepted answer. sometimes you just want to mention the issue and are not doing anything to it yet.
n
narkeeso

You can also cross reference repos:

githubuser/repository#xxx

xxx being the issue number


D
Dave Powers

they have an nice write up about the new issues 2.0 on their blog https://github.blog/2011-04-09-issues-2-0-the-next-generation/

synonyms include

fixes #xxx

fixed #xxx

fix #xxx

closes #xxx

close #xxx

closed #xxx

using any of the keywords in a commit message will make your commit either mentioned or close an issue.


That was already in my list, I do not think they are case sensitive.
S
Suhas Srivats Subburathinam

In order to link the issue number to your commit message, you should add: #issue_number in your git commit message.

Example Commit Message from Udacity Git Commit Message Style Guide

feat: Summarize changes in around 50 characters or less

More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of the commit and the rest of the text as the body. The
blank line separating the summary from the body is critical (unless
you omit the body entirely); various tools like `log`, `shortlog`
and `rebase` can get confused if you run the two together.

Explain the problem that this commit is solving. Focus on why you
are making this change as opposed to how (the code explains that).
Are there side effects or other unintuitive consequenses of this
change? Here's the place to explain them.

Further paragraphs come after blank lines.

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded
   by a single space, with blank lines in between, but conventions
   vary here

If you use an issue tracker, put references to them at the bottom,
like this:

Resolves: #123
See also: #456, #789

You can also reference the repositories:

githubuser/repository#issue_number

It makes no sense (and actually annoys me) that they use "feat" as an abbreviation for "feature", especially when at the same time they use "refactor" which is even longer than "feature".
@MichelJung you could argue that feat is used more often than refactor, also there's no obvious abbreviation for refactor (ref could mean reference, rf is too unclear, etc.).
B
Bananeweizen

Just as addition to the other answers: If you don't even want to write the commit message with the issue number and happen to use Eclipse for development, then you can install the eGit and Mylyn plugins as well as the GitHub connector for Mylyn. Eclipse can then automatically track which issue you are working on and automatically fill the commit message, including the issue number as shown in all the other answers.

For more details about that setup see http://wiki.eclipse.org/EGit/GitHub/UserGuide


o
omnikron

One of my first projects as a programmer was a gem called stagecoach that (among other things) allowed the automatic adding of a github issue number to every commit message on a branch, which is a part of the question that hasn't really been answered.

Essentially when creating a branch you'd use a custom command (something like stagecoach -b <branch_name> -g <issue_number>), and the issue number would then be assigned to that branch in a yml file. There was then a commit hook that appended the issue number to the commit message automatically.

I wouldn't recommend it for production use as at the time I'd only been programming for a few months and I no longer maintain it, but it may be of interest to somebody.


I think your answer is trying to address the exact question from the OP, i.e. "a way to automatically have a link to the issue added in the commit". All other answers rely on programmer remembering to add "Fixes #..., Resolved #... etc." phrase to the commit and this is not going to happen each time as we know it. Upvoting.