584

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.

Christopher Bradshaw
  • 2,615
  • 4
  • 24
  • 38
Chinaxing
  • 8,054
  • 4
  • 28
  • 36
  • 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 ` 6) run ` $ git push --mirror – surendrapanday Jul 08 '19 at 03:11
  • Followed the page and worked like a charm! https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository#using-filter-branch – Jugal Panchal Sep 08 '22 at 22:15

2 Answers2

1903

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

marsnebulasoup
  • 2,530
  • 2
  • 16
  • 37
Desta Haileselassie Hagos
  • 23,140
  • 7
  • 48
  • 53
  • Can I do pull requests after this, with a forked repository? – rraallvv Nov 29 '14 at 21:08
  • 29
    Awesome indeed. :) 0. Clone: `git clone https://github.com/blahblah/blahblah` :) – Konstantinos May 16 '15 at 13:14
  • 1
    @rraallvv No you cant, it will say: *Nothing to compare, master and x branch has entirely different commit history.* Pull request on Github become auto closed when you force push – NoNameProvided Jan 16 '16 at 09:24
  • Does this delete local commit history on my machine or the history in github.com ? – vick Jan 22 '16 at 23:07
  • 1
    @vick It deletes all commit history: both locally and on the server (github.com) – Albert Romkes Feb 18 '16 at 07:04
  • 17
    as good housekeeing, after those commands, a nice: git gc --aggressive --prune=all might be a good idea. Same for remove repository. – Tuncay Göncüoğlu Jul 30 '16 at 11:55
  • Sure seems like it ought to work, but when I try it (on SourceForge), I get this error message: `remote: error: denying non-fast-forward refs/heads/master (you should pull first)` – Stevens Miller Sep 24 '16 at 15:25
  • 2
    I have a repository 170MB and did this, now it is 180MB, how can I solve it? – erickdeoliveiraleal Feb 20 '17 at 15:19
  • 1
    Still history exists on local and bitbucket, maybe because project was forked from another project – Jemshit Mar 28 '17 at 08:41
  • 6
    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 '17 at 11:00
  • this will delete commit from master branch only – Govind Singh Dec 04 '17 at 06:43
  • 1
    @NoNameProvided the -f parameter forces the commit as the new master. That said, -f is a very powerful argument. 99.9% of the time its a bad idea to use, but if you really want to replace master and its commit history with the content and history of your branch, go for it. But realize if this isn't correctly thought out, it can go pear-shaped in a really bad (non recoverable) way! I'd recommend keeping a local copy of the original master in a separate machine as a safeguard. before you consider this – Andrew Norman Jan 25 '18 at 00:58
  • 1
    This is the only solution I have been able to find that actually works, i.e. delete all commit history, without the need to delete the repo and start again. NB. I was using bitbucket as my remote. – Vince Hill Nov 15 '18 at 00:17
  • What if I would like to effectively prune old commits? Say the first six months worth? – Andrew Truckle Dec 18 '18 at 05:27
  • 1
    I can not perform the last step. I get this error message: **You are not allowed to force push code to a protected branch on this project.** – Begueradj Mar 12 '19 at 14:03
  • 11
    I want to note that if a hacker got a hold of the url to the github page (https://github.com//blob/). This method will not remove that url link. Only complete deletion and recreation of the repo will. – user1655072 Apr 11 '19 at 21:35
  • In addition to that `$ git push --mirror git@example.com/new-location.git` will push local repository to remote, and all it's branches to remote. If 'master' branch is not in protected/default branch mode this will rewrite repository in remote as you got in local. – surendrapanday Jul 08 '19 at 02:59
  • @Begueradj It might be obvious but you need push rights to master for doing these by the way. – pungggi Aug 07 '19 at 07:43
  • 1
    Step 5. `error: refname refs/heads/latest_branch not found fatal: Branch rename failed` – Ziad Akiki Nov 24 '19 at 11:11
  • 2
    You may want to do "git add ." instead of "git add -A" if you do not wish to add files ignored at .gitignore. – dgkr Jan 06 '20 at 15:02
  • This solution cannot delete all "contributions" on the graph(contribution activity per year) – koo Apr 05 '20 at 15:17
  • This sort of works, but if you have a private repo, it may not work, if you cloned your repo, using http://github.com/username/yourrepo.git, but will if you cloned it, using git clone git@github.com:username/yourrepo.git – EastsideDev Apr 18 '20 at 20:03
  • 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](https://git-scm.com/docs/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 '20 at 16:48
  • Make sure that you removed all TAGs too – PavelGP Mar 25 '21 at 11:30
  • 1
    Aren't the commits still available in the reflog? – Aaron Franke Apr 13 '21 at 07:40
  • Is is possible to do this online from gitlabs.com? – programmer Sep 17 '21 at 02:57
  • This is great! Unfortunately, it doesn't work on AUR: the server hooks are "denying non-fast-forward". – Yaroslav Nikitenko Jul 01 '22 at 15:57
  • Thanks man. The most important thing here is adding files to the new branch instead of merging. – Sapthaka Aug 12 '22 at 19:53
  • This did not work for me - the size of the `.git` directory stayed the same. – WalksB Sep 23 '22 at 04:29
  • 1
    I get `error: src refspec main does not match any` – coler-j May 21 '23 at 14:15
160

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 git@github.com: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!

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
Amir Ali Akbari
  • 5,973
  • 5
  • 35
  • 47
  • 7
    but if i just want to keep latest 10 commit ? – Chinaxing Dec 13 '12 at 08:28
  • Can I do pull requests after this, with a forked repository? – rraallvv Nov 29 '14 at 21:05
  • 1
    this works but it will keep the history from previous commits on the tree like @Desta Haileselassie Hagos said – Julio Marins Dec 23 '14 at 19:09
  • 2
    @JulioMarins: I've just tried this and pushed to GitHub. No history was kept - there is [only one commit](https://github.com/iDoRecall/comparisons). – Dan Dascalescu Aug 28 '15 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. – Robert Muil May 03 '16 at 09:01
  • Downvoted. I don't want to delete everything, just commits. I want to keep upstream settings etc. – Tomáš Zato Mar 08 '17 at 19:11
  • 1
    @TomášZato are you talking about the upstream settings in `.git/config`? If so, save your .git/config before, and restore it after. – Danny Bullis Feb 01 '18 at 20:45
  • This did not work for me (and no other solution) – Begueradj Mar 12 '19 at 15:03
  • Its not going in direction, that it will delete the complete local commit history, if you mage `git init` and then get the repo from the server? So, you get the history from the server. – Doan Dec 29 '19 at 11:55
  • if you have other branches you wish to keep? In this method, you would lose all the data. – Thiago Sep 30 '21 at 15:33
  • extremely helpful thank you! – talsibony Dec 09 '21 at 15:43
  • @RobertMuil how would history still exist if the instructions clearly say "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"? In another words this method creates a fresh git repository with the same code base that has no connection to the remote and no history attached (.git). – A.G. Mar 18 '23 at 05:02
  • This is one way to do if you want to get a fresh start but if you want to keep some commits then this is not the way to go. It is also overkill to create a new repository just because you want to clear branch history. – A.G. Mar 18 '23 at 05:06
  • @A.G. the history still exists in the remote(s). You only remove from your local, but when you push to the remote, it does not automatically remove past commits and expunge the history, it just removes the pointers from that branch. – Robert Muil Mar 29 '23 at 18:46