How to reduce the depth of an existing git clone?

git fetch --depth 10

this will fetch the newest commits from origin (if there are any) and then cut off the local history to depth of 10 (if it was longer).

for normal purposes your git history is now at length of 10. but beware that the old commits still occupy space on your disk and that they still exist in the remote repository. visible and accesible for all who have access to the remote repository.

if your aim was to have a shorter log because you currently don’t need years worth of commit history then you are done. your log will be short and most common git commands now only see 10 commits.

if your aim was to free disk space because older commits have huge binary blobs which you don’t need to work now then you have to actually remove the old commits from your disk. see below for a short description how to do so.

if your aim was to completely remove the old commits (for example to remove a password from old commits) then this is not the correct command to do so. you need to remove them from the remote repository. see below for links with more info on how to remove commits from a remote repo.

to undo a --depth and get the entire history again:

git fetch --unshallow

how to remove the old commits to free disk space.

data loss warning! read the notes and pay attention to what you are doing.

in short: to actually remove the commits to free the disk space you need to remove all references that are holding them. that is (as far as i know) the reflog, tags, branches, and stashes.

to clear the reflog:

git reflog expire --expire=all --all

to remove all tags:

git tag -l | xargs git tag -d

to remove a branch:

git branch -d branchname

branches are complicated. inform and think for yourself how to handle your branches.

as for stashes: they should be temporary anyways. so just drop them like it’s hot.

git stash drop

once you have removed all references you can call git garbage collector to remove the dangling commits:

git gc --prune=all

now the old commits should be removed from disk.

beware of data loss! read the notes below and think before you delete.


notes

about the remove all tags command: the command will remove all tags from your local repository. if all your tags are also on the remote then this is fine. the next git fetch will refetch the relevant tags. but if you have tags which are only in your local repository then they will be gone. if you want to keep them then you need to save them somehow.

similar for branches. branches which are also remote will be refetched eventually. branches which are only local will be gone.

old reflog entries are cleared automatically after a certain time by git garbage collector (about 90 days IIRC). tags and branches however will stay around forever. so if you want to free disk space from old commits you have to at least remove the tags and branches manually.

the reflog is something like a local history of past local repository states. many git commands will record the previous state of the local repository in the reflog. with the reflog you can undo some commands or at least retrieve lost data if you made a mistake. so think before you clear the reflog.

the reflog is entirely local to your local repository.


see also

https://linuxhint.com/git-shallow-clone-and-clone-depth/

http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html

How do I edit past git commits to remove my password from the commit logs?

Delete all local git branches

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)