git-text can help you to avoid from committing non-text files to git repo.
git-text 能够帮忙避免提交非文本文件到 git 仓库。
- get a new command "git text"
获取新命令“git text”
git config --global alias.text '!f() { set -ex ; hookfile=$(git rev-parse --show-toplevel)/.git/hooks/pre-commit ; curl -sSL https://raw.githubusercontent.com/wolfogre/git-text/master/pre-commit -o $hookfile ; chmod +x $hookfile ; }; f'
- install hook for a git repo
为某个 git 仓库安装钩子
cd A_GIT_REPO_DIR
git text
When you run git text
, it will download a git hook to .git/hooks/pre-commit
, so that every time you commit files, the hook will use file command to determine file's mime type, and refuse committing if there are some non-text files.
当你运行
git text
,它会下载一个 git 钩子文件到.git/hooks/pre-commit
,这样每次你提交文件的时候,钩子会使用 file 命令来探测文件的 mime 类型,如果提交的文件中有非文本文件,提交会被终止。
- get git-text
获取 git-text
$ git config --global alias.text '!f() { set -ex ; hookfile=$(git rev-parse --show-toplevel)/.git/hooks/pre-commit ; curl -sSL https://raw.githubusercontent.com/wolfogre/git-text/master/pre-commit -o $hookfile ; chmod +x $hookfile ; }; f'
- create a new repo to test
创建一个新的 git 仓库用了测试
$ mkdir test-repo
$ cd test-repo/
$ git init
Initialized empty Git repository in /root/test-repo/.git/
- install git hook for the repo
为这个仓库安装 git 钩子
$ git text
++ git rev-parse --show-toplevel
+ hookfile=/root/test-repo/.git/hooks/pre-commit
+ curl -sSL https://raw.githubusercontent.com/wolfogre/git-text/master/pre-commit -o /root/test-repo/.git/hooks/pre-commit
+ chmod +x /root/test-repo/.git/hooks/pre-commit
- test committing text files
尝试提交文本文件
$ touch test-empty-file
$ echo ok > test-text-file
$ git add --all
$ git commit -m "test commit"
ALL FILES ARE TEXT:
test-empty-file: inode/x-empty
test-text-file: text/plain
[master (root-commit) f17008d] test commit
2 files changed, 1 insertion(+)
create mode 100644 test-empty-file
create mode 100644 test-text-file
- test committing non-text files
尝试提交非文本文件
$ gzip test-text-file
$ git add --all
$ git commit -m "test commit"
DELETE NON-TEXT FILES OR USE 'git commit -n':
test-text-file.gz: application/x-gzip
- if you really want to commit it
如果你真的需要提交非文本文件
$ git commit -n -m "force commit non-text"
[master 7c01515] force commit non-text
2 files changed, 64 deletions(-)
delete mode 100644 test-text-file
create mode 100644 test-text-file.gz