# How to add folder NON RECURSIVELY ?

6 messages from 2012-04-03 to 2012-04-04. Participants: J. Bakshi, Matthieu Moy, Dirk Süsserott.
Thread: https://gitlist.dev/t/30136

## J. Bakshi, 2012-04-03 12:01

Subject: How to add folder NON RECURSIVELY ?
Message-ID: <20120403173111.5329d7ed@shiva.selfip.org>
URL: https://gitlist.dev/e/20120403173111.5329d7ed%40shiva.selfip.org

```
Hello,

Please consider the following folder structure

` `  `  `
foo/

foo/dir1/file1

foo/dir2/file2
foo/dir2/file3

` ` ` ` ` ` ` 

I like to add the folder structure in a way that only foo/dir1/file1 is added to
the git. foo/dir2 and foo/dir3 should be added to the git also to show the directory
structure and not the contents of those folder.

It is easy in SVN by --non-recursive switch, but how can I do this in git with .gitignore ?

Thanks

BTW: Please CC to me

```

## Matthieu Moy, 2012-04-03 12:08

Subject: Re: How to add folder NON RECURSIVELY ?
Message-ID: <vpqzkatc915.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpqzkatc915.fsf%40bauges.imag.fr
In-Reply-To: <20120403173111.5329d7ed@shiva.selfip.org>

```
"J. Bakshi" <joydeep.bakshi@infoservices.in> writes:

> I like to add the folder structure in a way that only foo/dir1/file1 is added to
> the git. foo/dir2 and foo/dir3 should be added to the git also to show
> the directory
> structure and not the contents of those folder.

Git doesn't record the existance of directories. For Git, a directory
exists if and only if it has some files in it. So, "foo/dir2 and
foo/dir3 should be added to the git" cannot be done in Git.

A common workaround is to create a dummy file, typically .gitignore,
within the directories you want to add. Depending on the intended use of
the directory, you may want this file to be empty (to tell your
collaborators "there's nothing here for now, but there will be later"),
or to contain '*' to mean "there will never be any tracked files in this
directory".

IOW,

touch foo/dir2/.gitignore foo/dir3/.gitignore
git add foo/dir2/.gitignore foo/dir3/.gitignore

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

```

## J. Bakshi, 2012-04-03 12:22

Subject: Re: How to add folder NON RECURSIVELY ?
Message-ID: <201204031223.q33CMvQC004186@VM-MailSafe-02.soltecsis.com>
URL: https://gitlist.dev/e/201204031223.q33CMvQC004186%40VM-MailSafe-02.soltecsis.com
In-Reply-To: <vpqzkatc915.fsf@bauges.imag.fr>

```
On Tue, 03 Apr 2012 14:08:38 +0200
Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:

> "J. Bakshi" <joydeep.bakshi@infoservices.in> writes:
> 
> > I like to add the folder structure in a way that only foo/dir1/file1 is added to
> > the git. foo/dir2 and foo/dir3 should be added to the git also to show
> > the directory
> > structure and not the contents of those folder.
> 
> Git doesn't record the existance of directories. For Git, a directory
> exists if and only if it has some files in it. So, "foo/dir2 and
> foo/dir3 should be added to the git" cannot be done in Git.
> 
> A common workaround is to create a dummy file, typically .gitignore,
> within the directories you want to add. Depending on the intended use of
> the directory, you may want this file to be empty (to tell your
> collaborators "there's nothing here for now, but there will be later"),
> or to contain '*' to mean "there will never be any tracked files in this
> directory".
> 
> IOW,
> 
> touch foo/dir2/.gitignore foo/dir3/.gitignore
> git add foo/dir2/.gitignore foo/dir3/.gitignore
> 

Thanks for your suggestion.
So .gitignore for foo/dir2/  and  foo/dir3/ should add those directories non recursively.

Could you please suggest how can I add foo/dir1/file1 ?

guess

touch foo/dir1/.gitignore
git add foo/dir1/
git add foo/dir1/file1

??

```

## Dirk Süsserott, 2012-04-03 13:12

Subject: Re: How to add folder NON RECURSIVELY ?
Message-ID: <4F7AF740.9070906@dirk.my1.cc>
URL: https://gitlist.dev/e/4F7AF740.9070906%40dirk.my1.cc
In-Reply-To: <201204031223.q33CMvQC004186@VM-MailSafe-02.soltecsis.com>

```
Am 03.04.2012 14:22 schrieb J. Bakshi:
> On Tue, 03 Apr 2012 14:08:38 +0200
> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:
> 
>> "J. Bakshi" <joydeep.bakshi@infoservices.in> writes:
>>
>>> I like to add the folder structure in a way that only foo/dir1/file1 is added to
>>> the git. foo/dir2 and foo/dir3 should be added to the git also to show
>>> the directory
>>> structure and not the contents of those folder.
>>
>> Git doesn't record the existance of directories. For Git, a directory
>> exists if and only if it has some files in it. So, "foo/dir2 and
>> foo/dir3 should be added to the git" cannot be done in Git.
>>
>> A common workaround is to create a dummy file, typically .gitignore,
>> within the directories you want to add. Depending on the intended use of
>> the directory, you may want this file to be empty (to tell your
>> collaborators "there's nothing here for now, but there will be later"),
>> or to contain '*' to mean "there will never be any tracked files in this
>> directory".
>>
>> IOW,
>>
>> touch foo/dir2/.gitignore foo/dir3/.gitignore
>> git add foo/dir2/.gitignore foo/dir3/.gitignore
>>
> 
> Thanks for your suggestion.
> So .gitignore for foo/dir2/  and  foo/dir3/ should add those directories non recursively.
> 
> Could you please suggest how can I add foo/dir1/file1 ?
> 
> guess
> 
> touch foo/dir1/.gitignore
> git add foo/dir1/
> git add foo/dir1/file1
> 

Almost ;-)

Since foo/dir1 already contains file1 there's no need to add a
.gitignore in dir1 (except, of course, you really want to ignore sth. in
that directory).

If this is your directory structure:

foo/dir1/file1
foo/dir2
foo/dir3

foo/dir1/ is automatically added when you "git add foo/dir1/file1".
To add the (empty) directories dir2 and dir3 you need some placeholder
file in them because git does not track directories, only files
(together with their path).

Thus:

touch foo/dir2/.gitignore
touch foo/dir3/.gitignore
git add foo/dir2/.gitignore
git add foo/dir3/.gitignore

If you add a directory (like "git add foo/dir1/) git will add all files
in that directory (recursively), again, except for empty directories.


HTH,
	Dirk

```

## Matthieu Moy, 2012-04-03 13:15

Subject: Re: How to add folder NON RECURSIVELY ?
Message-ID: <vpqaa2tc5xi.fsf@bauges.imag.fr>
URL: https://gitlist.dev/e/vpqaa2tc5xi.fsf%40bauges.imag.fr
In-Reply-To: <201204031223.q33CMvQC004186@VM-MailSafe-02.soltecsis.com>

```
"J. Bakshi" <joydeep.bakshi@infoservices.in> writes:

> Could you please suggest how can I add foo/dir1/file1 ?
>
> guess
>
> touch foo/dir1/.gitignore
> git add foo/dir1/

Not if you want only "file1" to be added. As I said, Git doesn't know
and doesn't want to know about directories. So "git add directory/" is
just a shorthand that adds all (non-ignored) files in this directory.

> git add foo/dir1/file1

This last line alone will do it.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

```

## J. Bakshi, 2012-04-04 06:52

Subject: Re: How to add folder NON RECURSIVELY ?
Message-ID: <201204040652.q346qaC8020941@VM-MailSafe-02.soltecsis.com>
URL: https://gitlist.dev/e/201204040652.q346qaC8020941%40VM-MailSafe-02.soltecsis.com
In-Reply-To: <vpqaa2tc5xi.fsf@bauges.imag.fr>

```
On Tue, 03 Apr 2012 15:15:37 +0200
Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:

> "J. Bakshi" <joydeep.bakshi@infoservices.in> writes:
> 
> > Could you please suggest how can I add foo/dir1/file1 ?
> >
> > guess
> >
> > touch foo/dir1/.gitignore
> > git add foo/dir1/
> 
> Not if you want only "file1" to be added. As I said, Git doesn't know
> and doesn't want to know about directories. So "git add directory/" is
> just a shorthand that adds all (non-ignored) files in this directory.
> 
> > git add foo/dir1/file1
> 
> This last line alone will do it.
> 

Thanks a lot to all of you for your valuable suggestion.

```
