how to delete all commit history in github? [duplicate]
Asked Answered
I

2

617

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.

Indefensible answered 5/12, 2012 at 5:5 Comment(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.Edington
Followed the page and worked like a charm! docs.github.com/en/authentication/…Lith
B
2037

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.

Burl answered 23/9, 2014 at 16:43 Comment(31)
Can I do pull requests after this, with a forked repository?Ferrocene
Awesome indeed. :) 0. Clone: git clone https://github.com/blahblah/blahblah :)Perissodactyl
@Ferrocene 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 pushIncognito
Does this delete local commit history on my machine or the history in github.com ?Edo
@Edo It deletes all commit history: both locally and on the server (github.com)Fawne
as good housekeeing, after those commands, a nice: git gc --aggressive --prune=all might be a good idea. Same for remove repository.Reasoning
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)Escort
I have a repository 170MB and did this, now it is 180MB, how can I solve it?Docile
Still history exists on local and bitbucket, maybe because project was forked from another projectMillionaire
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?Flamsteed
this will delete commit from master branch onlyBarbbarba
@Incognito 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 thisRegina
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.Pneumoencephalogram
What if I would like to effectively prune old commits? Say the first six months worth?Knickerbocker
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.Flashover
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.Albuminous
In addition to that $ git push --mirror [email protected]/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.Edington
@Begueradj It might be obvious but you need push rights to master for doing these by the way.Action
Step 5. error: refname refs/heads/latest_branch not found fatal: Branch rename failedSekofski
You may want to do "git add ." instead of "git add -A" if you do not wish to add files ignored at .gitignore.Lifework
This solution cannot delete all "contributions" on the graph(contribution activity per year)Chongchoo
This sort of works, but if you have a private repo, it may not work, if you cloned your repo, using github.com/username/yourrepo.git, but will if you cloned it, using git clone [email protected]:username/yourrepo.gitSnowberry
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.Toadeater
Make sure that you removed all TAGs tooCarlottacarlovingian
Aren't the commits still available in the reflog?Mirandamire
Is is possible to do this online from gitlabs.com?Eugenle
This is great! Unfortunately, it doesn't work on AUR: the server hooks are "denying non-fast-forward".Volumetric
Thanks man. The most important thing here is adding files to the new branch instead of merging.Omnirange
This did not work for me - the size of the .git directory stayed the same.Budwig
I get error: src refspec main does not match anySewer
This is working fine with gitlab. Only additional thing to do before: Go to your repo settings and allow force push (Settings->Repository->Protected branches, and then check "allowed to force push")Subsoil
V
168

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!

Varix answered 5/12, 2012 at 5:14 Comment(14)
but if i just want to keep latest 10 commit ?Indefensible
Can I do pull requests after this, with a forked repository?Ferrocene
this works but it will keep the history from previous commits on the tree like @Desta Haileselassie Hagos saidYordan
@JulioMarins: I've just tried this and pushed to GitHub. No history was kept - there is only one commit.Stephens
@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.Dotard
Downvoted. I don't want to delete everything, just commits. I want to keep upstream settings etc.Edelweiss
@TomášZato are you talking about the upstream settings in .git/config? If so, save your .git/config before, and restore it after.Tetrachord
This did not work for me (and no other solution)Flashover
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.Stella
if you have other branches you wish to keep? In this method, you would lose all the data.Dihedron
extremely helpful thank you!Inculpable
@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).Hadji
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.Hadji
@Hadji 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.Dotard

© 2022 - 2024 — McMap. All rights reserved.