How to automatically close Issue(s) when you push a commit to GitHub – 046

You might have discovered this feature accidentally if you’ve ever pushed a fix up to GitHub using the magic words in your commit message. The story goes: You’ve committed a fix and pushed it to GitHub. Next you go to the Issue to close it, but to your (pleasant) surprise, the issue is already closed! How did it know?

I put pleasant in parenthesis because I hate technological surprises. I mean surprises are good, but I want to know what caused the surprise, so I’ll know how to surprise myself again in the future!

Two things must be true for your commit message to close an issue (see GitHub help article)

  1. contains a deviation of the words fix, close, or resolve, immediately followed by #<issue-number>
  2. the commit is on the default* branch (which is master by default, unless you or a repo maintainer has changed it)

If you take away nothing else from this tip, take away this: do not try to use the word Issue, e.g. Fix Issue #3 Otherwise, it won’t work. Gets me every time.

For example #1, suppose you have a repo where you want to delete a file that isn’t being used. You or someone else has created an Issue to track this work.

Delete Class1.cs #3 issue on github

You’ve fired up Visual Studio**, deleted the file from solution explorer (and saved the .csproj so the reference to the file is gone), and opened up the Team Explorer – Changes window to commit the change. You can type in “Fix #3” as the commit message to close Issue #3 in your repo.

commit message: Fix #3

Now when you return to your Issue, you’ll see it has been automatically closed with references to the commit automatically added to the Issue timeline.

Delete Class1.cs showing as Closed with how closed messages added to timeline

Now let’s say you are like me and after years and years and years of habit (and we’re talking about habit since the good ol’ days of RAID – where my MSFTies at!?!?), you put the word Issue in between the Fix and the #3. The Issue won’t get closed and you’ll have to close it manually.

 

And yes, as this blog post title implies, you can close multiple Issues.

For non-VS users out there, let’s do a command line example. Let’s say you have two Issues out there on your repo on GitHub. I’ll use close this time***, but close(s|d), fix(es|ed), or resolve(s|d) will all work.

git commit -am "Close #4 and Close #5"

*We’ll talk more about default branches later, but if you want to confirm the default branch, go to the branches link above the language bar in your repo.
**As if I could keep myself from blogging re Visual Studio Smile This is a Windows-first, non-Windows friendly blog, meaning I’m doing everything this from a Windows point of view. At some point the series will contain lots of Windows-only tips, but for now, I’m trying to straight the right balance in being non-Windows friendly.
*** In the command prompt example, I edited a readme file. I’m using the shortcut -am to do both the git add and the git commit at same time. More on these shortcuts later!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s