674

I want to delete all commit history but keep the code in its current state because, in my commit history, there are too many unused commits.

How can I do it?

Is there any git command can do this?

git filter-branch ?
git rebase ?
... 

My code is hosted on github.com.

2
  • 3
    1) Delete all .git files and .gitignore files in parent directory as well as subdirectory that might have separate .git/.gitignore files. In order to do so, run : rm -rf .*git command which will delete any file ending with .git. 2) Back out to parent directory and run git init which will initialize .git file by creating a new blank .git file without history 3) run git add . or git add * 4) run git commit --all -m "initial commit" 5) run git --set-upstream origin <git-url>` 6) run ` $ git push --mirror <git-repository-path` This process is going to re write history. Commented Jul 8, 2019 at 3:11
  • Followed the page and worked like a charm! docs.github.com/en/authentication/… Commented Sep 8, 2022 at 22:15

2 Answers 2

2229

Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but keep the code in its current state, it is very safe to do it as in the following:

  1. Checkout/create orphan branch (this branch won't show in git branch command):

    git checkout --orphan latest_branch
    
  2. Add all the files to the newly created branch:

    git add -A
    
  3. Commit the changes:

    git commit -am "commit message"
    
  4. Delete main (default) branch (this step is permanent):

    git branch -D main
    
  5. Rename the current branch to main:

    git branch -m main
    
  6. Finally, all changes are completed on your local repository, and force update your remote repository:

    git push -f origin main
    

PS: This will not keep your old commit history around. Now you should only see your new commit in the history of your git repository.

Sign up to request clarification or add additional context in comments.

35 Comments

Awesome indeed. :) 0. Clone: git clone https://github.com/blahblah/blahblah :)
as good housekeeing, after those commands, a nice: git gc --aggressive --prune=all might be a good idea. Same for remove repository.
Why add -A? When I do checkout --orphan, all the files are staged already; wouldn't add -A have the possibility of adding files that should stay untracked?
I want to note that if a hacker got a hold of the url to the github page (github.com/<repo>/blob/<hash>). This method will not remove that url link. Only complete deletion and recreation of the repo will.
This doesn’t remove the commits. It orphans them and, answering the question, cleans the history. But, the commits still exist, but nothing is pointing to them. Some plumbing commands or specific log commands will find them and you can even restore master. Orphan commits can be pruned with git-prune. I’ve never had to use it though, so I don’t know the exact command, nor do I know how to remove the orphan commits from the remote.
|
184

If you are sure you want to remove all commit history, simply delete the .git directory in your project root (note that it's hidden). Then initialize a new repository in the same folder and link it to the GitHub repository:

git init
git remote add origin [email protected]:user/repo

now commit your current version of code

git add *
git commit -am 'message'

and finally force the update to GitHub:

git push -f origin master

However, I suggest backing up the history (the .git folder in the repository) before taking these steps!

15 Comments

but if i just want to keep latest 10 commit ?
this works but it will keep the history from previous commits on the tree like @Desta Haileselassie Hagos said
@JulioMarins: I've just tried this and pushed to GitHub. No history was kept - there is only one commit.
@DanDascalescu the presence of only a single commit in the newly pushed master branch is very misleading - the history will still exist it just won't be accessible from that branch. If you have tags, for example, which point to older commits, these commits will be accessible. In fact, for anyone with a bit of git foo, I'm sure that after this git push, they will still be able to recover all history from the GitHub repository - and if you have other branches or tags, then they don't even need much git foo.
@TomášZato are you talking about the upstream settings in .git/config? If so, save your .git/config before, and restore it after.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.