Can you get the number of lines of code from a GitHub repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
GitHub does not display the total lines of code (LOC) for a repository directly. To get this number, use cloc (Count Lines of Code) after cloning the repo, the GitHub API's statistics endpoint, or a quick git ls-files | xargs wc -l command. Each method has different tradeoffs in accuracy, setup, and what it counts.
Method 1: Using cloc (Most Accurate)
cloc is the gold standard for counting lines of code. It differentiates between code, comments, and blank lines, and it recognizes over 250 programming languages.
Install cloc
Clone and Count
Example output:
Useful cloc Options
Method 2: Using the GitHub API
The GitHub API provides language byte counts, which you can use to estimate lines of code without cloning.
Get Language Statistics
Output:
These numbers are bytes, not lines. To estimate lines, divide by an average of 35-45 bytes per line (varies by language):
Get Contributor Statistics (Includes Additions/Deletions)
This returns weekly additions and deletions. Summing all additions minus deletions gives the current net LOC, though this includes all file types.
Method 3: Quick git Command (No Extra Tools)
If you already have the repository cloned, use this one-liner:
This counts every line in every tracked file, including comments, blank lines, configuration files, and generated code. It is fast but not precise.
Improved Version: Filter by File Type
Handle Filenames with Spaces
The -z flag uses null bytes as delimiters instead of newlines, preventing errors with filenames that contain spaces.
Method 4: GitHub Linguist (What GitHub Uses)
GitHub uses Linguist internally to compute the language bar you see on repository pages. You can run it locally:
Linguist respects .gitattributes rules, so vendor files and generated code are excluded by default (matching what GitHub shows).
Method 5: tokei (Fast Alternative to cloc)
tokei is a Rust-based LOC counter that is significantly faster than cloc on large repositories:
Example output:
Method 6: Without Cloning (Remote Analysis)
If you do not want to clone the repository:
Using the GitHub Stats Page
Navigate to https://github.com/{owner}/{repo}/graphs/contributors. This page shows additions and deletions per contributor. The "Additions" column roughly indicates total lines ever written (not current LOC).
Using codetabs API
This free API returns LOC without requiring you to clone. Rate limits apply.
Using scc via Docker
Comparison of Methods
| Method | Accuracy | Speed | Needs Clone? | Differentiates Code/Comments? | Setup |
cloc | High | Medium | Yes | Yes | Install package |
tokei | High | Fast | Yes | Yes | Install package |
scc | High | Fast | Yes | Yes | Install package |
git ls-files + wc | Low | Fast | Yes | No | None |
| GitHub API | Estimate | Fast | No | No | API token |
| GitHub Linguist | High | Slow | Yes | No (counts all lines) | Ruby + gem |
| codetabs API | Medium | Fast | No | Yes | None |
Counting Lines for a Specific Commit or Branch
Automating LOC Tracking in CI
You can track LOC trends over time by adding a CI step:
Common Pitfalls
- Counting
node_modulesorvendordirectories: These contain third-party code and inflate your LOC by orders of magnitude. Always exclude dependency directories:cloc . --exclude-dir=node_modules,vendor,dist. - Confusing bytes with lines: The GitHub API returns bytes per language, not lines. A rough conversion is 35-45 bytes per line, but this varies significantly. Do not report API byte counts as line counts.
- Counting generated code: Build artifacts, compiled outputs, and auto-generated files (like
package-lock.json) can add hundreds of thousands of lines. Use.gitattributesorcloc's--exclude-extto skip them. - LOC as a productivity metric: Lines of code is a measure of codebase size, not developer productivity or code quality. A refactoring that reduces LOC by 30% can be more valuable than one that adds 10,000 lines.
- Shallow clones missing history:
git clone --depth 1works fine for current LOC counting, butcloc --diffand history-based analysis require full history. Use a full clone for those operations. - Rate limiting on the GitHub API: Unauthenticated requests are limited to 60 per hour. Authenticate with a personal access token for 5,000 requests per hour.
Summary
- GitHub does not show total LOC directly. Use
cloc,tokei, orsccfor accurate counts after cloning. cloc . --exclude-dir=node_modules,distis the most commonly used command for an accurate count.- For a quick estimate without extra tools, use
git ls-files '*.py' '*.js' | xargs wc -l. - The GitHub API at
/repos/{owner}/{repo}/languagesreturns byte counts per language, not line counts. Divide by roughly 40 for a line estimate. - Always exclude vendor/dependency directories and generated files from your count.
tokeiandsccare faster alternatives toclocfor very large repositories.

