Quantcast
Channel: Autarchy of the Private Cave » how-to
Viewing all articles
Browse latest Browse all 35

How to truncate git history (sample script included)

$
0
0

Under a few assumptions (most importantly – you do not have any non-merged branches,), it is very easy to throw away git repository commits older than an arbitrarily-chosen commit.

Here’s a sample script (call it e.g. git-truncate and put into your ~/bin or whichever location you have in PATH).

[CODE]
#!/bin/bash
git checkout –orphan temp $1
git commit -m “Truncated history”
git rebase –onto temp $1 master
git branch -D temp
[/CODE]

Invocation: cd to your repository, then git-truncate refspec, where refspec is either a commit’s SHA1 hash-id, or a tag.

Expected result: a git repository starting with “Truncated history” initial commit, and continuing to the tip of the branch you were on when calling the script.

If you truncate repositories often, then consider adding an optional 2nd argument (truncate-commit message) and also some safeguards against improper use – currently, even if refspec is wrong, the script will not abort after a failed checkout.

Thanks for posting any improvements you may have.

Source: Tekkub’s post on github discussions.
See also: how to remove a single file from all of git’s commits.

Share


Viewing all articles
Browse latest Browse all 35

Trending Articles