Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / Git

Remove Commit History in git

0.00/5 (No votes)
6 Aug 2019CPOL1 min read 3.7K  
Have you ever had a need to clean up all the history in the git repository? I will show how to clean the commit history in git using tools and technique using command lines.

Have you ever had a need to clean up all the history in the git repository? I have. I dealt with this old fashioned way until I started interacting with the command line. The old fashioned way is to create a new repository and update files and replace the old with a new one. In this article, I am going to show how to clean the commit history in git using tools and techniques using command lines. In preparing this article, I used several resources and code examples and I acknowledge the original contributors listed in the reference section of this article. I plan to update this article with additional methods and I look forward to comments and suggestions for improvements.

Method 1: Re-Init the Repository

In this method, I remove and recreate the git and modify the origin and then I can push the code to remote. The following set of commands can be used:

$ rm -rf .git
$ git init
$ git add .
$ git commit -m "Starting Commit"
$ git remote add origin [......]
$ git push -u --force origin master

Method 2: Modify Branch

In this method, I initially create a temp branch, add all the files to that branch, delete the old branch and then rename the current branch to master and then push the changes to remote. Following sets of commands can do the job.

$ git checkout --orphan temp
$ git add -A
$ git commit -am "Starting Commit"
$ git branch -D master
$ git branch -m master
$ git push -f origin master

Method 3: Squashing the Commits

In this method, I combine all the commits into one which is known as squashing. For example, if I have 25 commits since the beginning, then I can combine all commits into one by using the following steps for squashing commits into one and amending the commit message:

$ git rebase -i Head~25
$ #edit the commits manually
$ git commit –amend

References/Additional Resources

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)