【问题标题】:git hook to batch unstage all files in a pathgit hook 批量取消暂存路径中的所有文件
【发布时间】:2021-07-04 21:41:50
【问题描述】:

我有一个devlog 文件夹,我在其中写下我希望通过git status 显示的想法/示例代码。排除这些文件后,我通常只能git add .; git commit,但显然devlog 中的内容禁止这样做。我喜欢有一个 git 钩子,它可以在提交之前取消整个路径及其内容。

我试过了:

git rm --cached devlog/*

但显然,它希望./devlog 的所有内容都已经上演(包括那些被.gitignore 忽略的内容),因此会引发错误:

git rm --cached devlog/*
#> fatal: pathspec 'devlog/check.log' did not match any files

其中devlog/check.log 被忽略并且根本没有上演。

【问题讨论】:

    标签: git hook pre-commit-hook unstage


    【解决方案1】:

    你可以:

    • 先强制添加check.log
    • 然后git rm --cached 一切

    由于check.log 被忽略(在.gitignore 中列出),它会在git rm --cached 之后被自动忽略(再次)。

    所以:

        git add --force devlog/check.log
        git rm --cached devlog/*
    

    【讨论】:

    • 谢谢。这个解决方案的主要问题是我每次更新 .gitignore 时都必须更新钩子
    • @AlJAbadi 为什么要多次更新挂钩?一次就足够了。
    • 我不确定我是否正确理解了您的提议。我假设,例如,如果我的 .gitignore 中有 **/*.pdf 并且有一天,我开始在 ./devlog 中有一个 pdf 文件(或者我更新我的 .gitignore 以忽略某些文件类型),我也必须@ 987654329@它在钩子里。如果我错了,请澄清。
    • @AlJAbadi 是修改你的钩子的源代码。但是您的解决方案更简单,并且得到了支持。
    【解决方案2】:

    这是一个似乎可行的解决方案。只需将以下内容添加到.git/hooks/pre-commit

    git reset -- devlog/*
    

    【讨论】:

    • 我相信你的意思是.git/hooks/pre-commit(除非你自定义了默认路径)
    • @Anthony 是的,我刚刚修好了。感谢您的来信。
    猜你喜欢
    • 2020-11-14
    • 2011-05-14
    • 2015-09-02
    • 2011-06-15
    • 2012-03-24
    • 2021-03-16
    • 2015-06-19
    • 2014-04-03
    • 1970-01-01
    相关资源
    最近更新 更多