如何让Git忽略个别线路,即gitignore的特定代码行[复制](How to tell git

2019-09-01 05:46发布

这个问题已经在这里有一个答案:

  • 可以忽略的git特定行? 8个回答

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

我经常和反复添加同一调试线一期工程,只需要记住在提交之前将其删除。 我想只要保持在代码行,让Git忽略他们。

Answer 1:

这就是你可以种用做git的过滤器 :

  1. 创建/打开gitattributes文件:
    • <项目根> /。gitattributes(将被提交到回购)
      要么
    • <项目根> /。GIT中/信息/属性(将不被提交到回购)
  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 ,即从回购拉动文件时束手无策

笔记:
当然,这是红宝石文件,当施加一个线结尾#gitignore ,在全局应用于~/.gitconfig 。 修改这个,但是你需要为你的目的。

警告!!
这会使你的工作文件不同于回购(当然)。 任何检查出或重订基期将意味着这些线路都将丢失! 这一招看似无用的,因为这些线路上反复检查失去了,重订,或拉,但我为了让使用过它特定的使用情况。

只是git stash save "proj1-debug" 而过滤器是无效的 (只是暂时禁用它gitconfig或东西)。 这样一来,我的调试代码总是可以git stash apply倒是我在任何时间代码,而不必担心这些线路曾经被意外犯下的。

我有处理这些问题的一个可能的想法,但我会尽力实现它的一些其他的时间。

由于鲁迪和jw013用于提git的过滤器和gitattributes。



Answer 2:

我有一个类似的问题编写Java代码。 我的解决办法是标记,我不想犯代码,然后添加一个pre-commit钩子,将寻找我的标记:

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


文章来源: How to tell git to ignore individual lines, i.e. gitignore for specific lines of code [duplicate]