How to tell git to ignore individual lines, i.e. gitignore for specific lines of code

GitGitignoreIgnore

Git Problem Overview


.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.

Git Solutions


Solution 1 - Git

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)
  1. Add a line defining the files to be filtered:
  • *.rb filter=gitignore, i.e. run filter named gitignore on all *.rb files
  1. 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.

Solution 2 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKacheView Question on Stackoverflow
Solution 1 - GitKacheView Answer on Stackoverflow
Solution 2 - GitMikeView Answer on Stackoverflow