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

时间:2016-06-15 14:47:04

标签: git bash github pre-commit-hook pre-commit

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

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

以下是pre-commit代码,正如我所说,请注意,这可以从找到字符串 {{STRING_PATTERN}} 标识符并使​​用动态更新它的角度来运行创建文本/代码 - 它实际上并没有提交这些更改,这就是问题。

#!/usr/bin/env bash

# Echo out text to ensure pre-commit is running
echo "pre-commit working"

# Get the current branch name
function git_branch {
    git rev-parse --abbrev-ref HEAD
}

# Set branch variable to current branch
branch=$(git_branch)

# Define the string/pattern marker to search for
# This pattern/string serves as a marker that
# gets replaced with the dynamically created text/code
id="{{STRING_PATTERN}}"

# Dynamically create text/code
# Inject the $branch variable into the correct location of the text
updated_text="Example text containing that is being added to the $branch branch"

# Find the string/pattern and replace it with the text/code
# Then replace it with the build status image
sed -i '' -e "s%{{STRING_PATTERN}}%$updated_text%g" README.md

1 个答案:

答案 0 :(得分:1)

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

相关问题