Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resync git repo with new .gitignore file

Tags:

git

Is it possible to "refresh" a git repository after updating the gitignore file?

I just added more ignorations(?) to my gitignore and would like to remove stuff already in the repo matching the new file.

like image 227
Christian Wattengård Avatar asked Aug 16 '11 09:08

Christian Wattengård


People also ask

Can I add Gitignore after creating repository?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I change Gitignore?

Use Git update-index to ignore changes To resume tracking, run the git update-index command with the --no-skip-worktree flag. Or, you can temporarily stop tracking a file and have Git ignore changes to the file by using the git update-index command with the assume-unchanged flag.


1 Answers

The solution mentioned in ".gitignore file not ignoring" is a bit extreme, but should work:

# rm all files git rm -r --cached . # add all files as per new .gitignore git add . # now, commit for new .gitignore to apply git commit -m ".gitignore is now working" 

(make sure to commit first your changes you want to keep, to avoid any incident as jball037 comments below.
The --cached option will keep your files untouched on your disk though.)

You also have other more fine-grained solution in the blog post "Making Git ignore already-tracked files":

git rm --cached `git ls-files -i --exclude-standard` 

Bassim suggests in his edit:

Files with space in their paths

In case you get an error message like fatal: path spec '...' did not match any files, there might be files with spaces in their path.

You can remove all other files with option --ignore-unmatch:

git rm --cached --ignore-unmatch `git ls-files -i --exclude-standard` 

but unmatched files will remain in your repository and will have to be removed explicitly by enclosing their path with double quotes:

git rm --cached "<path.to.remaining.file>" 
like image 188
VonC Avatar answered Sep 23 '22 18:09

VonC