I have an already initialized Git repository that I added a .gitignore
file to. How can I refresh the file index so the files I want ignored get ignored?
To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename
To untrack every file that is now in your .gitignore
:
First commit any outstanding code changes, and then, run this command:
git rm -r --cached .
This removes any changed files from the index(staging area), then just run:
git add .
Commit it:
git commit -m ".gitignore is now working"
To undo git rm --cached filename
, use git add filename
.
Make sure to commit all your important changes before running
git add .
Otherwise, you will lose any changes to other files.
Please be careful, when you push this to a repository and pull from somewhere else into a state where those files are still tracked, the files will be DELETED
git rm --cached filename
seems a little less drastic imho.. –
Evocative git version 1.8.2.1
. –
Tractate git rm --cached filename
, you can do git rm --cached --dry-run filename
to see what it is going to do. Also remember -r for directories. –
Strudel git rm -r --cached some-directory
This removes all files that are gitignored. After this call something like this: git commit -m 'Remove the now ignored directory "some-directory"'
git push origin master
–
Standice git push
fit into this flow? –
Ligule git push
afterwards or else this won't work –
Wailful git reset HEAD~1
to undo your last commit(recover your lost files). and use git reset HEAD .
if you only run git rm -r --cached .
–
Elwoodelwyn git rm -rf --cached .
–
Truditrudie If you are trying to ignore changes to a file that's already tracked in the repository (e.g., a dev.properties file that you would need to change for your local environment but you would never want to check in these changes), then what you want to do is:
git update-index --assume-unchanged <file>
If you want to start tracking changes again:
git update-index --no-assume-unchanged <file>
See git-update-index(1) Manual Page.
Also have a look at the skip-worktree
and no-skip-worktree
options for update-index if you need this to persist past a git-reset (via)
Update: Here's a convenient alias for seeing which files are currently "ignored" (--assume-unchanged) in your local workspace
git config --global alias.ignored = !git ls-files -v | grep "^[[:lower:]]"
git ls-files -v
If the character printed is lower-case, the file is marked assume-unchanged. see: https://mcmap.net/q/12434/-can-i-get-a-list-of-files-marked-assume-unchanged and: git-scm.com/docs/git-ls-files –
Epiblast ignored
alias, as it appears in my ~/.gitconfig file: ignored = !git ls-files -v $(git rev-parse --show-toplevel) | (grep '^[[:lower:]]' || echo 'None ignored.') && echo '\nIgnore changes with: git update-index --assume-unchanged <file> \nor track again with: git update-index --no-assume-unchanged <file>'
The toplevel part of it makes sure it searches the entire repository. –
Coquille --assume-unchanged
doesn't work with git stash
: the changes are reverted during git stash
and not reapplied during git stash pop
. See this question. –
Forgive assume-unchanged
, and thus I think you should call the alias 'hidden' not ignored. To me ignored files are ones that git doesn't look at because they match a pattern in .gitignore. You can find these files via git status -s --ignored | grep '^!!'
–
Pulvinate skip-worktree
would be a good alternative. –
Furcula git config --global alias.hidden '!git ls-files -v | grep "^[[:lower:]]"'
–
Mayfield git ls-files -v | grep '^[[:lower:]]'
–
Horoscopy git update-index
should NOT be used this way. –
Chole To untrack a file that has already been added/initialized to your repository, ie stop tracking the file but not delete it from your system use: git rm --cached filename
*.config
to your .gitignore
, you can do git rm --cached *.config
to stop tracking all *.config
files. –
Mouthpart git update-index --assume-unchanged file.name
–
Mouthpart assume-unchanged
, as @Mouthpart said, or --skip-worktree
as an alternative. –
Furcula Yes - .gitignore
system only ignores files not currently under version control from git.
I.e. if you've already added a file called test.txt
using git-add
, then adding test.txt
to .gitignore
will still cause changes to test.txt
to be tracked.
You would have to git rm test.txt
first and commit that change. Only then will changes to test.txt
be ignored.
<file>
is successfully ignored. I am using git version 1.8.1 -- If that was the issue. –
Tonguetied git rm test.txt
and here's a link to a more comprehensive answer #12661806 –
Gaither Remove trailing whitespace in .gitignore
Also, make sure you have no trailing whitespace in your .gitignore. I got to this question because I was searching for an answer, then I had a funny feeling I should open the editor instead of just cat'ing .gitignore. Removed a single extra space from the end and poof it works now :)
echo node_modules >> .gitignore
(at least on windows) –
Jerriejerrilee I followed these steps
git rm -r --cached .
git add .
git reset HEAD
After that, git delete all files (*.swp in my case) that should be ignoring.
Complex answers everywhere!
Just use the following
git rm -r --cached .
It will remove the files you are trying to ignore from the origin and not from the master on your computer!
After that just commit and push!
git add .
Credit to: dylanwooters.wordpress.com/2015/10/24/… –
Kannan If you want to stop tracking file without deleting the file from your local system, which I prefer for ignoring config/database.yml
file. Simply try:
git rm --cached config/database.yml
# this will delete your file from git history but not from your local system.
now, add this file to .gitignore
file and commit the changes. And from now on, any changes made to config/database.yml will not get tracked by git.
$ echo config/database.yml >> .gitignore
To remove just a few specific files from being tracked:
git update-index --assume-unchanged path/to/file
If ever you want to start tracking it again:
git update-index --no-assume-unchanged path/to/file
As dav_i says, in order to keep the file in repo and yet removing it from changes without creating an extra commit you can use:
git update-index --assume-unchanged filename
git update-index --no-assume-unchanged filename
–
Granada - Move the file out of the git-controlled directory
- Check the removal into git
- Move the file back into the git-controlled directory
After moving the file back, git will ignore it.
Works with directories too!
Not knowing quite what the 'answer' command did, I ran it, much to my dismay. It recursively removes every file from your git repo.
Stackoverflow to the rescue... How to revert a "git rm -r ."?
git reset HEAD
Did the trick, since I had uncommitted local files that I didn't want to overwrite.
git rm -r --cached .
didn't work for me. Git was still claiming an my textmate project file was not being tracked even though .tmproj is in my global ignore file. Resetting my local repro like this worked, though. Actually I added the 'hard' option as in git reset --hard HEAD
. That should have nearly the same effect in this case. –
Faust --hard
flag. It will throw out any uncommitted changes without a warning! –
Offensive Put the .gitignore file into your repository root not in .git folder.
If the files are already in version control you need to remove them manually.
git rm --cached
and git reset HEAD
both tools I'm fairly familiar with and just could get it from the repo. Success came from first rm --cached
, then actually manually deleting it, committing the delete, then recreating it manually. And it's gone. –
Trillion rm foo/bar && git add -u && git commit -m "removed foo/bar" && git push
. Then running touch foo/bar && git status
will show the file is now properly ignored. –
Sailor Another problem I had was I placed an inline comment.
tmp/* # ignore my tmp folder (this doesn't work)
This works
# ignore my tmp folder
tmp/
Thanks to your answer, I was able to write this little one-liner to improve it. I ran it on my .gitignore and repo, and had no issues, but if anybody sees any glaring problems, please comment. This should git rm -r --cached
from .gitignore
:
cat $(git rev-parse --show-toplevel)/.gitIgnore | sed "s/\/$//" | grep -v "^#" | xargs -L 1 -I {} find $(git rev-parse --show-toplevel) -name "{}" | xargs -L 1 git rm -r --cached
Note that you'll get a lot of fatal: pathspec '<pathspec>' did not match any files
. That's just for the files which haven't been modified.
s//$//
supposed to be s/$//
? Also, what's the point of the sed and grep commands? I'm guessing it's comment filtering from the gitignore? –
Tarim I have found a weird problem with .gitignore. Everything was in place and seemed correct. The only reason why my .gitignore was "ignored" was, that the line-ending was in Mac-Format (\r). So after saving the file with the correct line-ending (in vi using :set ff=unix) everything worked like a charm!
.gitignore
format is each line is either a comment (starting with a #
) or the whole line (including any whitespace) is full filename pattern. If you have \r
mixed into the line, git
will ignore only files that end up with \r
(you can create those if you want!). See man gitignore
for details, it's worth reading. –
Offensive One other problem not mentioned here is if you've created your .gitignore in Windows notepad it can look like gibberish on other platforms as I found out. The key is to make sure you the encoding is set to ANSI in notepad, (or make the file on linux as I did).
From my answer here: https://mcmap.net/q/11783/-gitignore-is-ignored-by-git
If you need to stop tracking a lot of ignored files, you can combine some commands:
git ls-files -i --exclude-standard | xargs -L1 git rm --cached
This would stop tracking the ignored files. If you want to actually remove files from filesystem, do not use the --cached
option. You can also specify a folder to limit the search, such as:
git ls-files -i --exclude-standard -- ${FOLDER} | xargs -L1 git rm
git rm -r --cached .
removes lot of other things! –
Moersch On my server linux server (not true on my local dev mac), directories are ignored as long as I don't add an asterisk:
www/archives/*
One thing to also keep in mind if .gitignore
does not seem to be ignoring untracked files is that you should not have comments on the same line as the ignores. So this is okay
# ignore all foo.txt, foo.markdown, foo.dat, etc.
foo*
But this will not work:
foo* # ignore all foo.txt, foo.markdown, foo.dat, etc.
.gitignore
interprets the latter case as "ignore files named "foo* # ignore all foo.txt, foo.markdown, foo.dat, etc."
, which, of course, you don't have.
© 2022 - 2024 — McMap. All rights reserved.