Remove all past Git commits

Quick note… I’m not doing this anymore with the starter repositories. About the same time I started doing this GitHub classroom started supporting template repositories and that is a way easier way to give students code from a repository without also giving them the entire history. Plus, it’s way faster when the students import into their repository. But, I’m going to leave this here since it’s still a good skill to have in your back pocket.

I’ve been using GitHub Classroom with my second and third-year students this year, and it’s overall been really great. The plan right now is to use it with my CSAPA classes as well when we get to a point where we’re working on larger projects. If you haven’t tried it yet, give it a shot.

Noticed last week that with the GitHub Education account we also qualify for a free educational Travis-CI account. Yes, the .org accounts are free anyway. But we’re using private repositories and that’s not supported by the .org version of Travis. So we need the .com.

Spent some time last week building a NetBeans project that I’ll use as a starter for later projects that supports Travis and JUnit if needed. But I didn’t want all of my commits to the template included in the project repositories, so I needed a way to remove commits.

Stack Overflow to the rescue.

The first method, and the one I used works as long as you’re not using submodules. Since I’m not, it worked great. I did tweak this just a bit from the Stack Overflow version since we’re using Windows machines in the lab.

cd /wherever-your-project-is/
rmdir .git /S
git init
git add .
git commit -m "Initial commit"
git remote add origin <github-uri>
git push -u --force origin master

The other version supposedly works with submodules, but since I didn’t need that I haven’t tried it.

cd /wherever-your-project-is/
git checkout --orphan newBranch
git add -A  # Add all files and commit them
git commit
git branch -D master  # Deletes the master branch
git branch -m master  # Rename the current branch to master
git push -f origin master  # Force push master branch to github
git gc --aggressive --prune=all     # remove the old files Newsletter

Want to stay in touch and keep up to date with the latest posts @

Leave a Reply

Your email address will not be published. Required fields are marked *