Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply .gitignore on an existing repository already tracking large number of files

Tags:

git

gitignore

I have an existing Visual Studio project in my repository. I recently added a .gitignore file under my project and I assume that tells Git to ignore the files listed in the file.

My problem is that all those files are already being tracked and as far as I know Git will not ignore a file that was already tracked before a rule was added to this file to ignore it.

It was suggested to use: git rm --cached and manually un-track them but that's going to take me forever to go through them one by one.

I thought about deleting the repository and recreating it again but this time with .gitignore file present, but there must be a better way to do this.

like image 303
Tohid Avatar asked Oct 29 '13 15:10

Tohid


People also ask

How do I Gitignore a file that is already tracked?

Simply move the files to a folder outside of git, then do "git add .", "git commit". (This removed the files) then add the gitignore, referencing the files/folders, commit again to add the gitignore file to git, then copy/move back in the folders, and they should be ignored.

Can a git repo have multiple Gitignore files?

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple . gitignore files.


4 Answers

This answer solved my problem:

First of all, commit all pending changes.

Then run this command:

git rm -r --cached . 

This removes everything from the index, then just run:

git add . 

Commit it:

git commit -m ".gitignore is now working" 

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

like image 156
Tohid Avatar answered Oct 13 '22 22:10

Tohid


  1. Create a .gitignore file, so to do that, you just create any blank .txt file.

  2. Then you have to change its name writing the following line on the cmd (where git.txt is the name of the file you've just created):

rename git.txt .gitignore

  1. Then you can open the file and write all the untracked files you want to ignore for good. For example, mine looks like this:

    OS junk files [Tt]humbs.db *.DS_Store

    #Visual Studio files *.[Oo]bj *.user *.aps *.pch *.vspscc *.vssscc *_i.c *_p.c *.ncb *.suo *.tlb *.tlh *.bak *.[Cc]ache *.ilk *.log *.lib *.sbr *.sdf .pyc .xml ipch/ obj/ [Bb]in [Dd]ebug/ [Rr]elease/ Ankh.NoLoad

    #Tooling _ReSharper*/ .resharper [Tt]est[Rr]esult

    #Project files [Bb]uild/

    #Subversion files .svn

    Office Temp Files

    ~$*

There's a whole collection of useful .gitignore files by GitHub

  1. Once you have this, you need to add it to your git repository just like any other file, only it has to be in the root of the repository.

  2. Then in your terminal you have to write the following line:

    git config --global core.excludesfile ~/.gitignore_global

From oficial doc:

You can also create a global .gitignore file, which is a list of rules for ignoring files in every Git repository on your computer. For example, you might create the file at ~/.gitignore_global and add some rules to it.

Open Terminal. Run the following command in your terminal: git config --global core.excludesfile ~/.gitignore_global

If the respository already exists then you have to run these commands:

git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

If the step 2 doesn´t work then you should write the hole route of the files that you would like to add.

like image 22
SomeAnonymousPerson Avatar answered Oct 13 '22 22:10

SomeAnonymousPerson


As specified here You can update the index:

git update-index --assume-unchanged /path/to/file

By doing this, the files will not show up in git status or git diff.

To begin tracking the files again you can run:

git update-index --no-assume-unchanged /path/to/file
like image 36
Patrick James McDougle Avatar answered Oct 13 '22 23:10

Patrick James McDougle


If you added your .gitignore too late, git will continue to track already commited files regardless. To fix this, you can always remove all cached instances of the unwanted files.

First, to check what files are you actually tracking, you can run:

git ls-tree --name-only --full-tree -r HEAD

Let say that you found unwanted files in a directory like cache/ so, it's safer to target that directory instead of all of your files.

So instead of:

git rm -r --cached .

It's safer to target the unwanted file or directory:

git rm -r --cached cache/

Then proceed to add all changes,

git add .

and commit...

git commit -m ".gitignore is now working"

Reference: https://amyetheredge.com/code/13.html

like image 36
Eli Nunez Avatar answered Oct 13 '22 23:10

Eli Nunez