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"):(you can even set that filter for a specific file, if you want)
Implement:
yourFilterName.smudge
(triggered ongit checkout
) andyourFilterName.clean
(triggered ongit add
)Your file would appear unchanged on
git status
, yet its checked out version would have the right value forisPhoneGap
.This is how you can kind of do it with git filters:
OR
*.rb filter=gitignore
, i.e. run filter namedgitignore
on all*.rb
filesgitignore
filter in yourgitconfig
:$ git config --global filter.gitignore.clean "sed '/#gitignore$/'d"
, i.e. delete these lines$ git config --global filter.gitignore.smudge cat
, i.e. do nothing when pulling file from repoNotes:
Of course, this is for ruby files, applied when a line ends with
#gitignore
, applied globally in~/.gitconfig
. Modify this however you need for your purposes.Warning!!
This leaves your working file different from the repo (of course). Any checking out or rebasing will mean these lines will be lost! This trick may seem useless since these lines are repeatedly lost on check out, rebase, or pull, but I've a specific use case in order to make use of it.
Just
git stash save "proj1-debug"
while the filter is inactive (just temporarily disable it ingitconfig
or something). This way, my debug code can always begit stash apply
'd to my code at any time without fear of these lines ever being accidentally committed.I have a possible idea for dealing with these problems, but I'll try implementing it some other time.
Thanks to Rudi and jw013 for mentioning git filters and gitattributes.
A content filter driver is not a good solution. You may be able to hide this line from
git status
/etc but it's not really being ignored. As soon as you change the value, your working directory will be marked dirty even though the change may not be visible.If you really want this line out of version control, changing it to a command line argument or placing it in an ignored include or build file may be the only practical means.
You can use
to ignore the changes of one file that you don't want track. I use this solution when I need to have a file in the repository but this file have some parts that change that I don't need track always.
When the file have changes that are important you have to do this:
Also, see the Git doc update-index for
--[no-]assume-unchanged
parameter.Continuing https://stackoverflow.com/a/20574486/4935114, @Mike proposed to create a
pre-commit
hook which willgrep
in the staged files for the lines which one might want to ignore. The hook checks if those lines were staged. If so, itecho
s a warning and itexit
's with code1
so the commit process won't continue.Inspired by @Mike's answer, I found myself using perhaps an improved version of his hook which automatically
reset
s (with the-p
flag) the specific line which we want to ignore.I'm not sure this hook will work for a situation where you have many files with this line to be ignored, but this
pre-commit
hook looks for a changed in this line in a specific filebuildVars.java
. The hook script looked like this when I tested it on my machine.Explanation
What I did was echoing a control sequences which searches for the regex
isPhoneGap
during an interactivereset
process. Thus emulating a user who presses/
to search forisPhoneGap
, pressesy
when asked if he wants to discard this patch and finally pressesq
to exit the interactivereset
.The interactive reversed patch process is documented here: https://git-scm.com/docs/git-add#git-add-patch
NOTE: The above script assuming that the variable
interactive.singleKey
isfalse
. If you configured yours totrue
, remove any$'\n'
from theecho
command right after the warning.I'm guessing this might be something that comes up in more than one line of your source.
I think it would be cleanest to have a .userbuildconfig file of some sort that you just include and have the defaults checked in. Then you can use Carlos's suggestion to mark that file alone as assume unchanged. This way other changes to the file where you need to check the setting are not missed.
This can allow you to tune preprocessor macros locally (Or for java something like this https://stackoverflow.com/a/1813873/1270965).