Why does Git automatically ignore most Node files?
-
21-12-2019 - |
题
I'm new at Node.js & NPM, and I'm experimenting by writing a website & adding it on Git (it's public, but only because I don't want to pay for private hosting). When I push the files to Github, Git automatically ignores pretty much every file related to Node, and I can't figure out why. Specifically, it seems to be ignoring all node_modules
folders, Gruntfile.js
es, and package.json
s. Is this because of Git or Node, or is it a dummy move somewhere on my part?
When I do git add -A
, git add .
or git add -u
, all of which are supposed to include all added & deleted files (I think), and then use git commit
or git commit -a
I get something like this:
$ git commit -m "hopefully added all files"
On branch dev
Your branch is up-to-date with 'origin/dev'.
Untracked files:
.jshintrc
app/
libs/Gruntfile.js
libs/node_modules/.bin/_mocha
libs/node_modules/.bin/_mocha.cmd
libs/node_modules/.bin/mocha
etc.
nothing added to commit but untracked files present
I tried searching for .gitignore
anywhere in the root folder, but all I could find was a gitignore
file in my Bootstrap folder (speaking of, why is that there?), but if I'm correct, that shouldn't affect folders above it? It had rules for node_modules, but not Gruntfile.js or package.json, and when I commented all rules on it the files were still ignored.
This is a submodule, so it uses the .git
folder of the parent project. That parent project has nothing but comments in its .git/info/exclude
, and also has no .gitignore
. No other submodules have a .gitignore
, either.
So why is this happening?
EDIT: git cherry
& git diff
both show nothing. git status
shows:
$ git status
On branch dev
Your branch is up-to-date with 'origin/dev'.
Untracked files:
(use "git add <file>..." to include in what wi
libs/Gruntfile.js
libs/node_modules/.bin/_mocha
libs/node_modules/.bin/_mocha.cmd
libs/node_modules/.bin/mocha
libs/node_modules/.bin/mocha.cmd
libs/node_modules/grunt-contrib-compass/
etc.
nothing added to commit but untracked files present (use "git add" to track)
EDIT 2: This website is a submodule, but I have never used .gitignore
or .git/info/exclude
on the superproject or any other submodules. Nevertheless, could this have anything to do with the problem?
EDIT 3: As I said, git add -A
, git add .
& git add -u
never add anything from node files or folders, but adding individual files & folders does. This makes it even weirder.
解决方案
If I understand it correctly, you have your project git repo that contains submodule repo in it. Can you please try this workflow?
- In some folder clone your submodule-repo
- In the same folder clone you project-repo and run
cd project-repo
git submodule init
git submodule update
cd ../submodule-repo
- Make changes and
git commit -a; git push
cd ../project-repo/submoduleDir
git pull
Now, you should have the parent repo pointing to the new submodule revision and when you cd to the project-repo you can commit this change (project-repo pointing to the latest submodule-repo revision).
More on this topic can be found here:
* http://git-scm.com/docs/git-submodule
* http://joncairns.com/2011/10/how-to-use-git-submodules/
Hope it helps.