Search code examples
gitgit-reflog

can you back up git reflog? Advice on best practice to guard against code loss


git reflog shows all activity, even squashed commits, etc. (correct me if I am wrong).

Is there a git CLI command to backup git reflog?

Obviously if I delete the local version of a repo I have lost my reflog (correct me if I am wrong).

I wondered if I can git push it for safety or something like that?

If not what approach should I take to prevent loss of reflog?

Update: in response to Jiri Kremser, if I backup .git/logs can I git reset --hard to any commit in the reflog after I have restored it?


Solution

  • git reflog shows all activity (even squashed commits, etc). (correct me if i am wrong)

    As you figured out git reflog store almost all the activities in git.
    Almost anything means that it does not actually store all the activities its store all the activities which modified your HEAD locally.

    The important thing it that is store only local data so it will be useless to backup and restore it.

    Why cant i backup reflog?

    Since reflog is only store relevant information of your local repository it will not work if you restore it into a different repo.

    for example consider this (very simple) local flow:

    # checkout master branch
    git checkout master
    
    # do some changes and commit 
    - At this point there will be a new entry in the reflog
    
    # now you decide to discard your changes
    git reset HEAD~1 --hard
    - At this point a new entry is added to your reflog
    - The commit which you made is a dangling commit which can be 
      recovered on your local machine but does not exist on any other
      repository beside yours.
    

    This is a very simple flow but as you can understand from this flow that even if you can backup the reflog (and you can simply backup .git/logs) it will be useless.

    There are many other cases like this one which will make your reflog useless.

    This is the reason why its locally for your machine and not for any other machine.

    Assume that now you have my backup of the reflog, its useless for you if i have executed rebase, filter-branch etc.

    enter image description here