kael
kael

Reputation: 6735

How to truncate or reduce a git repo that's on GitHub

I know various permutations of this questions are floating around, but I haven't been able to uncover anything that addresses my specific issue. The thing is this:

I've got a repo hosted on GitHub. It's the origin for two remote repos - one is my dev machine and the other is the server. I made a stupid mistake and had a script commit incremental user DB backups over the course of like a year and a half. So now I've got about 200mb of files and another 1Gb of incremental db changes committed in my git repo (yes, I learned my lesson). Visually, it looks like this, where "C" indicates a legitimate code change and "DB" means it's a commit containing only an unimportant DB backup:

C1--C2--C3--C4--DB--DB--DB--DB--DB--DB--DB--DB...(1.5 years)...DB--DB...

What I want to do is this:

                  /--DB--DB--DB--DB--DB...<--(throw all this away forever)
                 /
C1--C2--C3--C4--//<--REVERT TO THIS POINT --C5--C6--C7....

I'd basically create a branch containing all of those stupid DB commits, back my repo up to the point where the branch departs, then delete the branch. Any ideas about how to do this? Ideally, I wouldn't have to create a new GitHub repo, but I'll accept suggestions of any nature.

Upvotes: 4

Views: 4791

Answers (1)

Frank N
Frank N

Reputation: 10396

  1. find the commit you want to get back to: $ git log --before="2015-12-01" -n1 commit de4406f26ce506944b2b629890bba9e091468e05 Author: some Author<[email protected]> Date: Mon Nov 30 10:46:21 2015 +0100

  2. reset your (local) repository pointer to it:

    git reset --hard <commit-hash>

  3. force² push this to your server (² as you have to overwrite history)

    git push -f origin master

  4. The subsequent DB commits will be pruned upon next occasion, or you do it right away (pruning has no effect on your already achieved, desired cleanup. It's only about truly getting rid of that unreferenced stuff)

    git prune.


If you want to play safe, I would advise

  • you to create a branch named backup before step 1 (without checking it out, just to point to your old tip!)
  • after step 3 make sure, everything on your master branch is to your liking. Then delete that backup branch, then go for the prune.

in short:

  • It helps, to think of branches not as a whole line of commits, but rather as the ending tips, that keep the chains alive leading to it.
  • Those chain members get garbage collected, once they become unreferenced.**

Upvotes: 2

Related Questions