git - 为什么我的 git log 输出在我通过管道传输时会被破坏?

标签 git shell pipe

如果我让 Git 使用其内置的 head 风格的日志截断,我得到的正是我所期望的:

$ git log --color=always --graph -5                                                                               

* 5abf1e7b0aba45e895bbe5b235f2326ad808ad30 (HEAD -> feature/blahblah, origin/feature/blahblah) blah blah last commit message
* c7b065e3d06ed8066ded283c2feec17e9f5a95d6 whitelist the new field
* 6b4332b951b2b37dec4c0fc67defb778dbdc29d7 add blah blah model
* 323c7570388f13aad8144292ebeb31187eb1c742 add blah blah to the database
* bceae36bda53b48679556eb00ee8321f1c8392e6 (origin/feature/yadayada, feature/yadayada) add blah blah to each blah blah

但是如果我使用 head,它会删除分支标记(这是我想要的!):

$  git log --color=always --graph | head -5                                                                        

* 5abf1e7b0aba45e895bbe5b235f2326ad808ad30 blah blah last commit message
* c7b065e3d06ed8066ded283c2feec17e9f5a95d6 whitelist the new field
* 6b4332b951b2b37dec4c0fc67defb778dbdc29d7 add blah blah model
* 323c7570388f13aad8144292ebeb31187eb1c742 add blah blah to the database
* bceae36bda53b48679556eb00ee8321f1c8392e6 add blah blah to each blah blah

当我将 git-log 的输出通过管道传输到另一个工具时,如何避免丢失这些分支标记?

(我正在尝试将输出通过管道传输到 fzf,为了按照我想要的方式获取内容,需要将其反转,但 Git 不允许 --graph --reverse 一起使用,所以我必须使用像 tac 这样的外部实用程序来反转输出(实际上是 gtac macOS),这导致我丢失了分支标记,这是我编写自己的函数而不是使用 fcs 示例 here 的三个主要原因之一。)

最佳答案

您具体指的是 (HEAD -> feature/foo) 装饰,它有时会出现,有时不会,以某​​种方式神奇地自动检测您是否通过其他程序通过管道传输 git log 输出。

这些装饰是在各种标志的控制下添加的,如 the git log manual page 中所述。 :

--decorate[=short|full|auto|no]

    Print out the ref names of any commits that are shown. If short is specified, the ref name prefixes refs/heads/, refs/tags/ and refs/remotes/ will not be printed. If full is specified, the full ref name (including prefix) will be printed. If auto is specified, then if the output is going to a terminal, the ref names are shown as if short were given, otherwise no ref names are shown. The default option is short.

(这里的粗体字是我的)

旧版本的 Git 没有那么花哨,一些旧版本默认为 no 而不是 autoshort,但选项有已经存在很长时间了。此外,您可以在每个用户的 git config --global 设置中配置您自己的默认设置。您可能拥有默认为 auto 的 Git 版本(我敢打赌这个版本),或者已将您的设置配置为 auto

关于git - 为什么我的 git log 输出在我通过管道传输时会被破坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49290946/

相关文章:

git - git cherry-pick 和 git show 有什么区别?补丁-p1?

git - 如果我是一个孤独的开发者,我有什么理由应该在 Git 的一个分支之外工作吗?

shell - ZSH 自动完成搞砸了命令名称

c - C Pipe 中的 Unix Shell 问题

bash - 使用 xargs 将标准输入分配给变量

使用不同的用户名/帐户进行 git 克隆

shell - bash 脚本自动搜索代码中的字符串(包括子文件夹)

bash - 如何重复启动和终止一个永无止境的 bash 进程

c - multi-pipe() C 程序中的无限循环

git - 最佳实践 git merge 具有许多更改的两个分支