267

.gitignore can ignore whole files, but is there a way to ignore specific lines of code while coding?

I frequently and repeatedly add the same debug lines in a project, only to have to remember to remove them before committing. I'd like to just keep the lines in the code and have git disregard them.

8
  • 159
    I can't tell whether this is a terrible idea or a brilliant one. Commented May 1, 2013 at 6:53
  • 3
    @KyleStrand at the very least, I can add it to debug lines I type and feel safe in knowing they won't be accidentally committed Commented May 1, 2013 at 21:29
  • 11
    Right, that's the "brilliant" part. Commented May 1, 2013 at 21:29
  • Maybe I'd feel more comfortable with it if it could only be applied to print lines, or something. And I suppose that if there's a unit testing infrastructure in place that runs after each commit, this would be pretty safe. Commented May 1, 2013 at 21:35
  • Oh, then you could just change the sed regex Commented May 1, 2013 at 23:12

2 Answers 2

208

This is how you can kind of do it with git filters:

  1. Create/Open gitattributes file:
    • <project root>/.gitattributes (will be committed into repo)
      OR
    • <project root>/.git/info/attributes (won't be committed into repo)
  2. Add a line defining the files to be filtered:
    • *.rb filter=gitignore, i.e. run filter named gitignore on all *.rb files
  3. Define the gitignore filter in your gitconfig:
    • $ 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 repo

Notes:
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 in gitconfig or something). This way, my debug code can always be git 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.

Sign up to request clarification or add additional context in comments.

13 Comments

Shouldn't that be: git config --global filter.gitignore.clean sed '/#gitignore$/d' since you've named the filter gitignore
I did this for '.py'-files and ut worked. Then I did it again for '.xml'-files. That didn't work, and it also broke the previously working filter for python-files. Now I get this error all the time "error: external filter sed". Do you know how I can fix this or simply delete all filters?
This is interesting. But it basically acts like the line in question isn't there. It won't work for something like simply making the changes to a line be ignored. So you can't, for example, change the value of a variable in a function call. Eg, instead of changing flags = foo | bar to flags = foo | bar | debug_thingy, you'd have to make sure you add a completely new line like flags |= debug_thingy after.
I found a solution that works for me. I'm using git config --global filter.gitignore.clean "sed 's/.*gitignore//'" and replacing code like this , var x = replaced #gitignore var x = 42. So the content after the #gitignore replaces the line, so the indentation must be kept after the #gitignore.
Here's also a sed command to ignore blocks of code "sed '/^# start git-block-ignore/,/^# end git-block-ignore/d;'"
|
80

I had a similar issue writing java code. My solution was to markup code that I didn't want to commit and then add a pre-commit hook that would look for my markup:

#!/bin/bash # # This hook will look for code comments marked '//no-commit' # - case-insensitive # - dash is optional # - there may be a space after the // # noCommitCount=$(git diff --no-ext-diff --cached | egrep -i --count "(@No|\/\/\s?no[ -]?)commit") if [ "$noCommitCount" -ne "0" ]; then echo "WARNING: You are attempting to commit changes which include a 'no-commit'." echo "Please check the following files:" git diff --no-ext-diff --cached --name-only -i -G"(@no|\/\/s?no-?)commit" | sed 's/^/ - /' echo echo "You can ignore this warning by running the commit command with '--no-verify'" exit 1 fi 

Put this into .git/hooks/pre-commit and give it a chmod +x .git/hooks/pre-commit

4 Comments

Markup definitely feels less scary solution and declarative, than sed based filtering or such. Nice one.
I would up vote this 100x if I could. This is a safe and straight forward sollution. Thank you!
Hmm. This certainly makes life safer, but not easier.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.