It probably wasn't Einstein who said that doing the same thing over and over and expecting a different result is insanity, but someone probably said it. And by all reports, that's what's happening here.
I committed and tried pushing a directory filled with large files.
git add .
git commit -m "commit message"
git push origin main
I got an error that the files were too large.
Okay. So in the first step you recklessly added all your files to the index. In the second step you turned that in a commit. This is the Bad Commit. Let's call it The Bad Commit, shall we?
In the third step, you tried to push The Bad Commit but you couldn't. That's how you know it is a Bad Commit.
I then added the directory name to my .gitignore file and tried to push again.
I got the same error.
Yes, because changing your gitignore file has no effect on any existing commits. You still have The Bad Commit. You are still trying to push it and it is still Bad so you still get the same error.
I deleted the files in that repo both locally and remotely.
I'm not sure what that means, especially the "remotely" part. But I presume you deleted the files from the working tree, i.e. the files you can actually see. Notice that this is not a "repo". It's a working tree. You cannot see a repo, it is invisible. (Well, you could sort of "see" it if you wanted to, but even if you did you wouldn't see any files you could delete. And if you did manage to delete any merely by seeing them and trashing them directly, you would break the repo and that would be the end of the game.)
Okay, so let's take it that you saw some files that were big and you deleted them. So what? That has no effect on the Bad Commit.
I committed and tried pushing again but I got the same error.
But The Bad Commit is still sitting there. Okay, so now you have The Bad Commit plus some other commit, but The Bad Commit remains there and it remains Bad.
How can I clear out those commits with the large files so I can start pushing again without losing remote AND local work?
Delete them. How? Well, you are having regret Type 2. So use git log
or git reflog
to figure out the SHA number of the commit before The Bad Commit and say
git reset --mixed SHA
That deletes The Bad Commit and the pointless commit you made after it, but leaves the work tree exactly as it is now. So now use git add
more wisely and construct a good commit. Make the commit and push it.