Customize .git depth for all repositories?

Viewed 125

Suppose I clone from a 30000 commit repo on GitHub. If my understanding is correct, it will copy over all 30000 commits, which can take up a lot of disk space. However, 99% of the time I don't care about 29990 of those commits, since I only need to see the most recent history from git log.

I am aware of using git clone --depth N in order to only checkout the last N commits, however I'm wondering if there is a way to continually enforce this?

E.g. I checkout the latest 1000 commits. 200 more commits are then made on remote, so the next time I pull, the oldest 200 commits are removed from disk and the new 200 commits are added, so that I only ever store 1000 commits locally.

I don't have a very in-depth understanding of git, so any explanation is very helpful!

1 Answers

After the initial shallow clone with git clone --depth 1000 you can continue maintaining the shallow depth using git fetch --depth 1000.

Please remember that git fetch doesn't update the currently checked out branch so you have to do something like git pull --depth 1000 to update the current branch. Or git merge origin/master or git rebase origin/master.

You can also do git reset --hard origin/master. But let's me remind that git reset --hard removes the current uncommitted changes so please be careful.

There is no way to automate depth maintaining. You need to pass --depth 1000 to any git clone/fetch/pull command you run.

Related