DEV Community

Arth
Arth

Posted on • Updated on

How to undo sensitive git history in a Github repo?

You just started working on a project and the code has been around for some time and now it's starting to get traction from other contributors but there seems to be an issue with the repository and it's that the commits are really messy. You have one commit for each lousy change you made in the Readme.md or the general code of the repo. Now it's time to clean everything up so the code is maintained nice and strong.

I'm not sure if this works for other developers but I recently found out that making a local wiki of how to tread along the code I wrote and then removing previous commit history with a single commit of the final code for a cycle ( say, 8 weeks after the first prototype ) is a good idea.

Using this technique of maintaining a repo, you push the code as usual ( try to be considerate with the commits and be informational with the commit message if possible ) and then after around 8 weeks ( a sample cycle of development for me ), delete the previous commit history, make sure there is enough information on the existing code on the wiki ( local or otherwise. a Readme with enough information works just as fine ) of the project such that you do not get lost in the code yourself and once you're sure of it, move on to the cleaning of the commit history.

But how do you do that? This method works perfectly fine for WSL ( I recommend highly recommend WSL for development if you are on Windows ) and other UNIX systems.

  • Make sure that git is set up on your system. If you're not sure, try running the git command. If it's not set up, I suggest you go through this procedure.

  • Once you're done with setting up everything, even the SSH keys part, clone the repo you're working on using git clone.

  • cd into the repository.

  • checkout with a new branch using git checkout --orphan newbranch

  • Add all the files from the master repo. git add -A

  • Commit the changes now using git commit -am "commit message".

  • Delete the master branch using git branch -D master

  • Rename the newbranch you created earlier to master using git branch -m master.

  • Now force update your repo using git push -f origin master.

These steps would work on Windows if you're using GitBash but there might be some errors. If there are, either look them up on StackOverflow effectively or idk contact me lol.

Hope this post meant something useful to you in one way or the other.
Keep chillin'!

Update: To be very clear, the part where I talk about cleaning the repo every 8 weeks is a specific case scenario where you are working on an MVP or code that just achieves a certain purpose and isn't production-ready yet. This technique should be avoided in the production code.

Top comments (1)

Collapse
 
kiraniyerdev profile image
Kiran Iyer

This is a great article, it cleared up many things for me in under 2 mins but I still have to practically apply them and test it out. You recommend doing this on WSL, so for those who don't know their way around on Linux this process is going to be a lot more difficult, so that begs the question if it can be done in the same perfect manner on Window10?

Also, you mentioned about having a local wiki helps, is there a link or something you can point me to for setting is up?

FYI I ran into this article via google, I'm about to take a step out of my comfort zone and wanna help someone maintain his repository. I have never done this before so I'm gonna follow up on all your pointers.