602

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
  • 2
    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. Jul 8, 2019 at 3:11
  • Followed the page and worked like a charm! docs.github.com/en/authentication/… Sep 8, 2022 at 22:15

2 Answers 2

1966

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

    git checkout --orphan latest_branch

  2. Add all the files

    git add -A

  3. Commit the changes

    git commit -am "commit message"

  4. Delete the branch

    git branch -D main

  5. Rename the current branch to main

    git branch -m main

  6. Finally, force update your repository

    git push -f origin main

PS: this will not keep your old commit history around

30
  • 29
    Awesome indeed. :) 0. Clone: git clone https://github.com/blahblah/blahblah :) May 16, 2015 at 13:14
  • 18
    as good housekeeing, after those commands, a nice: git gc --aggressive --prune=all might be a good idea. Same for remove repository. Jul 30, 2016 at 11:55
  • 8
    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?
    – unhammer
    Oct 24, 2017 at 11:00
  • 11
    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. Apr 11, 2019 at 21:35
  • 12
    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.
    – RWDJ
    Jun 13, 2020 at 16:48
164

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!

14
  • 7
    but if i just want to keep latest 10 commit ?
    – Chinaxing
    Dec 13, 2012 at 8:28
  • 1
    this works but it will keep the history from previous commits on the tree like @Desta Haileselassie Hagos said Dec 23, 2014 at 19:09
  • 2
    @JulioMarins: I've just tried this and pushed to GitHub. No history was kept - there is only one commit. Aug 28, 2015 at 20:58
  • 17
    @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. May 3, 2016 at 9:01
  • 1
    @TomášZato are you talking about the upstream settings in .git/config? If so, save your .git/config before, and restore it after. Feb 1, 2018 at 20:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.