Click here to Skip to main content
14,265,813 members

Remove Commit History in git

Rate this:
0.00 (No votes)
Please Sign up or sign in to vote.
0.00 (No votes)
6 Aug 2019CPOL
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)

Share

About the Author

Benktesh Sharma
Software Developer (Senior)
United States United States
Benktesh is a member of an amazing IT Lending development team at Cuna Mutual Financial Group (CMFG) in Madison WI 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 is always some side projects he is working on such as simulation modeling of emissions and writing articles both technical and non-technical (see his blog at https://benktesh.blogspot.com/). While not coding, he is either reading both fiction and non-fiction, watching tv shows, trading stocks, and discuss climate change, global warming, emission trading, and traveling to meet people, place, and culture.

Comments and Discussions

 
-- There are no messages in this forum --
Tip/Trick
Posted 4 Aug 2019

Tagged as

Stats

1.3K views