.gitignore exclude folder but include specific subfolder

后端 未结 17 1624
一个人的身影
一个人的身影 2020-11-21 06:36

I have the folder application/ which I add to the .gitignore. Inside the application/ folder is the folder application/language

相关标签:
17条回答
  • 2020-11-21 06:51

    I often use this workaround in CLI where instead of configuring my .gitignore, I create a separate .include file where I define the (sub)directories I want included in spite of directories directly or recursively ignored by .gitignore.

    Thus, I additionally use

    git add `cat .include`
    

    during staging, before committing.

    To the OP, I suggest using a .include which has these lines:

    <parent_folder_path>/application/language/gr/*
    

    NOTE: Using cat does not allow usage of aliases (within .include) for specifying $HOME (or any other specific directory). This is because the line homedir/app1/* when passed to git add using the above command appears as git add 'homedir/app1/*', and enclosing characters in single quotes ('') preserves the literal value of each character within the quotes, thus preventing aliases (such as homedir) from functioning (see Bash Single Quotes).

    Here is an example of a .include file I use in my repo here.

    /home/abhirup/token.txt
    /home/abhirup/.include
    /home/abhirup/.vim/*
    /home/abhirup/.viminfo
    /home/abhirup/.bashrc
    /home/abhirup/.vimrc
    /home/abhirup/.condarc
    
    0 讨论(0)
  • 2020-11-21 06:51

    my JetBrains IntelliJ IDEA .gitignore configuration, where I need exclude wholde .idea folder except .idea/runConfigurations:

    .idea
    !.idea/
    .idea/*
    !.idea/runConfigurations/
    

    see: https://github.com/daggerok/gitignore-idea-runConfigurations

    0 讨论(0)
  • 2020-11-21 06:54

    Add an additional answer:

    !/.vs/              <== include this folder to source control, folder only, nothing else
    /.vs/*              <== but ignore all files and sub-folder inside this folder
    !/.vs/ProjectSettings.json <== but include this file to source control
    !/.vs/config/       <== then include this folder to source control, folder only, nothing else
    !/.vs/config/*      <== then include all files inside the folder
    

    here is result:

    0 讨论(0)
  • 2020-11-21 06:55

    The simplest and probably best way is to try adding the files manually (generally this takes precedence over .gitignore-style rules):

    git add /path/to/module
    

    You may even want the -N intent to add flag, to suggest you will add them, but not immediately. I often do this for new files I’m not ready to stage yet.


    This a copy of an answer posted on what could easily be a duplicate QA. I am reposting it here for increased visibility—I find it easier not to have a mess of gitignore rules.

    0 讨论(0)
  • 2020-11-21 06:56

    In WordPress, this helped me:

    wp-admin/
    wp-includes/
    /wp-content/*
    !wp-content/plugins/
    /wp-content/plugins/*
    !/wp-content/plugins/plugin-name/
    !/wp-content/plugins/plugin-name/*.*
    !/wp-content/plugins/plugin-name/**
    
    0 讨论(0)
  • 2020-11-21 06:57

    gitignore - Specifies intentionally untracked files to ignore.

    Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar
    

    Another example for WordPress:

    !/wp-content
    wp-content/*
    !/wp-content/plugins
    wp-content/plugins/*
    !wp-content/plugins/my-awesome-plugin
    

    More informations in here: https://git-scm.com/docs/gitignore

    0 讨论(0)
提交回复
热议问题