我在手机的原生浏览器上测试时使用git同步到phonegap.因此我有以下几行:
var isPhoneGap = false;
Run Code Online (Sandbox Code Playgroud)
显然我在构建时更改了这个,但是有什么方法可以设置git来忽略这一行,或者我必须把它放在自己的文件中并忽略它吗?
我在OSX 10.6上使用Gitx和终端.
Von*_*onC 90
如果您的文件是特定类型,您可以声明一个内容过滤器驱动程序,您可以在.gitattributes文件中声明(如" Git属性 " 的"关键字扩展"中所示):

*.yourType filter=yourFilterName
Run Code Online (Sandbox Code Playgroud)
(如果需要,您甚至可以为特定文件设置该过滤器)
实行:
yourFilterName.smudge(触发git checkout)和
git config --global filter.yourFilterName.smudge 'sed "s/isPhoneGap = .*/isPhoneGap = true/"'
Run Code Online (Sandbox Code Playgroud)yourFilterName.clean(触发git add)
git config --global filter.yourFilterName.clean 'sed "s/isPhoneGap = .*/isPhoneGap = false/"'
Run Code Online (Sandbox Code Playgroud)您的文件将保持不变git status,但其签出的版本将具有正确的值isPhoneGap.
小智 35
您可以使用
git update-index --assume-unchanged [file]
Run Code Online (Sandbox Code Playgroud)
忽略您不想跟踪的一个文件的更改.当我需要在存储库中有一个文件时我使用这个解决方案,但是这个文件有一些部分改变了我总是不需要跟踪.
当文件包含重要的更改时,您必须执行以下操作:
git update-index --no-assume-unchanged [file]
Run Code Online (Sandbox Code Playgroud)
此外,看到的Git文档更新索引的--[no-]assume-unchanged参数.
指定这些标志时,不会更新为路径记录的对象名称.相反,这些选项设置和取消设置路径的"假定未更改"位.当"假定未更改"位打开时,git停止检查工作树文件是否有可能的修改,因此您需要手动取消设置该位以在更改工作树文件时告诉git.
Gitx应该让你提交或忽略单独的行(你可能已经知道了),但是你每次提交时都必须这样做.我认为最好为每个部署目标配置一个配置文件(您可以对其进行版本设置),然后为启动服务器(如./myserver --config=whatever.js)启动一些运行时参数.
继续/sf/answers/1440214051/,@Mike建议创建一个pre-commit钩子,它将grep在暂存文件中用于人们可能想要忽略的行。挂钩检查这些行是否已上演。如果是这样,它echo是一个警告并且它exit带有代码,1因此提交过程将不会继续。
受到@Mike's answer 的启发,我发现自己可能使用了他的钩子的改进版本,它会自动 reset(带有-p标志)我们想要忽略的特定行。
我不确定此钩子是否适用于您有许多文件要忽略此行的情况,但此pre-commit钩子会在特定文件中查找此行中的更改buildVars.java。当我在我的机器上测试它时,钩子脚本看起来像这样。
#!/bin/sh
# this hook looks for lines with the text `var isPhoneGap = false;` in the file `buildVars.java` and it resets these lines to the previous state before staged with `reset -p`
if [[ $(git diff --no-ext-diff --cached buildVars.java | grep --count -e "var\ isPhoneGap[\ ]*=[\ ]*") -ne 0 ]]; then
cat <<EOW
WARNING: You are attempting to commit changes which are not supposed to be commited according to this \`pre-commit\` hook
This \`pre-commit\` hook will reset all the files containing this line to it's previous state in the last commit.
EOW
echo /$'\n'isPhoneGap$'\n'y$'\n'q | git reset -p
# BONUS: Check if after reseting, there is no actual changes to be commited and if so, exit 1 so the commit process will abort.
if [[ $(git diff --no-ext-diff --cached | wc -l) -eq 0 ]]; then
echo there are no actual changes to be commited and besides the change to the variable \'isPhoneGap\' so I won\'t commit.
exit 1
fi
fi
Run Code Online (Sandbox Code Playgroud)
我所做的是回显一个控制序列,该序列isPhoneGap在交互reset过程中搜索正则表达式。从而模拟用户按下/搜索isPhoneGap,y在询问他是否要放弃此补丁时按下,最后按下q退出交互reset。
交互式反向补丁过程记录在此处:https : //git-scm.com/docs/git-add#git-add-patch
注意:上面的脚本假设变量interactive.singleKey是false. 如果您将您的配置为true,请在警告后立即$'\n'从echo命令中删除任何内容。
| 归档时间: |
|
| 查看次数: |
27366 次 |
| 最近记录: |