Stripping a git repo of all committed files that should have been ignored

Brian Fitzpatrick

I recently initiated a git repo and forgot to add a .gitignore file. After my initial commit, my repo has many subdirectories and many files that should have been ignored. Is there an efficient way to add a .gitignore and then push the repo without the files meant to be ignored to a tool like GitLab or GitHub?

Stephen Kitt

If you don’t mind deleting all the ignored files in the working directory, the following will stage all committed-but-ignored files for deletion:

find . -print0 | git check-ignore -z --stdin --no-index | xargs -0 git rm -f -r --cached --ignore-unmatch --

(assuming GNU find and a recent enough git for git check-ignore). It will also delete any other ignored file or directory.

This lists all files and directories starting from the current directory, checks whether they’re ignored (even if they’re committed), and delete them, regardless of whether they’re committed, untracked etc.

This works with any .gitignore files in the tree (or even elsewhere in your git configuration).

Follow up by committing the changes and pushing them; since the changes are a new commit, you won’t need to force-push anything. Rewriting history to remove all references to supposedly ignored files is a slightly more involved task.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Ignore files that have already been committed to a Git repository

From Dev

What files in a Maven project should be committed to git?

From Dev

Show all ignored files in git

From Dev

Bare repo: Not all refs have been pushed

From Dev

Bare repo: Not all refs have been pushed

From Java

Removing multiple files from a Git repo that have already been deleted from disk

From Dev

Git committed file but it's not in the repo

From Dev

When coding in TypeScript should the generated .js files be committed to git?

From Dev

Git ignore committed files

From Dev

Git ignore committed files

From Dev

How do I treat the existing files(already committed) that I ignored later in git?

From Dev

git - How to list all files that have not been changed since a certain date?

From Dev

Cat all the files in a git repo without cloning

From Dev

echo message that all files have been uploaded

From Dev

Iteration appears to have been ignored?

From Dev

Is there any way to undo a "`git reset .`"? It 'reverted' all files - all edits have been lost from my working directory

From Dev

Intellij - Git status shows files have been changed when they have not

From Dev

Should you ignore .obj files when pushing to git repo?

From Dev

What files of a meteor project I should push to a Git repo?

From Dev

Should I add Django admin static files to my git repo?

From Dev

Remove all ignored files from the git history, but not from local filesystem

From Dev

How to preserve all ignored files in git clean -fd?

From Dev

What happens when you git push but someone else has already committed something you don't have on your local repo?

From Dev

What happens when you git push but someone else has already committed something you don't have on your local repo?

From Dev

E: Some index files failed to download. They have been ignored...” when tunning “sudo aptitude update”?

From Dev

E: Some index files failed to download. They have been ignored...” when tunning “sudo aptitude update”?

From Dev

W: Some index files failed to download. They have been ignored, or old ones used instead

From Dev

Should all files have the .php extension or should PHP output a string?

From Dev

How to make git stash include new files that have not been staged?

Related Related

  1. 1

    Ignore files that have already been committed to a Git repository

  2. 2

    What files in a Maven project should be committed to git?

  3. 3

    Show all ignored files in git

  4. 4

    Bare repo: Not all refs have been pushed

  5. 5

    Bare repo: Not all refs have been pushed

  6. 6

    Removing multiple files from a Git repo that have already been deleted from disk

  7. 7

    Git committed file but it's not in the repo

  8. 8

    When coding in TypeScript should the generated .js files be committed to git?

  9. 9

    Git ignore committed files

  10. 10

    Git ignore committed files

  11. 11

    How do I treat the existing files(already committed) that I ignored later in git?

  12. 12

    git - How to list all files that have not been changed since a certain date?

  13. 13

    Cat all the files in a git repo without cloning

  14. 14

    echo message that all files have been uploaded

  15. 15

    Iteration appears to have been ignored?

  16. 16

    Is there any way to undo a "`git reset .`"? It 'reverted' all files - all edits have been lost from my working directory

  17. 17

    Intellij - Git status shows files have been changed when they have not

  18. 18

    Should you ignore .obj files when pushing to git repo?

  19. 19

    What files of a meteor project I should push to a Git repo?

  20. 20

    Should I add Django admin static files to my git repo?

  21. 21

    Remove all ignored files from the git history, but not from local filesystem

  22. 22

    How to preserve all ignored files in git clean -fd?

  23. 23

    What happens when you git push but someone else has already committed something you don't have on your local repo?

  24. 24

    What happens when you git push but someone else has already committed something you don't have on your local repo?

  25. 25

    E: Some index files failed to download. They have been ignored...” when tunning “sudo aptitude update”?

  26. 26

    E: Some index files failed to download. They have been ignored...” when tunning “sudo aptitude update”?

  27. 27

    W: Some index files failed to download. They have been ignored, or old ones used instead

  28. 28

    Should all files have the .php extension or should PHP output a string?

  29. 29

    How to make git stash include new files that have not been staged?

HotTag

Archive