即使无数次回到这个问题,我也总是被卡在某个地方。我已经提出了逐步执行此操作的详细过程:
首先只使用git add添加实际内容。
它将显示添加到索引的相关文件,而其他所有文件仍未跟踪。这有助于.gitignore逐步构建。
$ git add wp-content/themes/my-theme/*
$ git status
    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css
    Untracked files:
        wp-admin/
        wp-content/plugins/
        wp-content/themes/twentyeleven/
        wp-content/themes/twentytwelve/
        ...
        wp-includes/
        ...
DUMMY.TXT在目录中添加一个临时文件:
$ git status
    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css
    Untracked files:
        wp-admin/
        wp-content/plugins/
        wp-content/themes/twentyeleven/
        wp-content/themes/twentytwelve/
        ...
        wp-content/themes/my-theme/DUMMY.TXT  <<<
        ...
        wp-includes/
        ...
现在,我们的目标是构建规则,DUMMY.TXT以使它成为我们完成后仍然显示为“未跟踪”的唯一规则。
开始添加规则: 
.gitignore
/*
第一个只是忽略一切。未跟踪的文件应全部消失,仅应显示索引文件:
$ git status
    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css
在路径中添加第一个目录 wp-content
/*
!/wp-content
现在,未跟踪的文件将再次显示,但仅包含wp-content的内容
$ git status
    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css
    Untracked files:
        wp-content/plugins/
        wp-content/themes/twentyeleven/
        wp-content/themes/twentytwelve/
        ..
忽略第一个目录中的所有内容/wp-content/*并忽略!/wp-content/themes
/*
!/wp-content
/wp-content/*
!/wp-content/themes
现在,未跟踪的文件将进一步缩小到仅 wp-content/themes
$ git status
    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css
    Untracked files:
        wp-content/themes/twentyeleven/
        wp-content/themes/twentytwelve/
        ..
重复此过程,直到该哑文件是唯一仍显示为“未跟踪”的文件:
/*
!/wp-content
/wp-content/*
!/wp-content/themes
/wp-content/themes/*
!/wp-content/themes/my-theme
 
$ git status
    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css
    Untracked files:
        wp-content/themes/my-theme/DUMMY.TXT
               
              
**如果您的模式中没有斜杠,则通配符仅起作用,请参见sparethought.wordpress.com/2011/07/19/…– 2013