I\'m trying to remove a previously tracked directory from git, which works, but it\'s being added back with each subsequent git add .
, git add -A
, etc.
You can do
git check-ignore -v --no-index path/with/unexpected/result
to see why git add
did or didn't add that path.
git check-ignore docs.
In particular, the point is you want to check what's actually getting added, not a directory.
further, do find . -name .git
. Submodules are nested repos, .gitmodules
and the submodule command are handy but they're just there to help with them.
Here is what I do to ignore node_modules folder after I tracked it.
Create the .gitignore file and add node_modules to it.
Commit the .gitignore file. After this point, whatever you update in the node_modules folder won't appear in git status
.
But this does not delete what we already have on the repo under the node_modules folder. So now we need to remove whatever we have committed previously.
For this, use git rm --cached node_modules -r
Now git status
will show that the files are deleted.
Use git commit -m "node_modules removed"
command with any message.
Now everything should be removed from the repo and future changes will not be tracked.
The origin has to be bare. Here is a little bash to demonstrate it. Feel free to edit it, if it does not represent your use case.
#!/bin/bash
rm -rf /tmp/git_test
mkdir /tmp/git_test/
git init --bare /tmp/git_test/primary_repo
# populate repo
cd /tmp/git_test/
git clone ./primary_repo ./secondary_repo
cd secondary_repo
echo hi_bob > some_content
mkdir node_modules
echo hi_dave > node_modules/moduleA
git add .
git commit -m "populated primary"
git push
# do the removal in tertiary
cd /tmp/git_test/
git clone ./primary_repo ./tertiary_repo
cd tertiary_repo
echo node_modules >> .gitignore
git add .gitignore
git rm -r --cached node_modules
git commit -a -m "removed node_modules"
git push origin master
rm -r node_modules
echo --------------------------------
echo git status:
git status
gitignore - Specifies intentionally untracked files to ignore.
$HOME/.config/git/ignore, $GIT_DIR/info/exclude, .gitignore
Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):
To ignore entire directory you should use /**
,
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.
Or
You can ignore the entire directory by adding this line to your root .gitignore file:
/Dir_Name
Instead, you can add a /logs/.gitignore file containing this:
[^.]*
The directory will remain in your repo, but all files inside /directory will be ignored. Easy!
The steps you need to follows are,
Remove it from the project directory (without actually deleting it):
git rm --cached folder/*
Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):
$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar