1

I have a script that generates a lot of commits to a git repository, then periodically pushes all branches. There is no need to keep a big history on that machine and it eats up disk space pretty quickly (and my disk space is scarce).

I am currently running a periodic

git commit -a (...)
git push --all
git prune
git gc

which is not sufficient to keep disk usage low enough on the long run. I am looking for a command to add to this script that would keep only the last few commits from each branch (I only have 2) on this machine and then let git gc reclaim disk space for me.

Let me clarify: I do not want to rewrite history, just to keep the local repository .git directory as small as can be, while keeping the full history on origin. Surprisingly I found very little information on that.

EDIT: this sounds much like a shallow repository (which I never used), but from what I read here, a shallow repository means only to skip part of the history at cloning time, not forgetting data after commit/push.

There is a possible workaround, which I will use if nothing better arises:

# recreate a shallow clone every commit
git commit -a && git push
rm -rf .git/
git clone --depth=1   # shallow clone
PPC
  • 1,734
  • 1
  • 21
  • 41
  • You can use a shallow clone, though I do not know how you would update it to keep truncating N commits behind the tip. – Lasse V. Karlsen Jan 09 '18 at 19:36
  • 1
    Possible duplicate of [Converting git repository to shallow?](https://stackoverflow.com/questions/4698759/converting-git-repository-to-shallow) – piedar Jan 09 '18 at 23:14

2 Answers2

1

You could keep on amending to the last commit and force-pushing.

In your script:

git commit -a --amend --reuse-message=HEAD
git commit --force push --all

This would maintain having only the current number of commits that you currently have.

To be clear though, this is re-writing history, so it's not team-friendly.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • Unless I misunderstand, this will just keep every source modif into one single commit, which will make origin lose history too. I want origin to know everything (just like a regular project), and my "dev machine" to forget after pushing. I will amend the OP to clarify – PPC Jan 10 '18 at 19:59
0

I ended up doing a disk space check as a cron job, and whenever the local repository exceeds a threshold (200MB), then "reset" it.

git commit -a && git push            # just in case
rm -rf .git/
git clone --depth=1 https://....

This solution requires git >= 1.9 since previous versions did not allow push from a repository marked as shallow. Also note that very bad things may happen if you are not 100% sure that your working directory is clean.

This is in no way an elegant solution, but that's how I ended up dealing with the problem because I couldn't get git to maintain only the current HEAD and auto-forget the rest.

PPC
  • 1,734
  • 1
  • 21
  • 41