我可以向 git 提交添加元数据吗?或者我可以在 gitk 中隐藏一些标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2683248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:14:56  来源:igfitidea点击:

Can I add metadata to git commits? Or can I hide some tags in gitk

gittagsmetadatagitk

提问by Chris Nelson

I want to associate custom metadata with a git commit. Specifically to record a review ID from a code review but it could be anything. Tags seem a natural way to do that but I expect to have a review for every commit and I don't want to clutter gitkwith tons of tags. Is there some other mechanism to add custom metadata? Can I make certain tags invisible? If I could tell gitknot to display tags matching some pattern or RE, that would likely work but I don't see a way to do that.

我想将自定义元数据与git commit. 特别是从代码中记录 ID,但它可以是任何东西。标签似乎是一种自然的方式来做到这一点,但我希望对每次提交进行,我不想被gitk大量的标签弄得乱七八糟。是否有其他机制可以添加自定义元数据?我可以让某些标签不可见吗?如果我可以告诉gitk不显示与某些模式或 RE 匹配的标签,那可能会起作用,但我看不到这样做的方法。

采纳答案by J?rg W Mittag

That's precisely what git notesare for.

这正是git notes的用途。

回答by Guildenstern

Git-notes

Git笔记

With git notesyou can add a “note” to a commit. You can also add them to other Git objects, but let's just focus on commits since that is what the question is about.

有了git notes你可以添加一个“注”的承诺。您也可以将它们添加到其他 Git 对象,但让我们只关注提交,因为这就是问题所在。

A note is a Git object, and can in principle be “whatever” (arbitrary data). But we'll focus on something simple and textual for our purposes.

笔记是一个 Git 对象,原则上可以是“任何”(任意数据)。但出于我们的目的,我们将专注于一些简单和文本化的内容。

Example: review id

示例:评论 ID

The question mentions review ids, so let's make up some way to represent such a thing. I don't know what review ids really look like, but hopefully the following would be sensible:

问题提到了review id,所以让我们用某种方式来表示这样的事情。我不知道评论 ID 到底是什么样子,但希望以下内容是明智的:

Review-id: 42

So this is effectively a key-value pair. Let's add the above string to the current commit:

所以这实际上是一个键值对。让我们将上述字符串添加到当前提交中:

git notes add -m "Review-id: 42"

If you run git logthe note will be shown inline:(?1)

如果您运行git log该注释将内联显示:(?1)

Author: Victor Version Control <[email protected]>
Date:   Tue Nov 8 21:10:25 2016 +0100

    Implement feature x

Notes:
    Review-id: 42

Another example

另一个例子

Of course you can add more “subnotes” to this note (we will stick with the simple key: valuesyntax, one value per line). For example, if you found out three months later that the commit message got something wrong, just append the correction to the note:

当然,您可以在此注释中添加更多“子注释”(我们将坚持使用简单的key: value语法,每行一个值)。例如,如果您在三个月后发现提交消息有问题,只需将更正附加到注释中:

git notes append -m "Errata: It was actually feature y."

git log:

git log

Author: Victor Version Control <[email protected]>
Date:   Tue Nov 8 21:10:25 2016 +0100

    Implement feature x

Notes:
    Review-id: 42

    Errata: It was actually feature y.

We use git notes appendin order to easily add this extra data to the note. You could also use git notes editin order to edit the file directly.

我们使用git notes append是为了轻松地将这些额外数据添加到注释中。您也可以使用git notes edit来直接编辑文件。

Of course, since a Git note is just a single mutable file, you can run into merge conflicts. To make that less likely, you can:

当然,由于 Git 笔记只是一个单一的可变文件,您可能会遇到合并冲突。为了降低这种可能性,您可以:

  1. Stick to simple data like the above (one key-value per line).
  2. Use special merge strategies; see man git-notes, section “Notes merge strategies”.
  1. 坚持像上面这样的简单数据(每行一个键值)。
  2. 使用特殊的合并策略;请参阅man git-notes,“Notes 合并策略”部分。

Visibility

能见度

The OP asked:

OP问:

> Can I make certain tags invisible?

> 我可以让某些标签不可见吗?

By default, git logonly shows one note, namely .git/refs/notes/commits. commitsis just one note in the namespace. Maybe you want issuesto be in their own namespace:

默认情况下,git log只显示一个音符,即 .git/refs/notes/commitscommits只是命名空间中的一个注释。也许您希望问题在它们自己的命名空间中:

git notes --ref=issues add -m "Fixes: #32"

Since this is stored in .git/refs/notes/issuesand not in .git/refs/notes/commits, “Fixes: #32” won't show up when you run git log. So you have effectively made such notes invisible by default.

由于它存储在 中.git/refs/notes/issues而不是在 中 .git/refs/notes/commits,因此运行时不会显示“Fixes: #32” git log。因此,默认情况下,您已有效地使此类注释不可见。

If you want it to be shown, pass --notes=issuesto git log:

如果要显示它,请传递--notes=issuesgit log

$ git log --notes=issues
Author: Victor Version Control <[email protected]>
Date:   Tue Nov 8 21:10:25 2016 +0100

    Implement feature x

Notes (issues):
    Fixes: #32

But now .git/refs/notes/commitsare hidden. That one can easily be included as well:

但现在.git/refs/notes/commits都隐藏了。那一个也可以很容易地包括在内:

$ git log --notes=issues --notes=commits
Author: Victor Version Control <[email protected]>
Date:   Tue Nov 8 21:10:25 2016 +0100

    Implement feature x

Notes (issues):
    Fixes: #32

Notes:
    Review-id: 42

    Errata: It was actually feature y.

There are variables to configure which notes are shown by default; see man git-config.

有一些变量可以配置默认显示哪些音符;见 man git-config

Benefits compared to commit messages

与提交消息相比的好处

Metadata can of course be recorded in the commit message directly.(?2) But commit messages are immutable, so to change them really means to make a whole new commit, with all the rippling consequences that that entails. Git-notes on the other hand are mutable, so you are always able to revise them. And each modification of a note is of course version controlled. In our case, for .git/refs/notes/commits:

元数据当然可以直接记录在提交消息中。(?2) 但是提交消息是不可变的,所以改变它们实际上意味着进行一个全新的提交,以及随之而来的所有涟漪后果。另一方面,Git-notes 是可变的,所以你总是可以修改它们。注释的每次修改当然都是受版本控制的。在我们的例子中,对于.git/refs/notes/commits

$ git log refs/notes/commits
Author: Victor Version Control <[email protected]>
commit 9f0697c6bbbc6a97ecce9834d4c9afa0d668bcad
Date:   Tue Nov 8 21:13:52 2016 +0100

    Notes added by 'git notes append'

commit b60997e49444732ed2defc8a6ca88e9e21001a1d
Author: Victor Version Control <[email protected]>
Date:   Tue Nov 8 21:10:38 2016 +0100

    Notes added by 'git notes add'

Sharing notes

分享笔记

Your notes aren't shared by default; you have to do so explicitly. And compared to other refs, sharing notes isn't very user-friendly. We have to use the refspecsyntax:

默认情况下,您的笔记不会共享;你必须明确地这样做。与其他参考文献相比,共享笔记不是很人性化。我们必须使用refspec语法:

git push refs/notes/*

The above will push all of your notes to your remote.

以上将把你所有的笔记推送到你的遥控器。

It seems that fetching notes is a bit more involved; you can do it if you specify both sides of the refspec:

取笔记似乎更复杂一些;如果您指定 refspec 的两侧,则可以执行此操作:

git fetch origin refs/notes/*:refs/notes/*

So that's definitely not convenient. If you intend to use Git-notes regularly, you'll probably want to set up your gitconfig to always fetch notes:

所以肯定不方便。如果您打算定期使用 Git-notes,您可能希望将 gitconfig 设置为始终获取笔记:

[remote "origin"]
    …
    fetch = +refs/notes/*:refs/notes/*

Carry over notes on rewrites

关于重写的结转笔记

Git has the inconvenient default that notes are not carried over when a commit is rewritten. So if you for example rebase a series of commits, the notes will not carry over to the new commits.

Git 有一个不方便的默认设置,即在重写提交时不会保留注释。因此,例如,如果您对一系列提交进行变基,则注释将不会转移到新的提交中。

The variable notes.rewrite.<command>is by default set to true, so one might assume that notes arecarried over. But the problem is that the variable notes.rewriteRef, which determines whichnotes will be carried over, has no deafult vaule. To set this value to match all notes, execute the following:

该变量notes.rewrite.<command>默认设置为true,因此人们可能会假设音符结转。但问题是,notes.rewriteRef决定哪些音符将被结转的变量 没有默认值。要将此值设置为匹配所有音符,请执行以下操作:

git config --global notes.rewriteRef "refs/notes/*"

Now all notes will be carried over when doing rewrite operations like git rebase.

现在,在进行诸如git rebase.

Carry over notes through email patches

通过电子邮件补丁结转笔记

If you are using git format-patchto format your changes to be sent as emails, and you have some metadata stored as Git notes, you can pass the --notesoption to git format-patchin order to append the notes to the email draft.

如果您正在使用git format-patch格式将更改作为电子邮件发送,并且您将一些元数据存储为 Git 注释,则可以传递--notes选项git format-patch以将注释附加到电子邮件草稿中。



? 1: “This is the default for git log[…] when there is no --pretty, --format, or --onelineoption given on the command line.” ― man git-log, git version 2.10.2

? 1:“这是默认的git log[...]当没有--pretty--format或者--oneline在命令行上给出的选项。” ― man git-log, git 版本 2.10.2

? 2: One practice/convention for metadata-in-commit-messages that is used in projects like e.g. Git and the Linux kernel is to add key–value pairs in the “trailer” of the commit message, i.e. at the bottom. See for example this commitby Linus Torvalds:

? 2:在诸如 Git 和 Linux 内核之类的项目中使用的元数据提交消息的一种实践/约定是在提交消息的“尾部”中添加键值对,即在底部。例如,请参阅Linus Torvalds 的这个提交

 mm: remove gup_flags FOLL_WRITE games from __get_user_pages()
 This is an ancient bug that was actually attempted to be fixed once
 (badly) by me eleven years ago in commit 4ceb5db9757a ("Fix
 get_user_pages() race for write access") but that was then undone due to
 problems on s390 by commit f33ea7f404e5 ("fix get_user_pages bug").

 In the meantime, the s390 situation has long been fixed, and we can now
 fix it by checking the pte_dirty() bit properly (and do it better).  The
 s390 dirty bit was implemented in abf09bed3cce ("s390/mm: implement
 software dirty bits") which made it into v3.9.  Earlier kernels will
 have to look at the page state itself.

 Also, the VM has become more scalable, and what used a purely
 theoretical race back then has become easier to trigger.

 To fix it, we introduce a new internal FOLL_COW flag to mark the "yes,
 we already did a COW" rather than play racy games with FOLL_WRITE that
 is very fundamental, and then use the pte dirty flag to validate that
 the FOLL_COW flag is still valid.

 Reported-and-tested-by: Phil "not Paul" Oester <[email protected]>
 Acked-by: Hugh Dickins <[email protected]>
 Reviewed-by: Michal Hocko <[email protected]>
 Cc: Andy Lutomirski <[email protected]>
 Cc: Kees Cook <[email protected]>
 Cc: Oleg Nesterov <[email protected]>
 Cc: Willy Tarreau <[email protected]>
 Cc: Nick Piggin <[email protected]>
 Cc: Greg Thelen <[email protected]>
 Cc: [email protected]
 Signed-off-by: Linus Torvalds <[email protected]>

See:

看:

  • man git-interpret-trailers
  • This Kernel Wiki pagewhich lists various trailer lines (usually key–value pairs) that are used in various projects.
  • man git-interpret-trailers
  • 这个内核 Wiki 页面列出了在各种项目中使用的各种预告片(通常是键值对)。