git实现提交前自动替换某些字符串

新建hook文件

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# cat .git/hooks/pre-commit
##!/bin/sh
for FILE in `git diff --cached --name-only --diff-filter=ACM`;do
echo $FILE
sed -i '' -E 's/(key1|key2)/coder4/' $FILE
git add $FILE
done
# cat .git/hooks/pre-commit ##!/bin/sh for FILE in `git diff --cached --name-only --diff-filter=ACM`;do echo $FILE sed -i '' -E 's/(key1|key2)/coder4/' $FILE git add $FILE done
# cat .git/hooks/pre-commit

##!/bin/sh
for FILE in `git diff --cached --name-only --diff-filter=ACM`;do
    echo $FILE
    sed -i '' -E 's/(key1|key2)/coder4/' $FILE 
    git add $FILE
done

添加执行权限

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
chmod a+x .git/hooks/pre-commit
chmod a+x .git/hooks/pre-commit
chmod a+x .git/hooks/pre-commit

之后,在commit后,会自动替换字符串

 

Leave a Reply

Your email address will not be published. Required fields are marked *