简体   繁体   中英

git ignore everything except a file, a folder, and an extension within that folder

I need to create a .gitignore file that follows these rules:

  1. Ignore everything in root
  2. Except for a single file fil.py
  3. Except for a single folder fol/
  4. But do ignore files with the extension .pyc within /fol

I've tried several combinations, but I can't get it to work. Here's what I've tested:

1-

# ignore everything in root
*
# except for this file
!fil.py
# except for this folder
!/fol
# But do ignore these files
/fol/*.pyc

This will track changes made to files within the fol/ folder, but will ignore all new files added to it.

2-

# ignore everything in root
*/
# except for this file
!fil.py
# except for this folder
!/fol
# But do ignore these files
/fol/*.pyc

This correctly tracks both old and new files within fol/ , while ignoring *.pyc files; but will also track files in the root folder.

3-

# ignore everything in root
/*
# except for this file
!fil.py
# except for this folder
!/fol
# But do ignore these files
/fol/*.pyc

This correctly tracks both old and new files within fol/ , and ignores files in the root folder. But it will also track all *.pyc files in the fol/ folder.

Any help will be much appreciated.

Using the following .gitignore, I believe this is working as you are hoping for.

/*
!fil.py
!/fol
*.pyc

工作实例

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