我可以在 reflog 中添加一行吗?

Can I add a line to the reflog?

由于一个拙劣的 git commit --fixup,我正处于 rebase 地狱之中。我想我已经确定了来源,而且我的处境比开始时更好。但是,如果我查看 git reflog,这一系列 'rebase -i' 行看起来就像我之前的拙劣尝试。

我可以将自己的行添加到 reflog 吗?说一些看起来像的东西:

$ 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

您可以随时使用 git 用于添加新的 reflog 条目的相同命令添加新的 reflog 条目,即 git update-ref。这是一个 "plumbing"(面向脚本的)命令,因此对用户来说不是很友好,您可能想要添加自己的小包装脚本或别名。

示例:

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

请注意 <ref>(第一个非选项参数)必须完整拼写。 <newvalue> 可以是解析为有效 SHA-1 的任何内容,这就是三个示例的中间命令可以工作的原因,但为了安全起见,最好使用第三种形式(重复 <ref> 准确)或使用实际的 SHA-1 哈希(第四种形式),让 git rev-parse 验证这实际上是一个有效的分支名称。

(使用 HEAD 时可以跳过验证,因为如果 HEAD 不是有效名称,git 根本无法运行。)

如本文所述2020 thread

There is the option core.logAllRefUpdates, which has the value "always" in more modern versions of Git.
The documentation says:

If the option is set to always, then a missing reflog is automatically created for any ref under refs/.

Now, that assumes that you want reflogs for all your refs, but there's really not much downside to having a reflog and not using it.

杰夫·金斯补充道:

The current rule is actually to append to any reflog that already exists, or to consult core.logAllRefUpdates when deciding whether to create one that doesn't exist. So I think setting a one-shot config variable like:

git -c core.logAllRefUpdates=always update-ref refs/foo/bar ...

would create the reflog, and then any subsequent updates (even without that config set) would append to it.

You can also do this:

mkdir -p .git/logs/refs/foo/
touch .git/logs/refs/foo/bar
git update-ref refs/foo/bar ...

but I wouldn't recommend it.
When we eventually move to supporting other ref backend formats, they won't necessarily store the logs in the same way.