I always get stuck somewhere on this even after coming back to this question numerous times. I've come up with a detailed process of doing it step by step:
First just use git add
to add the actual content.
It'll show the relevant files added to the index while all others still untracked. This helps contructing .gitignore
step by step.
$ 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/
...
Add a temporary DUMMY.TXT
file in your directory:
$ 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/
...
Our goal now is to construct the rules such that this DUMMY.TXT
be the only one still showing up as Untracked when we're done.
Start adding the rules:
.gitignore
/*
First one is just to ignore everything. Untracked files should be all gone, only indexed files should be showing:
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Add the first dir in the path wp-content
/*
!/wp-content
Now the Untracked files will show up again, but only have wp-content
's contents
$ 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/
..
Ignore everything in the first dir /wp-content/*
and un-ignore !/wp-content/themes
/*
!/wp-content
/wp-content/*
!/wp-content/themes
Now the Untracked files will further narrow down to only 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/
..
Repeat the process till that dummy file is the only one still showing as Untracked:
/*
!/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
**
wildcard only workds if you have no slashes in your pattern, see sparethought.wordpress.com/2011/07/19/… – Rivarivage