{"id":13941283,"url":"https://github.com/wolfogre/git-text","last_synced_at":"2025-04-11T01:52:18.624Z","repository":{"id":55933675,"uuid":"168483797","full_name":"wolfogre/git-text","owner":"wolfogre","description":" git-text can help you to avoid from committing non-text files to git repo","archived":false,"fork":false,"pushed_at":"2020-12-06T06:39:52.000Z","size":21,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T01:52:14.233Z","etag":null,"topics":["git-hook"],"latest_commit_sha":null,"homepage":"https://blog.wolfogre.com/posts/git-text/","language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wolfogre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-01-31T07:40:49.000Z","updated_at":"2024-02-20T02:08:00.000Z","dependencies_parsed_at":"2022-08-15T09:50:19.763Z","dependency_job_id":null,"html_url":"https://github.com/wolfogre/git-text","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolfogre%2Fgit-text","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolfogre%2Fgit-text/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolfogre%2Fgit-text/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wolfogre%2Fgit-text/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wolfogre","download_url":"https://codeload.github.com/wolfogre/git-text/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248328108,"owners_count":21085258,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git-hook"],"created_at":"2024-08-08T02:01:15.578Z","updated_at":"2025-04-11T01:52:18.599Z","avatar_url":"https://github.com/wolfogre.png","language":"Shell","readme":"# git-text\n\n**git-text** can help you to avoid from committing non-text files to git repo.\n\n\u003e **git-text** 能够帮忙避免提交非文本文件到 git 仓库。\n\n## How to use it\n\n\u003e ## 如何使用\n\n1. get a new command \"git text\"\n\n\u003e 获取新命令“git text”\n\n```bash\ngit 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'\n```\n\n2. install hook for a git repo\n\n\u003e 为某个 git 仓库安装钩子\n\n```bash\ncd A_GIT_REPO_DIR\ngit text\n```\n\n## How it works\n\n\u003e ## 工作原理\n\nWhen 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](http://man7.org/linux/man-pages/man1/file.1.html) command to determine file's [mime type](https://www.iana.org/assignments/media-types/media-types.xhtml), and refuse committing if there are some non-text files.\n\n\u003e 当你运行 `git text`，它会下载一个 git 钩子文件到 `.git/hooks/pre-commit`，这样每次你提交文件的时候，钩子会使用 [file](http://man7.org/linux/man-pages/man1/file.1.html) 命令来探测文件的 [mime 类型](https://www.iana.org/assignments/media-types/media-types.xhtml)，如果提交的文件中有非文本文件，提交会被终止。\n\n## Try it!\n\n\u003e ## 试试吧！\n\n1. get git-text\n\n\u003e 获取 git-text\n\n```text\n$ 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'\n```\n\n2. create a new repo to test\n\n\u003e 创建一个新的 git 仓库用了测试\n\n```text\n$ mkdir test-repo\n$ cd test-repo/\n$ git init\nInitialized empty Git repository in /root/test-repo/.git/\n```\n\n3. install git hook for the repo\n\n\u003e 为这个仓库安装 git 钩子\n\n```text\n$ git text\n++ git rev-parse --show-toplevel\n+ hookfile=/root/test-repo/.git/hooks/pre-commit\n+ curl -sSL https://raw.githubusercontent.com/wolfogre/git-text/master/pre-commit -o /root/test-repo/.git/hooks/pre-commit\n+ chmod +x /root/test-repo/.git/hooks/pre-commit\n```\n\n4. test committing text files\n\n\u003e 尝试提交文本文件\n\n```text\n$ touch test-empty-file\n$ echo ok \u003e test-text-file\n$ git add --all\n$ git commit -m \"test commit\"\nALL FILES ARE TEXT:\ntest-empty-file: inode/x-empty\ntest-text-file:  text/plain\n[master (root-commit) f17008d] test commit\n 2 files changed, 1 insertion(+)\n create mode 100644 test-empty-file\n create mode 100644 test-text-file\n```\n\n5. test committing non-text files\n\n\u003e 尝试提交非文本文件\n\n```text\n$ gzip test-text-file\n$ git add --all\n$ git commit -m \"test commit\"\nDELETE NON-TEXT FILES OR USE 'git commit -n':\ntest-text-file.gz: application/x-gzip\n```\n\n6. if you really want to commit it\n\n\u003e 如果你真的需要提交非文本文件\n\n```text\n$ git commit -n -m \"force commit non-text\"\n[master 7c01515] force commit non-text\n 2 files changed, 64 deletions(-)\n delete mode 100644 test-text-file\n create mode 100644 test-text-file.gz\n```\n","funding_links":[],"categories":["Shell"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwolfogre%2Fgit-text","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwolfogre%2Fgit-text","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwolfogre%2Fgit-text/lists"}