如何告诉 git 忽略单个行,即特定代码行的 gitignore

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16244969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:02:53  来源:igfitidea点击:

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

gitgitignoreignore

提问by Kache

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

.gitignore可以忽略整个文件,但是有没有办法在编码时忽略特定的代码行?

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 忽略它们。

采纳答案by Kache

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

这就是你可以用git 过滤器来做的方式:

  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 gitignoreon all *.rbfiles
  3. Define the gitignorefilter 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
  1. 创建/打开 gitattributes 文件:
    • <project root>/.gitattributes(将被提交到回购)
    • <project root>/.git/info/attributes(不会被提交到回购)
  2. 添加一行定义要过滤的文件:
    • *.rb filter=gitignore, 即运行过滤器命名gitignore的所有*.rb文件
  3. gitignore在您的 中定义过滤器gitconfig
    • $ git config --global filter.gitignore.clean "sed '/#gitignore$/d'", 即删除这些行
    • $ git config --global filter.gitignore.smudge cat, 即从 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.

注意:
当然,这是针对 ruby​​ 文件的,当一行以 结尾时#gitignore应用,在~/.gitconfig. 根据您的目的修改它。

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

git stash save "proj1-debug"在过滤器处于非活动状态时(只是暂时禁用它gitconfig或其他东西)。这样,我的调试代码可以随时添加git stash apply到我的代码中,而不必担心这些行会被意外提交。

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.

感谢 Rudi 和 jw013 提到 git 过滤器和 gitattributes。

回答by Mike

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:

我在编写 java 代码时遇到了类似的问题。我的解决方案是标记我不想提交的代码,然后添加一个预提交挂钩来查找我的标记:

#!/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