本文介绍了如何让git忽略个别行,即特定代码行的gitignore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

这就是你如何能够使用进行:


  1. 创建/打开gitattributes文件:

    • < project root> /。gitattributes(将提交到repo)

    • < project root> /。git / info / attributes(不会被提交回购)
    >
  2. 添加一行定义要过滤的文件:

    • *。rb filter = gitignore
    • code>,即在所有 *。rb 文件上运行名为 gitignore 的过滤器

    • gitconfig 中定义 gitignore 过滤器:

      • $ git config --global filter.gitignore.cleansed'/#gitignore $ /'d,即删除这些行

      • $ git config --global fil




注意:

当然,这是针对ruby文件的,当一行以 #gitignore 结尾时应用,全局应用〜/的.gitconfig 。修改这个,不过你需要为你的目的。



警告!!

这会让你的工作文件与回购(当然)不同。任何退房或重新绑定将意味着这些线路将会丢失!这个技巧可能看起来没有用处,因为这些行在检出,重定位或拉动时会反复丢失,但为了使用它,我有一个特定用例。



只需 git stash保存proj1-debug 而过滤器处于非活动状态(只需暂时在 gitconfig code>或其他)。通过这种方式,我的调试代码可以随时在我的代码中随时应用 git stash apply 'd,而不用担心这些行会被意外提交。



我有一个处理这些问题的可能想法,但我会尝试在其他时间实施它。



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


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

解决方案

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.

这篇关于如何让git忽略个别行,即特定代码行的gitignore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 03:20