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