Git - Ignoring files with .gitignore

2

Can anyone explain to me clearly the difference between using the commands below in the .gitignore file?

  • /pasta

  • pasta/

asked by anonymous 26.11.2018 / 14:23

1 answer

8

You know you can by .gitignore in any subfolder of the project ? It took me a few years to figure this out, and I was happy with the discovery.

The first case, pasta/ , does not require the folder to be in the same directory as .gitignore . It can be any subdirectory called pasta that will be ignored.

The second case, /pasta/ forces the directory to be skipped to be in the same folder as .gitignore . So you can be very specific.

Imagine the following structure:

/--+
  |
  +- dira/ --+
   |         |
   |         +- .gitignore [1]
   |         +- subdira/ --+
   |         |              |
   |         |              +- pasta/
   |         +- pasta/
   |
   +- dirb/--+
   |         |
   |         +- .gitignore [2]
   |         +- subdirb/ --+
   |         |              |
   |         |              +- pasta/
   |         +- pasta/
   +- pasta/

The content of /dira/.gitignore (indicated by [1] ) is:

pasta/

As for /dirb/.gitignore (indicated by [2] ) is:

/pasta/

With this, the project will show the differences in the following pasta/ directories:

  • /dirb/subdirb/pasta/
  • /pasta/

And ignore the following:

  • /dira/pasta/
  • /dira/subdira/pasta/
  • /dirb/pasta/
26.11.2018 / 15:12