Skip to content

Instantly share code, notes, and snippets.

@codekiln
Last active January 19, 2025 11:02
Show Gist options
  • Save codekiln/9e35a14420960c999f71c11dc1902373 to your computer and use it in GitHub Desktop.
Save codekiln/9e35a14420960c999f71c11dc1902373 to your computer and use it in GitHub Desktop.
Supplying directory context to AI that respects a .gitignore

Git Alias to Supply Directory Context to AI

When collaborating with AI tools for code assistance, it's beneficial to provide them with an overview of your project's directory structure, excluding files specified in .gitignore. This ensures that the AI has relevant context without being cluttered by unnecessary or ignored files. This gist sets up the ability to use git lsf to list all relevant files and git lsfwd to list files specific to the current working directory in a way that respects .gitignore exclusions.


Setting Up the Git Aliases

Alias: lsf

You can create a Git alias named lsf to list all tracked and untracked files, excluding those ignored by Git. To set up this alias, execute the following command:

git config --global alias.lsf '!git ls-files --cached --others --exclude-standard'

Script: lsfwd

For files specific to the current working directory, create a custom script named git-lsfwd in your local PATH. This script ensures files listed are relative to the current directory within the repository.

  1. Create the script in ~/.local/bin:

    mkdir -p ~/.local/bin
    touch ~/.local/bin/git-lsfwd

    Edit ~/.local/bin/git-lsfwd and save:

    #!/bin/bash
    git lsf | grep "^$(pwd | sed "s|$(git rev-parse --show-toplevel)/||")"
  2. Make the script executable:

    chmod +x ~/.local/bin/git-lsfwd
  3. Ensure ~/.local/bin is in your PATH. Add this line to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

    export PATH=$HOME/.local/bin:$PATH

Verifying the Setup

Once the script is in place, test it:

git lsfwd

This command lists all files in the current directory context that are tracked or untracked, excluding those ignored by .gitignore.


Example Directory Structure

Consider the following project structure:

my_project/
├── .gitignore
├── src/
│   ├── main.py
│   └── utils.py
├── data/
│   ├── raw_data.csv
│   └── processed_data.csv
├── README.md
└── notes.txt

And a .gitignore file with the following content:

# Ignore data directory
data/

# Ignore notes
notes.txt

Sample Usage

Listing All Relevant Files

Run:

git lsf

Output:

src/main.py
src/utils.py
README.md

Listing Files in the Current Working Directory

Navigate to a subdirectory (e.g., src/) and run:

git lsfwd

Output:

main.py
utils.py

Benefits of lsf and lsfwd

  • Efficiency: Quickly gather a filtered view of your project files.
  • Focus: Supply relevant directory-specific context to AI tools.
  • Convenience: Automate context gathering with minimal setup.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment