Did you forgot to add some rules into your .gitignore and committed some files by mistake? Here is how to fix that. Be aware that this procedure is going to overwrite the entire history of the project and your collaborators will have to clone the repository again.
Fix your .gitignore files. You can have more than one, by placing them into different folders (useful for projects using different programming languages). Use https://gitignore.io to generate good defaults.
git rm -r --cached .
git add .
git commit -am "Remove ignored files"
git push
Install pipx (useful to manage Python tools):
python3 -m pip install --user pipx
python3 -m pipx ensurepath
Install git-filter-repo:
pipx install git-filter-repo
git-filter-repo should be executed in a freshly cloned repository and rewrites the whole history of the projects, so it is safer to keep the previous project directory.
origin="$(git remote -v | awk '{ if (NR == 1) print $2 }')"
branch="$(git symbolic-ref --short HEAD)"
cd ..
git clone --no-local "$OLDPWD" "${OLDPWD}_fixed"
cd "$_"
git filter-repo --invert-paths --path .git/ $(git show --diff-filter=D --summary --format='' | cut -d' ' -f 5- | xargs -I% echo -n ' --path %')
git remote add origin "$origin"
git push --set-upstream origin "$branch" --force
You can delete the old repository folder and rename the new one afterwards.
@bniebuhr a similar system can also be used to remove everything except a directory, and then make the content of such directory a new repository (useful to unbundle a library, for example):
$neworigin
should be the new repository (like[email protected]:username/myrepo.git
, and$directory
is the directory containing the module/code you want to extract.