ChatGPT解决这个技术问题 Extra ChatGPT

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

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.

you can git rm --cached entire directories with the -r option, if that's helpful

j
john Smith

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


Perfect answer ! Please add a git push origin also to reflect the changes in the remote repository.
I started using this answer...and it deleted nearly everything in my project! Obviously I was doing something wrong. But I want to warn folks not to follow this suggestion casually.
Does this end up losing the history for the files when you remove and re-add them
I wonder how many commits there are using this .gitignore is now working message. :)
A
Abdur-Rahmaan Janhangeer

Create a .gitignore file, so to do that, you just create any blank .txt file. 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

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

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. 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.


@Karen_Wiznia the "git config ... _global" creates a global gitingore for all Git repos on the computer. The OP did not want to do that.
I wish I could like this more than once, I continually come back to this question seeking this reminder.
It should be "mv git.txt .gitignore" instead of "rename git.txt .gitignore".
C
Community

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

This is the better answer - it is focused only on the single file, the file that you don't want to be tracked by the repository. The highest marked answer will work as well, but it does much more than just ignore a file from tracking by git.
This is a good one. But what if we pull ? Are there any merge conflicts ?
@pramod, great question. I'm not really sure. Try it in a stripped down test repository?
E
Eli Nunez

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


This is the best and clean answer if you want to remove only a few files, not the entire repository. I give you my thumbs up.
Thank you!!! I've just level-ed up. Worked as described. After issuing git commit you can see the files affected, and then it removes them from the repo. Perfect!
For conceptual understanding only, assuming I have no other change, is the step "git add ." necessary in this case or is it a safe practice?
If there are no other staged changes, proceed to commit.
佚名

Here is one way to “untrack” any files that are would otherwise be ignored under the current set of exclude patterns:

(GIT_INDEX_FILE=some-non-existent-file \
git ls-files --exclude-standard --others --directory --ignored -z) |
xargs -0 git rm --cached -r --ignore-unmatch --

This leaves the files in your working directory but removes them from the index.

The trick used here is to provide a non-existent index file to git ls-files so that it thinks there are no tracked files. The shell code above asks for all the files that would be ignored if the index were empty and then removes them from the actual index with git rm.

After the files have been “untracked”, use git status to verify that nothing important was removed (if so adjust your exclude patterns and use git reset -- path to restore the removed index entry). Then make a new commit that leaves out the “crud”.

The “crud” will still be in any old commits. You can use git filter-branch to produce clean versions of the old commits if you really need a clean history (n.b. using git filter-branch will “rewrite history”, so it should not be undertaken lightly if you have any collaborators that have pulled any of your historical commits after the “crud” was first introduced).


sounds like a solid solution :) I presume that this is for Linux shell right ? unfortunately I am a Win7 user. Any way I can do this in windows ?
N
Nedudi

Use git clean
Get help on this running

git clean -h

If you want to see what would happen first, make sure to pass the -n switch for a dry run:

git clean -xn

To remove gitingnored garbage

git clean -xdf

Careful: You may be ignoring local config files like database.yml which would also be removed. Use at your own risk.

Then

git add .
git commit -m ".gitignore is now working"
git push

R
Roshan Poudyal

I think this is an easy way for adding a .gitignore file to an existing repository.

Prerequisite:

You need a browser to access your github account.

Steps

Create a new file in your required (existing) project and name it .gitignore. You will a get suggestive prompt for .gitignore templates as soon as you name the file as .gitignore. Either use these templates or use gitignore.io to generate the content of your gitignore file. Commit the changes. Now clone this repository.

Have fun!