git-check-ignore - Linux


Overview

git-check-ignore checks if a path is ignored by Git. It’s useful for shell scripts to determine if a file or directory should be included in a git add.

Syntax

git check-ignore [-v] [-z] <path>...

Options/Flags

  • -v: Verbose output. Print the reason why a path is ignored.
  • -z: NUL-terminate output. Paths are separated by NUL characters.

Examples

Check if the file foo.txt is ignored:

git check-ignore foo.txt

Check if any of the files in the src directory are ignored:

git check-ignore src/*

Common Issues

If git-check-ignore prints unmatch, it means the path is not ignored. If it prints ignored, the path is ignored.

Integration

git-check-ignore can be used in shell scripts to exclude ignored files from being added to Git. For example, the following script adds all files in the current directory except for those that are ignored:

#!/bin/sh

for file in *; do
  if ! git check-ignore "$file"; then
    git add "$file"
  fi
done

Related Commands

  • git-ls-files: Lists files in the working tree.
  • git-status: Shows the status of files in the working tree.