I have some C++ source files in a GitHub repository which have an extension of .C and are using C syntax highlighting, which I don't want.

How can I set GitHub to apply C++ syntax highlighting to these files?

Is there a method to how GitHub identifies the source language for syntax highlighting purposes?

It looks like it uses only the file extension, but is there a way to force a particular language?

5 Answers 5


You can now force the language of any file in your repositories using Linguist overrides.

GitHub Linguist is the open-source library used to detect the language of files across Github.

To force your .c files to be highlighted using the C++ grammar, you can add the following to your .gitattributes file:

*.c linguist-language=C++
  • I've tried this for *.S linguist-language=asm to no avail....
    – mckenzm
    Commented Jul 18, 2018 at 11:33
  • @mckenzm It seems your message got truncated. The .gitattributes line you posted should force Linguist to recognize all .S files as Assembly. If it doesn't work, I can have a look at the repository to try to see what's happening (do you have a link?).
    – pchaigno
    Commented Jul 18, 2018 at 16:02
  • Excellent this was exactly the answer I was looking for. This worked exactly as advertised. Thanks! :)
    – NHDaly
    Commented Jan 15, 2021 at 15:39
  • 2
    You may need to change the source file(s) after committing the .gitattributes file for highlighting to kick in.
    – skozin
    Commented Apr 8, 2021 at 11:37

I asked [email protected]; this is their reply:

We use Pygments (http://pygments.org/) to do syntax highlighting and determine which lexer to use based on each file's extension. So unfortunately, it doesn't look like there's a way to get C++ highlighting without renaming the files from *.c -> *.cpp and *.h -> *.hpp.

You can do this without losing git history of a file by using the git mv command.

  • 1
    This answer is now outdated. GitHub doesn't use Pygments anymore and there's a way to override Linguist's classification results.
    – pchaigno
    Commented Dec 29, 2021 at 15:02

GitHub's language detection is done by the Linguist module, which is conveniently open source. It relies primarily on the file extension to detect the language, although it can be a bit clever to detect ambiguous files (such as .h files). As you can see from the configuration file, .c is firmly defined as a C file. Given the number of files that have to be assessed on GitHub, efficiency is a key requirement, if at the cost of some accuracy.

It looks like per repo configurations have been ruled out by the developer, so the only way you could make files syntax highlight as C++ would be to use C++ extensions. To keep those extensions and have highlighting you may have to try another service, or host the code somewhere yourself.

Update Oct 2014: GitHub haven't stood still, in the last year they have introduced some basic heuristics to help determine the language in the file. More specific to this questions, .c files are now checked to see if they are C, C++ or Objective-C.

GitHub support's answer is a little interesting, they do use Pygments for highlighting and the lexer, but the rulesets are in their own Linguist module. Which you'd hope they'd know!


For files with a Shebang, the Shebang is considered when determining the language but seems to be evenly weighted against other tokens. This seems to be a big error because the Shebang should definitively define the language of the file. This can cause issues with highlighting.

As a workaround you can add dummy tokens in the form of a comment to "tip the scales" in favor of the correct language. This is experimental but I have had luck with it.


Sharing my answer here I discovered elsewhere on SO.

I discovered that you can add a vim or emacs modeline per the Linguist readme to the top of your source file (unfortunately, required for each file) to coerce syntax highlighting for Github.com. Based on the languages.yml file, I think you'd need to add /* vim: syntax=C++ */ to your source file.

  • This is good progress. One would think the company behind VSCode that now supports the most obscure syntax with extensions could extend that sort of choice to GitHub. It can't be any worse than RCS comments.
    – mckenzm
    Commented Aug 5, 2021 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.