This question already has answers here:
How do I make Git forget about a file that was tracked, but is now in .gitignore?
(33 answers)
Closed 5 years ago.
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
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) than what you want to do is:
git update-index --assume-unchanged <file>
If you wanna 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:
Since people have been asking, here's a convenient (and updated since commented on below) 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:]]"
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
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.
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 :)
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!
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
Thanks
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
None of the answers worked for me.
Instead:
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.
There is another suggestion maybe for the slow guys like me =) Put the .gitignore file into your repository root not in .git folder. Cheers!
If the files are already in version control you need to remove them manually.
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.
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!
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://stackoverflow.com/a/11451916/406592
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
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/*
I don't know why but it made me loose a couple of hours, so I wanted to share...
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.
I'm am running macOS Sierra, and am in the process of moving all dotfiles into one directory. I have successfully exported many environment variables for various installations (vagrant, composer, oh-my-zsh etc) that allow me to install to a sub-directory of my choice.
Unfortunately, programs like npm, subversion, homestead, git, and others do not offer such configurations.
I use a dotfiles repository, where I keep my configuration files under git. The idea is not new. What I did is move them to another directory and them create a symlink to them in the home directory. It does not clean the home directory as you wanted, since it's the standard place for configuration files, as stated by Norman Gray, but at least you can version them and share them across machines.
Example:
cd ~
mkdir dotfiles
mv .gitconfig dotfiles/.gitconfig
ln -s ~/dotfiles/.gitconfig ~/.gitconfig
Check out stow. That's what I use.
I have a ~/dotfiles/ directory which has folders in it like vim/ X/, etc.
Now for example vim/ will have a .vimrc file in it, and from ~/dotfiles I can run stow vim/ and it will automatically manage the symlinks to the home directory.
I can also run
cd ~/dotfiles
for folder in ./
do [[ -d $folder ]] && stow -R $folder
done
To update all my dotfiles (the -R deletes old symlinks that don't exist anymore)
There is a good introduction here: http://brandon.invergo.net/news/2012-05-26-using-gnu-stow-to-manage-your-dotfiles.html
I have added some files to svn repository during initial revisions. But now, those files are gone and that was done by removing them directly in xcode 4.2. But they still exist in svn and I want to update the svn repository to my local project version. I know that I could use svn rm command to delete every file but the idea about that makes me annoyed because I will need to find, pick and delete every file manually. Any less painful solutions?
ps. also, any advices on how to check the difference between local project and svn repository are welcome. Currently, if I'm performing update then xcode is sayging that my local copy is up to date, so I have even manually to find the missing files.
If you don't mind going into the command line, you could run svn status and find the files you deleted. These will start with the exclamation point. Here's an example from a repository I just futzed with:
$ svn status
! subversion/pre-commit-kitchen-sink-hook.html
! subversion/svn-watch.html
! bludgen/bludgen.html
! duplicate-properties-files/fixup.html
! cvstools/group.html
! cvstools/find-branch-usage.html
A cvstools/someprogram.py
M cvstools/find-branch-usage.pl
! cvstools/scramble.html
M cvstools/findNames.pl
Now, all you need to do is find the names of the programs. If you have no spaces in the names of your programs, a simply [awk] script should be sufficient:
$ svn status | awk '/^!/ {print $2}'
subversion/pre-commit-kitchen-sink-hook.html
subversion/svn-watch.html
bludgen/bludgen.html
duplicate-properties-files/fixup.html
cvstools/group.html
cvstools/find-branch-usage.html
cvstools/scramble.html
Now that you have a list of programs, you could run that through xargs into a svn rm command:
$ svn status | awk '/^!/ {print $2}' | xargs svn rm
D subversion/pre-commit-kitchen-sink-hook.html
D subversion/svn-watch.html
D bludgen/bludgen.html
D duplicate-properties-files/fixup.html
D cvstools/group.html
D cvstools/find-branch-usage.html
D cvstools/scramble.html
Fortunately, Macs come with the Subversion command line client. This will delete the files you manually removed via Subversion and will allow you to commit your changes. You'll be able to then update your copy of the repository with the latest code. There might be some conflicts (you deleted a file that someone in a later revision updated), but these should be pretty manageable.
You can use svn diff to look for differences between your current working copy and the HEAD revision of the repository. Unfortunately, the --summarize parameter only works if you do a repository to repository difference. However, you can filter out the files that are different by using grep to look for lines that start with Index::
$ svn diff -rHEAD | grep "^Index:"
Index: windows-tools/which.pl
Index: subversion/pre-commit
Index: subversion/README
Index: subversion/control.ini
Index: subversion/pre-commit-kitchen-sink-hook.html
Index: subversion/svn-watch.html
Index: cvstools/findNames.pl
Index: cvstools/group.html
Index: cvstools/find-branch-usage.html
Index: cvstools/someprogram.py
Index: cvstools/find-branch-usage.pl
Index: cvstools/scramble.html
Index: bludgen/bludgen.html
Index: duplicate-properties-files/fixup.html
The only real issue is whether the Subversion client that XCode uses is compatible with the Subversion command line client. For example, the VisualStudio Subversion client uses _svn as the name of the Subversion information directories while the command line client uses .svn.
I believe that the XCode Subversion client is compatible with the revision 1.6.x of the Subversion command line client (and even earlier revisions all the way back to 1.4), but not with the latest 1.7 version of Subversion.
If you haven't tampered with your Mac's Subversion installation, it should be fine.
The only way that I'm aware of to do what you are trying to is:
Goto File->Source Control->Repositories
Select Your repository
Click Commit
Click Flat view icon (on the left)
Any missing files are shown as !, and you can right click to discard the change.
EDIT - Sorry, I misunderstood the problem. This may help you to find missing files, but wont help you commit. I was thinking you were trying to restore the deletions, not commit them.
How do you get a copy of an earlier revision of a file in Mercurial without making that the new default working copy of the file in your workspace?
I've found the hg revert command and I think it does what I want but I'm not sure.
I need to get a copy of an earlier revision of my code to work with for a few minutes. But I don't want to disturb the current version which is working fine.
So I was going to do this:
hg revert -r 10 myfile.pls
Is there a way to output it to a different directory so my current working version of the file is not disturbed? Something like:
hg revert -r 10 myfile.pls > c:\temp\dump\myfile_revision10.pls
The cat command can be used to retrieve any revision of a file:
$ hg cat -r 10 myfile.pls
You can redirect the output to another file with
$ hg cat -r 10 myfile.pls > old.pls
or by using the --output flag. If you need to do this for several files, then take a look at the archive command, which can do this for an entire project, e.g.,
$ hg archive -r 10 ../revision-10
This creates the folder revision-10 which contains a snapshot of your repository as it looked in revision 10.
However, most of the time you should just use the update command to checkout an earlier revision. Update is the command you use to bring the working copy up to date after pulling in new changes, but the command can also be used to make your working copy outdated if needed. So
$ hg update -r 10 # go back
(look at your files, test, etc...)
$ hg update # go back to the tip
The command you use is this:
hg cat -r 10 myfile.pls > C:\temp\dump\myfile_revision10.pls
Knowing a bit of Unix helps with Mercurial commands. Perhaps cat should have a built in alias print or something similar.
I use Eclipse Subversion client to checkout my project from a svn repository.
My coworkers has committed 2 new files which i can see on the svn remote repository but I can't get them when i update from the head.
The 2 files are in the trunk, like me.
Someone have an idea ?
Perhaps you have a sparse checkout? Then you could try do to an explicit update to the files:
svn update path/to/missing/file.txt
Old question, but I had a similar problem.
This is what I did. Check with
svn ls
and compare to
ls -1
Then
svn ls | xargs svn up
To do this recursively just add -R
svn ls -R | xargs svn up
Note that this will take a while if you have a big code base
You can also explicitly update to a revision using '-r' and then the current revision number:
svn update -r 1234 path/to/missing/file.txt
I switched to the HEAD and set the recursive options to full in the Eclispe Team menu. And now it works :-) Curious I was on the head before.