git2txt Documentation

repository·main·Indexed 20 days ago

https://github.com/addyosmani/git2txt

A CLI tool and Node.js API for converting GitHub repositories into single text files. It supports full HTTPS URLs, short username/repository formats, and SSH formats. The tool recursively processes files, excluding binary data and large files by default (0.1 MB threshold), and provides formatted output with file paths and sizes for repository analysis, documentation generation, or AI training datasets.

Tokens
1.8K
Snippets
5
Records
8
Agent score
19%

What's inside git2txt

  1. Understand git2txt default behavior and output format

    main

    By default, git2txt performs the following actions:

    • Exclusions: Automatically excludes binary files and files larger than 100KB (0.1 MB).
    • Recursion: Processes files recursively through all subdirectories, while automatically excluding node_modules and .git folders.
    • Output Location: Creates the output file in the current directory, named after the repository.
    • Output Structure: The generated text file uses clear markers to separate files, including the relative path and file size.

    Output Format Example:

    ================================================================================
    File: path/to/file.txt
    Size: 1.2 KB
    ================================================================================
    
    [File contents here]
    
    ================================================================================
    File: another/file.js
    Size: 4.5 KB
    ================================================================================
    
    [File contents here]
  2. Convert a GitHub repository to a text file

    main

    Use the git2txt command followed by a GitHub repository identifier to download a repository and concatenate its contents into a single text file. The tool supports several input formats:

    • Full HTTPS URL: https://github.com/username/repository
    • Short format: username/repository
    • SSH format: git@github.com:username/repository

    It accepts URLs with or without the .git suffix and with or without trailing slashes.

    # Full HTTPS URL
    git2txt https://github.com/username/repository
    
    # Short format
    git2txt username/repository
    
    # SSH format
    git2txt git@github.com:username/repository
  3. How git2txt processes files

    main

    The file processing logic follows these rules to ensure the output is useful for text-based analysis (like LLM context or code review):

    1. Directory Traversal: It recursively walks the repository tree, skipping .git and node_modules directories.
    2. Size Filtering: By default, files larger than the --threshold (default 0.1 MB) are skipped. This can be bypassed using --include-all.
    3. Binary Detection: By default, binary files are skipped to prevent corrupting the text output. This can be bypassed using --include-all.
    4. Output Format: Each file's content is wrapped in a header block:
      ================================================================================
      File: path/to/file.ext
      Size: 1.2 KB
      ================================================================================
      
      [File Content]
  4. Use the git2txt CLI

    main

    The git2txt command-line tool converts a public GitHub repository into a single, readable text file. It clones the repository, filters out binary and large files (by default), and concatenates the remaining text content with file headers indicating the path and size.

    Basic Usage

    To convert a repository using its full URL:

    $ git2txt https://github.com/username/repository

    To convert a repository using the short user/repo format:

    $ git2txt username/repository

    To specify a custom output filename:

    $ git2txt https://github.com/username/repository --output=my_code_dump.txt
    #!/usr/bin/env node
    
    # Example CLI commands
    $ git2txt https://github.com/username/repository
    $ git2txt https://github.com/username/repository --output=output.txt
  5. Configure git2txt options

    main

    You can customize the conversion process using the following CLI flags:

    FlagLong FlagDescription
    -o--outputSpecify output file path (default: repo-name.txt)
    -t--thresholdSet file size threshold in MB (default: 0.1)
    N/A--include-allInclude all files regardless of size or type
    N/A--debugEnable debug mode with verbose logging
    N/A--helpShow help
    N/A--versionShow version
    # Example: Custom output and 2MB threshold
    git2txt username/repository --output=output.txt --threshold=2
    
    # Example: Include everything
    git2txt username/repository --include-all
  6. Programmatic API for git2txt

    main

    If you are integrating git2txt into your own Node.js application, you can use its exported functions to orchestrate the repository-to-text conversion process.

    Core Functions

    • validateInput(input: string[]): Promise<string> Validates that the provided input contains a supported GitHub repository URL or short format.

    • downloadRepository(url: string): Promise<{ tempDir: string, repoName: string }> Clones the specified GitHub repository into a temporary directory. Returns the path to the temporary directory and the repository name.

    • processFiles(directory: string, options: { threshold: number, includeAll: boolean }): Promise<string> Recursively reads the files in the provided directory. It filters files based on the threshold (in MB) and whether includeAll is true. Returns a single string containing the concatenated content of all processed files, formatted with file headers.

    • writeOutput(content: string, outputPath: string): Promise<void> Writes the generated text content to the specified file path.

    • cleanup(directory: string): Promise<void> Removes the specified directory and its contents (typically used to clean up the temporary clone directory).

  7. git2txt CLI Options Reference

    main

    The following flags are available when running the git2txt command via the CLI:

    FlagShortTypeDescription
    --output-ostringSpecify the output file path. If omitted, defaults to [repo-name].txt.
    --threshold-tnumberSet the file size threshold in MB. Files larger than this are skipped. Default is 0.1 MB.
    --include-all(none)booleanInclude all files regardless of size or whether they are binary.
    --debug(none)booleanEnable debug mode with verbose logging.
    --help(none)(none)Show help text.
    --version(none)(none)Show version.
    --output, -o     Specify output file path
        --threshold, -t  Set file size threshold in MB (default: 0.1)
        --include-all    Include all files regardless of size or type
        --debug         Enable debug mode with verbose logging
        --help          Show help
        --version       Show version