Skip to content

Instantly share code, notes, and snippets.

@TomerFi
Last active October 29, 2025 03:23
Show Gist options
  • Select an option

  • Save TomerFi/0911f573ea0474b9ab74bcfcef0f2a49 to your computer and use it in GitHub Desktop.

Select an option

Save TomerFi/0911f573ea0474b9ab74bcfcef0f2a49 to your computer and use it in GitHub Desktop.
Git smudge-clean script for using as filter for mutiple reducted values for various file types in various repositories.
#!/bin/bash
#####################################################################################################
# This script is meant to be used as a global smudge-clean filter for removing sensitive data #
# from your commits. #
# #
# 1. Place this script in an acceisble path, i.e. ~/scripts/git-smudge-clean-filter.sh. #
# #
# 2. Populate the 'mapArr' using what you need hidden as the key, and the replacment as the value. #
# DO NOT use same values for multiple keys (this will work only in one direction). #
# #
# 3. Set up the filter with git (2 options): #
# 3.1. You can either add the following section in your global ~/.gitconfig file: #
# [filter "reductScript"] #
# smudge = ~/scripts/git-smudge-clean-filter.sh smudge #
# clean = ~/scripts/git-smudge-clean-filter.sh clean #
# 3.2. Or run the following command from you cli: #
# git config --global filter.reductScript.smudge "~/scripts/git-smudge-clean-filter.sh smudge" #
# git config --global filter.reductScript.clean "~/scripts/git-smudge-clean-filter.sh clean" #
# #
# 4. For every file type, in every repository you are working on, and need sensitive data removed, #
# add the 'filter=reductScript' property in the attributes file and you're good to go. #
# For example for filtering yaml files: `*.yaml text eol=lf filter=reductScript`. #
# #
# Tip: for shared repositories, you can store your attributes in in '$GIT_DIR/info/attributes' #
# instead of the standard '.gitattributes' file. #
# Follow this https://git-scm.com/docs/gitattributes for more attibtues inforamtion. #
#####################################################################################################
declare -A mapArr
mapArr["my-work-private-server.mywork.com"]="<reducted-work-server>"
mapArr["my-personal-private-server.myowndomain.org"]="<reducted-personal-server>"
mapArr["A*&#QAADDA(77##F"]="super-secret-token"
mapArr["[email protected]"]="[email protected]"
# mac users: use gsed instead of sed
sedcmd="sed"
if [[ "$1" == "clean" ]]; then
for key in ${!mapArr[@]}; do
sedcmd+=" -e \"s/${key}/${mapArr[${key}]}/g\""
done
elif [[ "$1" == "smudge" ]]; then
for key in ${!mapArr[@]}; do
sedcmd+=" -e \"s/${mapArr[${key}]}/${key}/g\""
done
else
echo "use smudge/clean as the first argument"
exit 1
fi
eval $sedcmd
@TwoWheelCoder
Copy link

TwoWheelCoder commented Jul 30, 2025

@TomerFi Does this work in windows VSCode? I've done all the setup as required and yet when I add text that should be edited, it still appears in the staged code changes.

@TomerFi
Copy link
Author

TomerFi commented Aug 1, 2025

Your choice of IDE shouldn't affect the outcome. If you've followed the instructions and it's still not working, run the script from the command line and check for any errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment