3

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
danday74
  • 52,471
  • 49
  • 232
  • 283

4 Answers4

5

Yes and no. There is no command for it, but you can make the backup of .git/logs where the reflog is stored.

However, only the references to commits are stored there, so it's like making backup of symbolic links (or shortcuts in Windows). It may work, depending on what you want to do.

In general, it's not a good idea to make the reflog public, because it was meant to be for local repo only.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
1

You can think of the reflog kind of like an audit trail for all of the things you've done with your Git repository. It's immediately useful if you need to go back and restore something that was lost via some process, but backing it up in the long run won't accomplish all that much.

The more valuable thing to back up would be the actual repository. Losing those actual, concrete changes to your Git repository would absolutely cause data loss, whereas losing the reflog would generally mean you can't look at the list of commands and figure out where a rebase started. While that's infinitely useful when you've got a stable environment, if you need to restore it for any reason, you genuinely should care more about the overall repository.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Since reflog is only pointers to the commits, not the actual commit data, as soon as your commits are old enough and git runs git gc (runs silently under the covers on most things), the actual commits are still gone. Rather than backing up reflog, you may want to just litter your code with branch labels. Those will not only preserve the history, it'll also preserve the actual commits. Once you're satisfied with your squash / rebase / other destructive things, delete these old branches, and git gc will happily collect the old data again.

robrich
  • 13,017
  • 7
  • 36
  • 63
0

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

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167