How do I remove the old history from a git repository? How do I remove the old history from a git repository? git git

How do I remove the old history from a git repository?


Maybe it's too late to post a reply, but as this page is the first Google's result, it may still be helpful.

If you want to free some space in your git repo, but do not want to rebuild all your commits (rebase or graft), and still be able to push/pull/merge from people who has the full repo, you may use the git clone shallow clone (--depth parameter).

; Clone the original repo into limitedRepogit clone file:///path_to/originalRepo limitedRepo --depth=10; Remove the original repo, to free up some spacerm -rf originalRepocd limitedRepogit remote rm origin

You may be able to shallow your existing repo, by following these steps:

; Shallow to last 5 commitsgit rev-parse HEAD~5 > .git/shallow; Manually remove all other branches, tags and remotes that refers to old commits; Prune unreachable objectsgit fsck --unreachable ; Will show you the list of what will be deletedgit gc --prune=now     ; Will actually delete your data

How to remove all git local tags?

Ps: Older versions of git didn't support clone/push/pull from/to shallow repos.


You can create a graft of the parent of your new root commit to no parent (or to an empty commit, e.g. the real root commit of your repository). E.g. echo "<NEW-ROOT-SHA1>" > .git/info/grafts

After creating the graft, it takes effect right away; you should be able to look at git log and see that the unwanted old commits have gone away:

$ echo 4a46bc886318679d8b15e05aea40b83ff6c3bd47 > .git/info/grafts$ git log --decorate | tail --lines=11commit cb3da2d4d8c3378919844b29e815bfd5fdc0210cAuthor: Your Name <your.email@example.com>Date:   Fri May 24 14:04:10 2013 +0200    Another message commit 4a46bc886318679d8b15e05aea40b83ff6c3bd47 (grafted)Author: Your Name <your.email@example.com>Date:   Thu May 23 22:27:48 2013 +0200    Some message

If all looks as intended, you can utilize git filter-branch -- --all to make it permanent.

BEWARE: after doing the filter-branch step, all commit ids will have changed, so anybody using the old repo must never merge with anyone using the new repo.


This method is easy to understand and works fine. The argument to the script ($1) is a reference (tag, hash, ...) to the commit starting from which you want to keep your history.

#!/bin/bashgit checkout --orphan temp $1 # create a new branch without parent historygit commit -m "Truncated history" # create a first commit on this branchgit rebase --onto temp $1 master # now rebase the part of master branch that we want to keep onto this branchgit branch -D temp # delete the temp branch# The following 2 commands are optional - they keep your git repo in good shape.git prune --progress # delete all the objects w/o referencesgit gc --aggressive # aggressively collect garbage; may take a lot of time on large repos

NOTE that old tags will still remain present; so you might need to remove them manually

remark: I know this is almost the same aswer as @yoyodin, but there are some important extra commands and informations here. I tried to edit the answer, but since it is a substantial change to @yoyodin's answer, my edit was rejected, so here's the information!