简体   繁体   中英

How to ignore some directories but NOT some files inside those directories with git?

Please note the following structure:

/assets --> this dir shouldn't be ignored. but SOME OF their contents SHOULD.;
ignoredfile.jpg --> should be ignored.
ignoredfile2.gif --> --> should be ignored.
 /blog --> this dir shouldn't be ignored. but SOME OF their contents SHOULD.;
  /images --> this dir shouldn't be ignored. but ALL their contents SHOULD.;

Inside /assets folder I have a gitignore file with:

# Ignore everything in this directory
*
# Except this file
!.gitignore
!/images
!/resize
!/blog

Inside '/assets/blog' I have another .gitignore .

# Ignore everything in this directory
*
# Except those files
!.gitignore
!/images

ISSUE

When I push to the remote server, git DO pushed the /blog directory but NOT the /blog/images one. Why ?

How can I solve this ?

Just remove it from .gitignore and re-add it using the usual git add -A <directory-name> , and you should be fine..

Beware of merges that might remove the directory again, but if you are merging everything correctly you should be fine.

Update

You cannot commit empty directories in git; a common way to do that is to put an empty "placeholder" file inside it, just to say you want it to stay there.

For example, put an empty hidden file named .keep into the /images directory and change the .gitignore like this:

/images/*
!/images/.keep

now, you add/commit the .keep file and you should be fine..

Alternate solution

Place a .gitignore inside /images , with this content:

*
!.gitignore

git does not track directories. If you ignore all files in that directory, git will not record that that directory even exists. You could create an (otherwise useless) empty file called eg .keep in that directory and git add that file, but I'm not sure if that suits your end goal or not.

Remove the directory from your .gitignore .

Add the directory to your repo using git add <dir> .

Commit and push again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM