GIT 预提交挂钩更改文件但不添加到提交

cor*_*rey 3 git bash github pre-commit pre-commit-hook

我正在使用 GITpre-commit挂钩来查找 README.md 文件 ex 中的字符串模式。{{STRING_PATTERN}},然后将字符串模式更改为所需的文本/代码输出,其中还包含当前分支名称。

\n\n

这就是问题所在。该pre-commit脚本的工作原理是它找到字符串模式并将其替换为正确的文本/代码,但它似乎在提交更改之前没有这样做......这意味着在我运行git commit -m "Updated README.md"并检查git status自述文件之后.md 文件显示为正在修改,如果我要运行git push origin branch_nameREADME.md 文件,则包含实际的{{STRING_PATTERN}}标识符(不需要),而不是更新的文本(这正是我想要的)。

\n\n

正如我所说,这是pre-commit代码,请注意,从定位字符串{{STRING_PATTERN}}标识符并使​​用动态创建的文本/代码 \xe2\x80\x93 更新它的角度来看,这确实有效。并没有真正提交这些更改,这就是问题所在。

\n\n
#!/usr/bin/env bash\n\n# Echo out text to ensure pre-commit is running\necho "pre-commit working"\n\n# Get the current branch name\nfunction git_branch {\n    git rev-parse --abbrev-ref HEAD\n}\n\n# Set branch variable to current branch\nbranch=$(git_branch)\n\n# Define the string/pattern marker to search for\n# This pattern/string serves as a marker that\n# gets replaced with the dynamically created text/code\nid="{{STRING_PATTERN}}"\n\n# Dynamically create text/code\n# Inject the $branch variable into the correct location of the text\nupdated_text="Example text containing that is being added to the $branch branch"\n\n# Find the string/pattern and replace it with the text/code\n# Then replace it with the build status image\nsed -i \'\' -e "s%{{STRING_PATTERN}}%$updated_text%g" README.md\n
Run Code Online (Sandbox Code Playgroud)\n

Krz*_*soń 6

您还应该git add在预提交挂钩中将更改添加到存储库中。