Skip to content

Instantly share code, notes, and snippets.

@tillig
Created March 16, 2017 21:45
Show Gist options
  • Save tillig/d5c606d7000cc7d8f187de45c67c6353 to your computer and use it in GitHub Desktop.
Save tillig/d5c606d7000cc7d8f187de45c67c6353 to your computer and use it in GitHub Desktop.
Truncate Git repo history, back-date initial commit, push to new repo
  1. Check out the branch you want to truncate.
  2. Get the SHA1 hash of the commit in that branch you want to start history.
  3. truncate.sh SHA1 branchname like truncate.sh 01cea49c9cbefd696bbd54ee41b47cbf1b6d1bd5 master
  4. Get the SHA1 hash of the new base commit.
  5. Get the date you want that commit to have in the format like Sat, 14 Dec 2013 12:40:00 -0800
  6. fixdate.sh SHA1 "date" like fixdate.sh c776e91dd573053ec09ecb1b0737909f9aacd0c9 "Sat, 14 Dec 2013 12:40:00 -0800"
  7. Add a remote to the new repo.
  8. Push the updated branch to the remote.
#!/bin/bash
git filter-branch --env-filter \
"if test \$GIT_COMMIT = '$1'
then
export GIT_AUTHOR_DATE='$2'
export GIT_COMMITTER_DATE='$2'
fi" && rm -fr "$(git rev-parse --git-dir)/refs/original/"
#!/bin/bash
git checkout --orphan temp $1
git commit -m "Truncated history."
git rebase --onto temp $1 $2
git branch -D temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment