git negation of excluding hidden directory with hidden files - linux

I manage config files in my home directory with git.
Here is the simplified content of the .gitignore file in my home directory.
*
!.bashrc
!.bash_aliases
!.gitignore
I created a new directory in my home directory .bashrc_includes with several files:
.bashrc_includes/
├── .bashrc_android
├── .bashrc_asdf
└── .bashrc_sdkman
I would like to add this directory with all the files inside it to the repository. I tried to add !.bashrc_includes/* to my .gitignore, but look like it is still ignored by git.
What is the right way to do it?

Adding !.bashrc_includes/* will exclude the files from the ignored files, but the directory itself is still ignored!
The following .gitignore allows you to exclude the directory and the files inside from the ignored files:
*
!.bashrc
!.bashrc_includes/*
!.bashrc_includes/
!.bash_aliases
!.gitignore
You can find some more in-depth explanations in this question: .gitignore exclude folder but include specific subfolder.

Related

How can I pull request if i have over 14k changes, is it normal that node_modules is so big? [duplicate]

I have a project containing multiple other projects :
Main project
Mini project 1
Mini project 2
All containing node_modules folder. I want git to ignore the folder no matter where it is starting from the root folder. Something like this to add in .gitignore :
*node_modules/*
Add node_modules/
or node_modules
to the .gitignore file to ignore all directories called node_modules in the current folder and any subfolders like the below image.
Use the universal one-liner in terminal in the project directory:
touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ; git status
It works no matter if you've created a .gitignore or not, no matter if you've added node_modules to git tracking or not.
Then commit and push the .gitignore changes.
Explanation
touch will generate the .gitignore file if it doesn't already exist.
echo and >> will append node_modules/ at the end of .gitignore, causing the node_modules folder and all subfolders to be ignored.
git rm -r --cached removes the node_modules folder from git control if it was added before. Otherwise, this will show a warning pathspec 'node_modules' did not match any files, which has no side effects and you can safely ignore. The flags cause the removal to be recursive and include the cache.
git status displays the new changes. A change to .gitignore will appear, while node_modules will not appear as it is no longer being tracked by git.
Edit - (Before 2022-04-09)
In a new monorepo setup I found just using this
node_modules
solved it to ignore all the node_modules in the subdirectory, note there is no slash before or after which means recursive.
Reference
Old Way - (Before 2022-04-09)
**/node_modules
** is used for a recursive call in the whole project
Two consecutive asterisks ** in patterns matched against full pathname may have special meaning:
A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory foo anywhere, the same as pattern foo. **/foo/bar matches file or directory bar anywhere that is directly under directory foo.
A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/\**/b matches a/b, a/x/b, a/x/y/b and so on.
Other consecutive asterisks are considered invalid.
Why this approach is better than node_modules/
The ** acts as a recursive pattern. It is useful in monorepo projects where you have node_modules in sub directories. ** will search for all the node_modules inside the directory & ignore them.
Reference
First and foremost thing is to add .gitignore file in my-app. Like so in image below.
and next add this in your .gitignore file
/node_modules
Note
You can also add others files too to ignore them to be pushed on github. Here are some more files kept in .gitignore. You can add them according to your requirement. # is just a way to comment in .gitignore file.
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Adding below line in .gitignore will ignore node modules from the entire repository.
node_modules
Create .gitignore file in root folder directly by code editor or by command
For Mac & Linux
touch .gitignore
For Windows
echo >.gitignore
open .gitignore declare folder or file name like this /foldername
**node_modules
This works for me
recursive approach to ignore all node_modules present in sub folders
it will automatically create a .gitignore file if not then create a file name .gitignore
and add copy & paste the below code
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
these below are all unnecessary files
See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
and save the .gitignore file and you can upload
Add below line to your .gitignore
/node_modules/
In my case, writing /node_modules without the foreslash after was not working
you can do it with SVN/Tortoise git as well.
just right click on node_modules -> Tortoise git -> add to ignore list.
This will generate .gitIgnore for you and you won't find node_modules folder in staging again.
If your subproject/client node_modules gets committed,
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
then add "node_modules" at the last line.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules
# ------ Up Here ------
Follow these steps -
open git bash on the folder where your project is, or open vs code terminal
by hitting
CTRL + `
write, [ echo > .gitignore ] in the terminal or, create a file [.gitignore] directly into the folder
then write this to ignore node modules from entire repository
node_modules
or, try this to ignore all node_modules from multiple sub folders
**node_modules
Note : if you make spelling mistake while typing the name of the folder or file, it won't work. so, double check spelling
Add node_modules/ or node_modules to the .gitignore file to ignore all directories called node_modules in the current folder and any subfolders.
In Mac,
Open sourcetree
click particular project
click settings
click Advanced
click Edit gitignore
Write "node_modules"
And Save.
If you wanna do it via the command line, type in echo node_modules/ > .gitignore. You can check if git is tracking the folder by typing in git status. If its being tracked type in git rm -r --cached node_modules.
Add below line to your .gitignore
*/node_modules/*
This will ignore all node_modules in your current directory as well as subdirectory.
just add different .gitignore files to mini project 1 and mini project 2. Each of the .gitignore files should /node_modules and you're good to go.
foe the ones who are trying the answers above and still facing the problem
I've tried almost all of the answers eventually , it fixed my problem with ignoring node_modules but only after i committed the changes !

Why is multiple Gitignore ignored?

The folder structure is as follows.
Created for personal project.
"SERVER" directory works as a server with nodejs, and "CLIENT" directory works with React. So, if you run npm run start inside the "SERVER" folder, the server starts, and the react html generated by "CLIENT" is imported and displayed on the screen.
In git, there is a folder called "GATHER" that contains all of these CLIENT and SERVER folders.
Currently the .gitignore file is only inside the "CLIENT" folder. The contents of the "CLIENT" folder are as follows.
/node_modules
/.pnp
.pnp.js
/build
So, in "SERVER", all parts that need to be added to .gitignore such as node_modules and build are detected as changes.
To solve this problem, I added .gitignore to the parent folder of "CLIENT" and "SERVER", but it doesn't work.
So, as a result of searching, I found that it works even if there are multiple gitignores.
I created a gitignore file in the "SERVER" folder and entered the same code as "CLIENT". But it doesn't work. node_modules are still being tampered with.
I tried git rm --cached node_modules
fatal: pathspec 'node_modules' did not match any files
I only get this error. How can I solve this?
In conclusion, I want to apply .gitignore to each of the "SERVER" and "CLIENT" folders in the GATHER folder.
If your shell current working path is where the .gitignore (and SERVER folder) are, the command to use is:
cd /path/to/repo/GATHER
git rm -r --cached -- SERVER/
To remove the full SERVER content (that you want to ignore).
The OP adds in the comments:
build/ and node_modules/ (directories that should be ignored) are all being ignored.
In addition, since it is recommended to manage only one gitignore file, there were originally two gitignore files: CLIENT internal gitignore and SERVER internal gitignore.
I ended up putting one gitignore in the parent directory of these two directories.
To check if this work, you can use git check-ignore -v
git check-ignore -v -- CLIENT/build/a/file/inside
If you see an ignore rule, the file is ignored.

gitignore all node_modules directories and subdirectories

If I put this in .gitignore:
node_modules
it doesn't appear to ignore subdirectories that contain a node_modules folder.
So my guess is that this will work:
*/node_modules/
does anyone know what the right syntax is for ignore files to ignore matching subfolders?
You can ignore sub-directories indeed, and the syntax you proposed is correct, here is a working example of a .gitignore i'm using :
# Bundler
Gemfile.lock
bin/*
.bundle/*
*/test/

How to use .gitignore

I want to exclude node_modules directory except for one file.
This is what the relevant part of my .gitignore looks like - I have tried a few things but no luck:
.gitignore
....
# node.js
#
node_modules/
npm-debug.log
yarn-error.log
....
I think I need to do something like:
node_modules/
!node_modules/path/to/index.js
But it says here that the above won't work due to some git restriction. Anyway, the answer is probably in that link, I'm just having a little trouble figuring it out.
Your way:
node_modules/
!node_modules/path/to/index.js
Nope! Due to a performance-related quirk in Git, you can not negate a file that is ignored due to a pattern matching a directory.
I don't sure way to
git add .
to add your index.js and all file exclude node_modules
But you can use git add node_modules/path/to/index.js -f
-f because your file are ignored by one of your .gitignore files
and use node_modules/ in .gitignore
git won't ignore your index.js file (added it to cache).
Another solution is to move your react-native-calendars directory outside your node_modules directory, e.g move it to a directory name nmod2. and then when you import it, add the path like var react-cal = require('./nmod2/react-native-calendars'); instead of var react-cal = require('react-native-calendars');. So that you can put the whole node_modules directory in .gitignore. Hope it helps

npm module missing files after publish

For reference, the repo is https://github.com/microsoftly/luis-response-builder.
The node module files are generated with tsc and output to the dist folder. I have a prepublishOnly step that removes the dist folder, runs tsc, then runs the test against the transpiled js. The tests pass when I publish just fine.
The problem is, when I install the project anywhere else, the dist folder contains only the file with the path dist/src/index.js.
I cannot for the life of me figure out why the file is missing when installed but not when published.
Quoting from npm-publish Documentation:
All files in the package directory are included if no local .gitignore or .npmignore file exists. If both files exist and a file is ignored by .gitignore but not by .npmignore then it will be included.
Your repository's .gitignore file contains the following:
node_modules
dist
*.env
yarn-error.log
Since dist is being ignored, it's not committed with npm publish, as per the documentation.
Check out the package.json documentation about files.
Since you haven't included the files key, it will only include the file specified in main (along with some other default files).
The files value is an array so you can include multiple files and/or folders.
eg:
files: [
"dist",
"config/somefile.js"
]

Resources