How to Remove Old Git History Before a Commit


When working with Git version control, it’s often necessary to modify the commit history manually, even though it is intended to be immutable. With a few commands, you can start fresh and delete all Git history before a certain point.

Why Remove Old Git Commits?

Sometimes when working with Git repositories, there may be a need to remove all Git history before a certain commit. This is usually useful when you want to start new with a fresh history. All the code will stay the same, but the record of all of its changes can be deleted without issue.

For example, say you have a really great website project with all the boilerplate already set up, and you want to make another website. You could start from scratch, or you could use the first website as a template by cloning it, removing all the pages and code you don’t need, and then re-committing it to a new repository. However, doing this would leave you with a messy commit history, full of all the changes before you cloned it. This is useful for forks of repositories, but in this case, it can be removed.

Removing old commits can also be useful when you want to remove sensitive data that may have inadvertently been committed. Because Git history is usually immutable, you can’t completely remove secrets or tokens that ended up in the commit logs by reverting the commit—you must completely delete it.

It’s important to note that altering Git history, in any way, can cause issues when other team members end up with an outdated repository. Git history is intended to be immutable—commits are only added, never subtracted. If you want to remove a commit from a shared GitHub repository, you must either do so before the commits are pushed, or “force push” to overwrite the repository contents. If you force push, you will need to instruct all your collaborators to re-clone the repository.

Removing All Commits Before a Commit

The first step is obtaining a reference to the commit ID that you want to target. This is easy with a GUI application or through the GitHub website, where you can view the commit log directly. Otherwise, you can use the git log command to view a list of commits.

git log

Next, we can use git checkout with the special --orphan flag, which won’t check out any version history before the given commit. For example, say we wanted to create a copy of the repository after “switch to .NET 6,” we could copy that commit’s SHA1 ID and run:

git checkout --orphan orphan-branch ee96691d2785da4bbbbb058382d41e0ccf2a7e7b

This will leave the repository in a state where the checked out HEAD references no commits, which may actually bug out Git GUI clients like GitKraken, shown here. You will need to stage all the files, and recommit the fresh start:

git add -A

git commit -m "Truncated history"

This will leave you with a new “orphan branch” with no references to commits before the truncated history.

However, it also has no references to commits after the truncated history, and master branch still points to the wrong location. To get these back, we must rebase the master branch onto the orphan branch. This will essentially cut all off the commits after the target from the master, drag them on top of the orphan branch, and then update the “master” branch label to point to this new location.

Using the same commit ID as before, use the following command to perform the rebase:

git rebase --onto orphan-branch ee96691d2785da4bbbbb058382d41e0ccf2a7e7b master

Now you should have a proper commit history, where the master branch isn’t referencing any old commits. If you’re connected to a remote, the “remote master” branch may still show the old commits, but these will go away once you force push.

The last thing to do is to delete the old data, remove the temporary orphan branch, and clean up with Git:

git branch -D orphan-branch
git prune --progress
git gc --aggressive

Once that’s done, the only thing left to do is to add a remote and push to the new GitHub repository. However, if you’re truncating history in an existing repository, you will need to force push to overwrite the history:

git push --force

And that’s all there is to it. You’ve tidied up your history old history to keep things neat and organized.





Source link

Previous articleTSMC, Foxconn hit in biggest Taiwan tech slump in 10 years
Next articleA Comprehensive Guide To The World’s Most Popular Cryptocurrency