git在预提交钩子中的隐藏在第一次提交时失败
我正在开发一个预提交钩子,它使用YUI压缩器来缩小任何已经提交的CSS和JavaScript文件。文件缩小后,缩小的版本会自动进行提交。我已经读到,自动将机器生成的文件添加到提交中通常不是一个好主意,但我认为在这种情况下也可以。这就是它看起来的样子:git在预提交钩子中的隐藏在第一次提交时失败,git,githooks,pre-commit-hook,git-stash,Git,Githooks,Pre Commit Hook,Git Stash,我正在开发一个预提交钩子,它使用YUI压缩器来缩小任何已经提交的CSS和JavaScript文件。文件缩小后,缩小的版本会自动进行提交。我已经读到,自动将机器生成的文件添加到提交中通常不是一个好主意,但我认为在这种情况下也可以。这就是它看起来的样子: git状态的输出: # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified
git状态的输出
:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: _site-wide.css
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: _subpage.css
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: _site-wide.css
# new file: _subpage.css
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: _site-wide.css
# new file: _subpage.css
# new file: site-wide.css
# new file: subpage.css
#
这里发生的是使用YUI压缩器缩小站点范围的.css的预提交钩子,将结果输出到站点范围的.css(无前导下划线)。然后,它在整个站点范围内执行css
提交。pre-commit钩子跳过了\u subpage.css
,因为尽管它已被修改,但它并没有被提交
由于磁盘上的CSS和JavaScript文件可能与提交时暂存的CSS和JavaScript文件不同,因此我在缩小文件之前运行git stash-q--keep index
,然后在缩小文件之后运行git stash pop-q
。这个预提交钩子在已经有提交的存储库上运行良好,但是如果我在第一次提交之前将预提交钩子放置到位,我会得到以下结果:
git状态的输出
:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: _site-wide.css
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: _subpage.css
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: _site-wide.css
# new file: _subpage.css
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: _site-wide.css
# new file: _subpage.css
# new file: site-wide.css
# new file: subpage.css
#
git状态的输出
:
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: _site-wide.css
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: _subpage.css
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: _site-wide.css
# new file: _subpage.css
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: _site-wide.css
# new file: _subpage.css
# new file: site-wide.css
# new file: subpage.css
#
我怎样才能做到这一点?任何帮助都将不胜感激。我的结论是,没有解决方案,因为一些Git命令在没有HEAD的情况下根本无法工作。另外,在仔细考虑之后,我决定这是一种边缘情况,至少对我来说是这样,因为我的第一次提交通常是一些基本的东西,比如我使用的框架的文件。所以我可以在第一次提交之后实现钩子 我很感谢哈塞克的时间,也很感谢其他读到这篇文章的人的时间。谢谢大家! 有了Git 2.22(2019年第二季度,7年后),就有了一个 见(2019年2月25日)作者
stash
:使push-q保持安静
此提交的行为发生了变化。
当没有初始提交时,仍然会显示stash的shell版本
留言。
如果指定了--quiet
或-q
,此提交将使推送
不显示任何消息
因此,您不应该看到“您还没有初始提交”
”和一个git stash-q
,从git 2.22的新的rewrited-in-Cstash
命令开始。您能发布钩子代码吗?它似乎试图检查尚不存在的标题
修订,这就是为什么会出现错误。您可以尝试添加一个try/catch。如果您仍然对此感兴趣,您正在寻找“干净的过滤器”,它可以在将工作树内容放入repo的过程中清理工作树内容。