git 命令行技巧 --no-pager 添加别名

2025-06-10

git 命令行技巧

--no-pager

添加别名

我个人更喜欢命令行而不是工具。原因如下:

  1. 命令行始终可用;
  2. 如果你可以通过命令行完成,那么你就可以在任何地方完成
  3. 使用命令行让我看起来很怪异

我写这篇文章的原因是我厌倦了输入git log --one-line -10显示最后 10 次提交的内容,并且还必须q在之后输入,而我需要的信息却消失了,因为我想创建一个与其余提交一致的提交消息。

因此我决定研究如何让我能够更愉快地从 git 快速获取一些历史信息。

我学到的第一件事是,大多数 git 命令最终都进入了所谓的“分页器”;

git log --oneline -10

e3aa791 (HEAD -> master) notes
81affbd docs: translated installation guide and moved errors to bottom
9a592cd docs: nearly there
aaa98fe docs: add stuff from the thing
c522b66 docs: generic docs
e09ee2b chore(Doc) sort
8740d11 chore(doc): add new stuff
bd8e74c notes after meeting
0de07df chore(Base): initial checkin
(END)

我发现可以绕过分页器,以便 gitcat通过添加--no-pager作为第一个参数来使用它。它只能作为第一个参数使用。

--no-pager

git --no-pager log --oneline -10

➜  notes git:(master) git --no-pager log --oneline -10
e3aa791 (HEAD -> master) notes
81affbd docs: translated installation guide and moved errors to bottom
9a592cd docs: nearly there
aaa98fe docs: add kostenscherm
c522b66 docs: generic docs
e09ee2b chore(Doc) sort
8740d11 chore(doc): add new stuff
bd8e74c notes after meeting
0de07df chore(Base): initial checkin
➜  notes git:(master) 

注意我恢复了 shell,信息仍然在屏幕上。现在我可以像其他信息一样,写一条 git 提交信息了。

所以这解决了我部分的问题。我不想一遍又一遍地输入上面的整个命令。在别人的机器上工作时记住它当然好,但这次我想“保存”它。

好消息是,你可以。每个命令,你可以切换是否应该使用分页器。

在这种情况下,您可以输入:

git config --global pager.log false

➜  notes git:(master) git config --global pager.log false
➜  notes git:(master) git log -1
commit e3aa791e0e970023734988c8aeb736ecda6099f4 (HEAD -> master)
Author: Remi Kristelijn <remi.kristelijn@myawesomecompany.com>
Date:   Fri Nov 29 15:51:02 2019 +0100

    notes
➜  notes git:(master) 

这些设置存储在~/.gitconfig

我还知道通过组合--pretty--graph您可以装饰您的日志消息并将其保存为别名。

添加别名

我使用的命令是log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -10

➜  git:(master) git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -10
*   d2a1b98 - (HEAD -> master, origin/master) Merge branch 'feat/wizard-steps' into 'master' (3 weeks ago) <Remi Kristelijn>
|\  
| * 20b7b2e - (origin/feat/wizard-steps, feat/wizard-steps) feat:step 2 of wizard (3 weeks ago) <Remi Kristelijn>
| * 077f5e1 - feat: wizard placeholder (3 weeks ago) <Remi Kristelijn>
| * e2f4c29 - feat: add readme.md as an example (3 weeks ago) <Remi Kristelijn>
| * debbfcd - fix: hyperlink goes outside of context (3 weeks ago) <Remi Kristelijn>
| * af11d21 - feat:allow *.md imports for Typescript (3 weeks ago) <Remi Kristelijn>
|/  
*   cb562e3 - Merge branch 'feat/style' into 'master' (3 weeks ago) <Remi Kristelijn>
|\  
| *   51df26c - (origin/feat/style, feat/style) fix:merge (3 weeks ago) <Remi Kristelijn>
| |\  
| |/  
|/|   
* |   3583c92 - Merge branch 'feat/docs' into 'master' (3 weeks ago) <Remi Kristelijn>
|\ \  
| * | ba0f827 - (origin/feat/docs) docs:add loglevel (3 weeks ago) <Remi Kristelijn>%  

要将此命令保存为别名,假设lg

git config --global alias.lg log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' -10

然后你可以标记这个命令,使其不使用“pager”

git config --global pager.lg false

但是因为对于分页器来说,日志已经被标记为 false,所以在使用 lg 时它会继续执行该操作。

鏂囩珷鏉ユ簮锛�https://dev.to/rkristelijn/git-command-line-tricks-24e8
PREV
使用 React Lazy 和 React Suspense 进行代码拆分 React 路由器 GenAI LIVE!| 2025 年 6 月 4 日
NEXT
每个程序员都应该学习的十大系统设计概念