Last active
December 14, 2024 10:59
-
-
Save aryelgois/3674648cbb34f5d0ee1042017494a173 to your computer and use it in GitHub Desktop.
Git filter to ignore lines in your files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Git filter to ignore lines in your files. | |
# | |
# Copyright (c) 2017-2019,2023 Aryel Mota Góis <[email protected]> | |
# | |
# MIT License | |
# | |
# | |
# SETUP: | |
# | |
# install -m 755 git-ignore-line.sh ~/bin/git-ignore-line | |
# | |
# | |
# USAGE: | |
# | |
# Mark single lines you want to ignore with 'GITIGNORE'. It ignores | |
# the whole line. It is recommended to be inside a comment in your code. | |
# | |
# Mark multiple lines surrounding them with 'GITIGNORE START' and | |
# 'GITIGNORE END'. It can not be nested. | |
# | |
# NOTE: Ignored lines might be lost on checkout. | |
# | |
# | |
# Add files to ignore: | |
# | |
# git ignore-line <pattern> | |
# | |
# Remove patterns with: | |
# | |
# git ignore-line -r <pattern> | |
# | |
# List configured patterns: | |
# | |
# git ignore-line -l | |
# | |
# PATTERN can be a file or a glob pattern: '*.html'. Remember to | |
# escape the `*`. | |
# | |
# | |
# TODO: | |
# | |
# - Stash the lines ignored by this filter before a git checkout. | |
set -eu | |
# Check if stdin is not tty and remove ignored lines from it. | |
[ -t 0 ] || exec sed \ | |
'/GITIGNORE START/,/GITIGNORE END/d; /GITIGNORE/d' \ | |
/dev/stdin | |
# Running from tty. | |
program=$(basename "$0") | |
# Find git repository. | |
git_dir=$(git rev-parse --git-dir) | |
# Path to attributes file. | |
attr_file=$git_dir/info/attributes | |
# Check arguments. | |
if [ $# -eq 2 ] && [ "$1" = '-r' ]; then | |
# Remove filter for pattern. | |
sed "s|^$2 filter=$program||" "$attr_file" > "$attr_file.tmp" | |
mv -- "$attr_file.tmp" "$attr_file" | |
elif [ $# -eq 1 ]; then | |
if [ "$1" = '-l' ]; then | |
# List patterns. | |
grep "filter=$program" "$attr_file" || true | |
else | |
# Add filter for pattern. | |
echo "$1 filter=$program" >> "$attr_file" | |
# Configure filter. | |
git config --global "filter.$program.clean" "$program" | |
git config --global "filter.$program.smudge" cat | |
fi | |
else | |
# Show help. | |
>&2 echo "Usage: $program [-r] <pattern>" | |
>&2 echo " or: $program -l" | |
exit 1 | |
fi |
Hello @SureshKumar311
First you need to add the file to your PATH
and it needs to be executable. I updated the Setup step to use a command that does that..
Now, for an example:
mkdir example
cd example
git init
# Example 1 : enable the filter in specific file
git-ignore-line my_file.md
# Example 2 : enable the filter in JavaScript files
# NOTE: if the script file starts with 'git-' the Git command detects it as a sub command
git ignore-line '*.js'
# Example 3 : enable the filter in all files
# NOTE: it can be very slow in big repos
git-ignore-line '*'
# Example 4 : enable the filter in all files of a directory
# NOTE: useful to skip for example node_modules
git-ignore-line 'src/*'
# Add test file:
echo Hello > my_file.md
git add my_file.md
# See it working:
cat >> my_file.md <<EOF
hidden <!-- GITIGNORE -->
visible
you cannot see: GITIGNORE START
HIDDEN
that's it.. GITIGNORE END now you will see:
ok bye :)
EOF
git diff
The filter script settings are stored in the global Git config and it's written automatically when adding a filter pattern
and the filter patterns are stored in the repository settings at .git/info/attributes
@aryelgois Thanks❤️
no problem 😃
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi Aryelgois,
@aryelgois can give an example to ignore an line on file using above script
Because i have try this script but it's not working for me
thanks