git 可以忽略特定行吗? [英] Can git ignore a specific line?

查看:34
本文介绍了git 可以忽略特定行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 git 同步到 phonegap,同时在手机的本机浏览器上进行测试.因此,我有以下几行:

var isPhoneGap = false;

显然我在构建时更改了这一点,但是有什么方法可以设置 git 来忽略这一行,还是我必须将它放在自己的文件中并以这种方式忽略它?

我在 OSX 10.6 上使用 Gitx 和终端.

解决方案

如果您的文件属于特定类型,您可以声明一个 )

实施:

  • yourFilterName.smudge(在 git checkout 上触发)和

     git config --global filter.yourFilterName.smudge 'sed "s/isPhoneGap = .*/isPhoneGap = true/''

  • yourFilterName.clean(由 git add 触发)

     git config --global filter.yourFilterName.clean 'sed "s/isPhoneGap = .*/isPhoneGap = false/"'

您的文件在 git status 上看起来没有变化,但它的检出版本将具有 isPhoneGap 的正确值.


注意:在评论中, ohaalRoalda> 建议在单独的 helper.sh 脚本中隔离 sed 调用:

sed "s/isPhoneGap = .*/isPhoneGap = true/";$@"

I'm using git to sync to phonegap while testing on the phone's native browser. As such I have the following line:

var isPhoneGap = false;

Obviously I change this when building, but is there any way I can set up git to ignore this one line or do I have to go and put it in its own file and ignore it that way?

I'm using Gitx and the terminal on OSX 10.6.

解决方案

If your file is of a specific type, you can declare a content filter driver, that you can declare in a .gitattributes file (as presented in the "Keyword expansion" of "Git Attributes"):

*.yourType filter=yourFilterName

(you can even set that filter for a specific file, if you want)

Implement:

  • yourFilterName.smudge (triggered on git checkout) and

      git config --global filter.yourFilterName.smudge 'sed "s/isPhoneGap = .*/isPhoneGap = true/"'
    

  • yourFilterName.clean (triggered on git add)

      git config --global filter.yourFilterName.clean 'sed "s/isPhoneGap = .*/isPhoneGap = false/"'
    

Your file would appear unchanged on git status, yet its checked out version would have the right value for isPhoneGap.


Note: in the comments, ohaal and Roald suggest to isolate the sed calls in a separate helper.sh script:

sed "s/isPhoneGap = .*/isPhoneGap = true/" "$@"

这篇关于git 可以忽略特定行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆