0

When I started working on one of my Python projects, I forgot to add my venv virtual environment directory to my .gitignore. Now, my GitHub repository is showing the additions and deletions from that commit and does not give me a good sense of when I made major changes (the venv directory had 100,000+ lines of code).

I've already updated my .gitignore file and ran git rm --cached . followed by a git add, commit, and push.

Is there any way to remove the additions and deletions from all previous commits where my venv directory changed so that GitHub will no longer count these additions/deletions in my commits? I would like to keep the parts of commits that don't include venv and only get rid of the venv part of the commit.

Jack Moody
  • 1,590
  • 3
  • 21
  • 38

1 Answers1

1
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch venv"

Please remember this rewrite the entire history of the branch so you have to force-push the branch and notify all users so that they force-pull it.

phd
  • 82,685
  • 13
  • 120
  • 165
  • Would this get rid of all the past commits? – Jack Moody Feb 18 '19 at 17:01
  • The command rewrites (not get rid of) all commits in the branch. – phd Feb 18 '19 at 17:19
  • 1
    Need `-- --all` on the end to catch every ref. @JackMoody, history is immutable, the rewrites make new history and rehang the refs, so all the old commits are still there until garbage collection cleans them out (and filter-branch keeps a copy of the originals under `refs/original/`, to get rid of those `git filter-branch -f --setup exit` to just do a no-op cleanup. – jthill Feb 18 '19 at 17:19
  • This was exactly what I was looking for. Thank you! Why do users need to force-pull it as opposed to a normal pull? – Jack Moody Feb 19 '19 at 10:47
  • Because the command rewrites the history (creates new commits instead of the current branch) and they have to **replace** (not merge, not rebase) their old branches with your new shiny branch. – phd Feb 19 '19 at 12:38