How to reduce the depth of an existing git clone?

前端 未结 4 678
感动是毒
感动是毒 2021-01-03 19:53

I have a clone. I want to reduce the history on it, without cloning from scratch with a reduced depth. Worked example:

$ git clone git@github.com:apache/spa         


        
4条回答
  •  礼貌的吻别
    2021-01-03 20:46

    since at least git version 2.14.1 (september 2017) there is

    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 actual commits still linger in your local 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 local repository. to do so you need to remove all references that are holding them. that is (as far as i know) the reflog and the tags. also branches and stashes.

    note that all commits still exist in the remote repository (origin). so if your aim was to remove a password from old commits then you need to remove the commits from the remote repository. also from all clones of the remote repository. see links below for more info.


    how to remove old commits (data loss warning! see notes below):

    to clear the reflog:

    git reflog expire --expire=all --all
    

    to remove all tags:

    git tag -l | xargs git tag -d
    

    then actually remove the commits from disk:

    git gc --prune=all
    

    now the old commits should be completely removed from disk.

    note 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 you need to backup them somehow.

    the reflog is cleared automatically after certain time (90 days?) by automatic git gc. tags however will stay around forever. so if you want to free disk space from old commits you have to at least remove the tags 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?

提交回复
热议问题