gitignore loads other gitignores

痞子三分冷 提交于 2019-12-04 02:35:54
VonC

I am trying to omit conflicts in gitignore file

Then don't version the final .gitignore file itself: generate it on demand.

Keep your .gitignore files separates, but add a content filter driver (a smudge script), which will:

  • detect the content of each .gitignore.xxx files
  • concatenate their content to the actual .gitignore (which is not added to git, and remains a private file)

As Git doesn't support this out of the box, you may implement one yourself with Git hooks, for example:

In any case, you will need to manage your ignores and includes in a custom file, not a .gitignore itself, as this one will be generated. So, let's say, you have a .gitignoreincludes file in a branch, and custom include files in other branches.

So, you may install a "post-merge" hook like this, for example:

#!/bin/bash
gitignoreWithIncludes=".gitignoreincludes"    
if [[ -f $gitignoreWithIncludes ]]; then
  echo ".gitignore" > .gitignore
  while read -r line
  do
      if [[ $line == include* ]]; then
        includeName=${line:8}
        while read -r includeLine
        do
          echo $includeLine >> .gitignore
        done < $includeName
      else
        echo $line >> .gitignore
      fi
  done < $gitignoreWithIncludes 
fi

This hook will launch on any successful merge, will look for a .gitignoreincludes file, and, if it exists, will generate a .gitignore file. .gitignore will contain all entries from .gitignoreincludes and all entries from all files referenced as include FILE_NAME. Having .gitignore ignoring itself allows it to be safely regenerated multiple times when doing merges.

This can be further improved by allowing include instructions in included files as well, or also a post-checkout hook can be implemented to regenerate the .gitignore when switching between branches, etc., etc..

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!