Click here to Skip to main content
15,886,788 members
Articles / DevOps / Git
Tip/Trick

Remove Commit History in git

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Aug 2019CPOL1 min read 3.5K  
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)


Written By
Engineer
United States United States
Benktesh is a senior software engineer at Sony where he is a software developer/architect. His main focus when coding is quality, simplicity, and scalability and he is experienced in a broad range of subjects (software design, software development process, project management, testing, UI, multithreading, database, mobile development, and more).

Outside of professional work, there are always some side projects he is working on such as simulation modeling of financial, energy and emissions data and writing articles both technical and non-technical (see his blog at https://benktesh.blogspot.com/). While not coding, he is either reading books fiction and/or non-fiction, watching tv shows, trading stocks, and discussing climate change, global warming, emission trading, and traveling to meet people, place, and culture.

Comments and Discussions

 
-- There are no messages in this forum --