Search code examples
gitgit-reflog

Can I add a line to the reflog?


I am in the middle of rebase hell due to a botched git commit --fixup. I think I've identified the source and I'm in a better place than I started. However, if I look at the git reflog, this sequence of 'rebase -i' lines looks just like my previous botched attempts.

Can I add my own line to the reflog? Say something that would look like:

$ git reflog mark '== we are not worse off than we started here =='
$ git reflog -3
cb6536f HEAD@{0}: mark: == we are not worse off than we started here ==
cb6536f HEAD@{1}: rebase -i (finish): fixup! foo: baz the widgets
9db07de HEAD@{1}: rebase -i (pick): fixup! baz: implement widget bazzing

Solution

  • You may add a new reflog entry at any time using the same command git uses to add new reflog entries, namely git update-ref. This is a "plumbing" (script-oriented) command so it is not very user friendly, and you might want to add your own little wrapper script or alias.

    Examples:

    git update-ref -m 'mark: whatever' HEAD HEAD
    git update-ref -m 'mark: another thing' refs/heads/branch branch
    git update-ref -m 'mark: third thing' refs/heads/branch refs/heads/branch
    hash=$(git rev-parse refs/heads/branch) && \
       git update-ref -m 'mark: 4' refs/heads/branch $hash
    

    Note that the <ref> (the first non-option argument) must be fully spelled out. The <newvalue> can be anything that resolves to a valid SHA-1, which is why the middle command of the three examples can work, but for safety it is probably best to use the third form (repeat the <ref> exactly) or use an actual SHA-1 hash (fourth form), letting git rev-parse verify that this is in fact a valid branch name.

    (When using HEAD you can skip validation since git cannot function at all if HEAD is not a valid name.)