Using .gitignore to ignore everything but specific

2019-01-05 09:34发布

My issue is that I have a bunch of WordPress websites in my git repo, of which I want to selectively commit only the content of my themes folders, while ignoring the rest of the redundant files found in WordPress.

I've used .gitignore files to ignore file types before, but can it be used the other way around- that is to ignore everything BUT a certain folder path?

root (git repo)
- / wordpress
- - / (WordPress Site 1)/wp-content/themes
- - / (WordPress Site 2)/wp-content/themes
- - / (WordPress Site 3)/wp-content/themes

Thanks-

UPDATE:

Based on the answers I did the following, but it's not working. Any ideas?

# Ignore everything:
*
# Except for wordpress themes:
!*/wp-content/themes/*

I've also tried the following variations:

!*/wp-content/themes*
!*wp-content/themes/*
!wp-content/themes/*
!/wordpress/*/wp-content/themes*
!wordpress/*/wp-content/themes*

None of these read my themes folders.

14条回答
ゆ 、 Hurt°
2楼-- · 2019-01-05 09:37

Here's how I did it:

# Ignore everything at root:
/*

# Except for directories:
!wordpress/(WordPress Site 1)/wp-content/themes/
!wordpress/(WordPress Site 1)/wp-content/themes/
!wordpress/(WordPress Site 1)/wp-content/themes/

# Except files:
!README

#Except files of type:
!*.txt

This is what worked for me. Allows you to ignore everything except specific files or folders

macOS sierra

查看更多
倾城 Initia
3楼-- · 2019-01-05 09:38

modify the first line

*

change it to

/*
查看更多
Explosion°爆炸
4楼-- · 2019-01-05 09:39

I needed to ignore everything but not one folder with subdirectories.

For me, this works

# Ignore everything
/*

# Not these directories
!folder/

# Not these files
!.gitignore
!.env
!file.txt
查看更多
倾城 Initia
5楼-- · 2019-01-05 09:42

For those looking for a cleaner solution, please try the following.

As mentioned in the comments of this answer, you have to use this method recursively.

In this example, you have a website setup at ./ where your .git folder and .gitignore file is located and a WordPress installation setup in ./wordpress. To correctly ignore the everything under the ./wordpress directory apart from the theme directory itself (wordpress/wp-content/themes/my-theme), you will have to recursively ignore and allow each directory up until the directory you wish to allow:

wordpress/*
wordpress/wp-content/*
wordpress/wp-content/themes/*
!wordpress/wp-content
!wordpress/wp-content/themes
!wordpress/wp-content/themes/my-theme

The reason for ignoring with a wildcard and allowing (or, ignoring 'apart from') the directory itself enables Git to look inside the directory first before ignoring everything inside. We then tell Git to ignore everything 'apart from' the directory we have specified. Here's the same syntax but in order of how Git is looking at it:

wordpress/*                            # Ignore everything inside here
!wordpress/wp-content                  # Apart from this directory

wordpress/wp-content/*                 # Ignore everything inside here
!wordpress/wp-content/themes           # Apart from this directory

wordpress/wp-content/themes/*          # Ignore everything inside here
!wordpress/wp-content/themes/my-theme  # Apart from this directory

Hopefully this helps someone better understand the need for the recursive method.

查看更多
Lonely孤独者°
6楼-- · 2019-01-05 09:45

If you prefix a pattern with an exclamation point (!) it negates any previous pattern which excluded it. So, presumably, you could ignore everything, then only allow what you want by using this pattern.

查看更多
▲ chillily
7楼-- · 2019-01-05 09:45

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
查看更多
登录 后发表回答