

The string 'Initial' will show up as commit message of the first commit. The SHA is the ID of the previous commit of the commit you want to keep (in this example, 8365366). You need this because this commit will hold all base files for the history you are keeping.

This commit will appear as the first commit in your truncated repo. Here's what worked for me for trimming down the history using latest git 2.26:įirst create a dummy commit. There are too many answers here which are not current and some don't fully explain the consequences. If you still have some old references (tags, branches, remote heads) that point to older commits, they won't be cleaned up and you won't save more disk space. At least, don't have the same entries in there, it will fail. But be careful not to use grafts and shallow at the same time.

So you could run something like this: git fetch -prune But be careful that nothing is pointing at a commit from before. If you have a shallow copy of another repository and just want to save some disk space, you can update. In order to forget all the old commits: git prune I didn't done step 2, because I wanted my copy to stay compatible with the upstream. Every copy of this repository needs to be updated forcefully. If you want to make this grafted fake parent a real one, then run: git filter-branch -all The above command will overwrite the grafts file if present. Git rev-parse gives us the commit id of the 2000th parent of the current commit. If we wanted to say that we care only about the last 2000 commits, we can type: git rev-parse HEAD~2000 >. A line with just a commit id, says that the commit doesn't have a parent. git/info/grafts can define fake parents for a commit. Ignore everything older than a certain commit I needed to read several answers and some other info to understand what I was doing.ġ. I tried to edit the answer, but since it is a substantial change to answer, my edit was rejected, so here's the information! Remark: I know this is almost the same aswer as but there are some important extra commands and informations here. NOTE that old tags will still remain present so you might need to remove them manually Git gc -aggressive # aggressively collect garbage may take a lot of time on large repos Git prune -progress # delete all the objects w/o references # The following 2 commands are optional - they keep your git repo in good shape. Git branch -D temp # delete the temp branch Git rebase -onto temp $1 master # now rebase the part of master branch that we want to keep onto this branch Git commit -m "Truncated history" # create a first commit on this branch Git checkout -orphan temp $1 # create a new branch without parent history ) to the commit starting from which you want to keep your history. The argument to the script ( $1) is a reference (tag, hash. This method is easy to understand and works fine. If all looks as intended, you can utilize git filter-branch -all to make it permanent.īEWARE: 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. git/info/graftsĪfter 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 >. the real root commit of your repository). You can create a graft of the parent of your new root commit to no parent (or to an empty commit, e.g. Note: this has been deprecated in favor of git replace.
