Callum Linington
Callum Linington

Reputation: 14417

Clean Git Repository using gitignore

So I have this two year old C# repo on bitbucket. It didn't have a .gitignore until yesterday - I know, late.

This is a personal project.

Can I clean the repo using the .gitignore as a basis. So the worker would effectively delete anything that the .gitignore said shouldn't be there?

Upvotes: 2

Views: 3291

Answers (3)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

Notice: First of all you better take a backup of your git repository, since it always can go wrong badly (the exact behavior of git can depend on all kinds of settings/... so it is not completely predictable what the result of the process will be, so better be safe than sorry). It is sufficient to copy the directory to some other location: there is a (hidden) .git directory in that directory containing the history, tags, etc.

Only the last commit

If you want to remove only stuff for the new commits, so you don't care about the files being stored in the history of the file system, you can remove these with:

git rm --cached -r .
git add .

rm --cached -r . means you are going to remove everything from the git repository, but keep the files in the file system. You do this recursively so none of the files will be part of the repository anymore. Next you add all the files again to the repository, but then the .gitignore file will do not re-add those that should be ignored.

All commits (entire history)

In case you want to remove these files from all previous commits, I propose to use a bash script. It's probably not the most efficient.

Now what we can do is list all the files that should have been ignored at least once:

git ls-files --exclude-standard --others --directory --ignored

I would first inspect the list and check if there are no files that should be kept back. Update the .gitignore accordingly and only go to the next step if none of these files occur anymore.

If there are no more files in the list you want to keep in the repository, you can run this script:

#!/bin/bash
for f in `git ls-files --exclude-standard --others --directory --ignored`
do
    git filter-branch --force --index-filter "git rm --cached --ignore-unmatch '$f'" --prune-empty --tag-name-filter cat -- --all
done

The script iterates over the files to be ignored at least once in history and performs a filtering of the files through all the commits. Note that the files are not removed from your filesystem. But you will lose the history of these files.

Furthermore one must be a bit careful with standard .gitignores (that can for instance be found at GitHub) since I've heard there are differences between what is white/blacklisted on filesystems running Windows.

It is possible some commits will disappear, because the only differences in these commits involve files that are no longer part of the repository. For instance if you ignore a *.log file and the log contains the date; it is possible only that file.log file was modified. If you remove that file, the commit modifying the file will be removed as well.

Upvotes: 7

Roy Stevelink
Roy Stevelink

Reputation: 41

I think what you want is this: https://stackoverflow.com/a/1139797/3025093

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"

Upvotes: 2

avinashkrsharma
avinashkrsharma

Reputation: 143

You need to clone the project locally(if you don't have it already) and then remove the files or folders manually(which shouldn't be present anymore i.e. the ones mentioned in the gitignore file) and then commit those deletions. Then you can push it to your remote repo.

Upvotes: 0

Related Questions