在 Git 中可视化分支拓扑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1838873/
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
Visualizing branch topology in Git
提问by Benjol
I'm playing with Git in isolation on my own machine, and I find it difficult to maintain a mental model of all my branches and commits. I know I can do a git log
to see the commit history from where I am, but is there a way to see the entire branch topography, something like these ASCII maps that seem to be used everywhere for explaining branches?
我在自己的机器上孤立地使用 Git,我发现很难维护我所有分支和提交的心智模型。我知道我可以git log
从我所在的位置查看提交历史记录,但是有没有办法查看整个分支地形,比如这些 ASCII 映射似乎无处不在用于解释分支?
.-A---M---N---O---P
/ / / / /
I B C D E
\ / / / /
`-------------'
It just feels like someone coming along and trying to pick up my repository would have difficulty working out exactly what was going on.
感觉就像有人过来并试图拿起我的存储库会很难弄清楚到底发生了什么。
I guess I'm influenced by AccuRev's stream browser...
我想我受到了 AccuRev 的流浏览器的影响......
回答by jrockway
Use git log --graph
or gitk
. (Both also accept --all
, which will show all the branches instead of just the current one.)
使用git log --graph
或gitk
。(两者都接受--all
,这将显示所有分支,而不仅仅是当前分支。)
For branch names and a compact view, try:
对于分支名称和紧凑视图,请尝试:
git log --graph --decorate --oneline
回答by P Shved
I usually use
我通常使用
git log --graph --full-history --all --pretty=format:"%h%x09%d%x20%s"
With colors (if your shell is Bash):
使用颜色(如果您的外壳是 Bash):
git log --graph --full-history --all --color \
--pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
This will print text-based representation like this:
这将打印基于文本的表示,如下所示:
* 040cc7c (HEAD, master) Manual is NOT built by default
* a29ceb7 Removed offensive binary file that was compiled on my machine and was hence incompatible with other machines.
| * 901c7dd (cvc3) cvc3 now configured before building
| * d9e8b5e More sane Yices SMT solver caller
| | * 5b98a10 (nullvars) All uninitialized variables get zero inits
| |/
| * 1cad874 CFLAGS for cvc3 to work successfully
| * 1579581 Merge branch 'llvm-inv' into cvc3
| |\
| | * a9a246b nostaticalias option
| | * 73b91cc Comment about aliases.
| | * 001b20a Prints number of iteration and node.
| |/
|/|
| * 39d2638 Included header files to cvc3 sources
| * 266023b Added cvc3 to blast infrastructure.
| * ac9eb10 Initial sources of cvc3-1.5
|/
* d642f88 Option -aliasstat, by default stats are suppressed
(You could just use git log --format=oneline
, but it will tie commit messages to numbers, which looks less pretty IMHO).
(您可以只使用git log --format=oneline
,但它会将提交消息与数字联系起来,恕我直言,这看起来不那么漂亮)。
To make a shortcut for this command, you may want to edit your ~/.gitconfig
file:
要为此命令创建快捷方式,您可能需要编辑~/.gitconfig
文件:
[alias]
gr = log --graph --full-history --all --color --pretty=tformat:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s%x20%x1b[33m(%an)%x1b[0m"
However, as Sodel the Vociferousnotes in the comments, such long formatting command is hard to memorize. Usually, it's not a problem as you may put it into the ~/.gitconfig
file. However, if you sometimes have to log in to a remote machine where you can't modify the config file, you could use a more simple but faster to type version:
但是,正如Sodel the Vociferous在评论中所指出的那样,如此长的格式化命令很难记住。通常,这不是问题,因为您可以将其放入~/.gitconfig
文件中。但是,如果您有时必须登录到无法修改配置文件的远程计算机,则可以使用更简单但速度更快的版本输入:
git log --graph --oneline
回答by Slipp D. Thompson
I have 3 aliases (and 4 alias-aliases for quick usage)that I normally throw in my ~/.gitconfig
file:
我有 3 个别名(和 4 个别名以供快速使用),我通常将它们放入我的~/.gitconfig
文件中:
[alias]
lg = lg1
lg1 = lg1-specific --all
lg2 = lg2-specific --all
lg3 = lg3-specific --all
lg1-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)'
lg2-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'
lg3-specific = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset) %C(bold cyan)(committed: %cD)%C(reset) %C(auto)%d%C(reset)%n'' %C(white)%s%C(reset)%n'' %C(dim white)- %an <%ae> %C(reset) %C(dim white)(committer: %cn <%ce>)%C(reset)'
git lg
/git lg1
looks like this:
git lg
/git lg1
看起来像这样:
git lg2
looks like this:
git lg2
看起来像这样:
and git lg3
looks like this:
和git lg3
看起来像这样:
It should be noted that this isn't meant as a end-all-be-all solution— it's a template for you to change, add to and fix up to your liking. If you want to use these, my recommendation is to:
应该注意的是,这并不是一个最终的解决方案——它是一个模板,您可以根据自己的喜好进行更改、添加和修复。如果你想使用这些,我的建议是:
- Add them to your
.gitconfig
, - Customize to your liking (different color choices, different line arrangements for the 2- and 3-line versions, etc.),
- And then save a copy to a Gist or other code snippet tool so you can copy & paste it into
.gitconfig
s in the future (or alternatively version control your dotfiles, of course).
- 将它们添加到您的
.gitconfig
, - 根据您的喜好定制(不同的颜色选择,2 线和 3 线版本的不同线路安排等),
- 然后将副本保存到 Gist 或其他代码片段工具,以便您
.gitconfig
将来可以将其复制并粘贴到s 中(当然,也可以选择版本控制您的点文件)。
Note: Answer copied from and improved upon the answer at stackoverflow.com/questions/1057564/pretty-git-branch-graphssince it's far more appropriate here than it was there.??Left the copy on the other question for historical reasons— it's closed now, and the answer's referenced by a bunch of other answers.
注意:答案是从stackoverflow.com/questions/1057564/pretty-git-branch-graphs上的答案复制并改进的,因为它在这里比在那里更合适。??由于历史原因,将副本留在另一个问题上-它现在已关闭,并且该答案已被许多其他答案引用。
回答by Andrew
To any of these recipes (based on git log or gitk), you can add --simplify-by-decoration
to collapse the uninteresting linear parts of the history. This makes much more of the topology visible at once. I can now understand large histories that would be incomprehensible without this option!
对于这些食谱中的任何一个(基于 git log 或 gitk),您都可以添加--simplify-by-decoration
折叠历史记录中无趣的线性部分。这使得更多的拓扑结构一次可见。我现在可以理解如果没有这个选项就无法理解的大历史!
I felt the need to post this because it doesn't seem to be as well-known as it should be. It doesn't appear in most of the Stack Overflow questions about visualizing history, and it took me quite a bit of searching to find--even after I knew I wanted it! I finally found it in this Debian bug report. The first mention on Stack Overflow seems to be this answerby Antoine Pelisse.
我觉得有必要发布这个,因为它似乎不像它应该的那样广为人知。它没有出现在大多数关于可视化历史的 Stack Overflow 问题中,我花了很多时间才找到——即使在我知道我想要它之后!我终于在这个Debian 错误报告中找到了它。第一次提到 Stack Overflow 似乎是Antoine Pelisse 的这个答案。
回答by checksum
Gitk
sometime painful for me to read.
Gitk
有时我读起来很痛苦。
Motivate me to write GitVersionTree.
激励我写GitVersionTree。
回答by Yeo
99.999% of my time is looking at history by git lg
and the 0.001% is by git log
.
我 99.999% 的时间都在看历史,git lg
而 0.001% 是在git log
.
I just want to share two log aliases that might be useful (configure from .gitconfig):
我只想分享两个可能有用的日志别名(从 .gitconfig 配置):
[Alias]
lg = log --graph --pretty=format:'%Cred%h%Creset %ad %s %C(yellow)%d%Creset %C(bold blue)<%an>%Creset' --date=short
hist = log --graph --full-history --all --pretty=format:'%Cred%h%Creset %ad %s %C(yellow)%d%Creset %C(bold blue)<%an>%Creset' --date=short
git lg
will see the current branch history.git hist
will see the whole branch history.
git lg
将看到当前的分支历史。git hist
将看到整个分支历史。
回答by pylang
Take a look at Gitkraken- a cross-platform GUI that shows topology in a lucid way.
看看Gitkraken- 一个跨平台的 GUI,它以清晰的方式显示拓扑。
Here's a quick video tutorialon some advanced features.
这是有关一些高级功能的快速视频教程。
回答by VonC
I like, with git log
, to do:
我喜欢用git log
, 做:
git log --graph --oneline --branches
(also with --all, for viewing remote branches as well)
(还有--all,也用于查看远程分支)
Works with recent Git releases: introduced since 1.6.3(Thu, 7 May 2009)
适用于最近的 Git 版本:从1.6.3 开始引入(2009 年 5 月 7 日星期四)
"
--pretty=<style>
" option to the log family of commands can now be spelled as "--format=<style>
".
In addition,--format=%formatstring
is a short-hand for--pretty=tformat:%formatstring
."
--oneline
" is a synonym for "--pretty=oneline --abbrev-commit
".
--pretty=<style>
日志命令系列的“ ”选项现在可以拼写为“--format=<style>
”。
此外,--format=%formatstring
是 的简写--pretty=tformat:%formatstring
。“
--oneline
”是“ ”的同义词--pretty=oneline --abbrev-commit
。
PS D:\git\tests\finalRepo> git log --graph --oneline --branches --all
* 4919b68 a second bug10 fix
* 3469e13 a first bug10 fix
* dbcc7aa a first legacy evolution
| * 55aac85 another main evol
| | * 47e6ee1 a second bug10 fix
| | * 8183707 a first bug10 fix
| |/
| * e727105 a second evol for 2.0
| * 473d44e a main evol
|/
* b68c1f5 first evol, for making 1.0
You can also limit the span of the log display (number of commits):
您还可以限制日志显示的跨度(提交次数):
PS D:\git\tests\finalRepo> git log --graph --oneline --branches --all -5
* 4919b68 a second bug10 fix
* 3469e13 a first bug10 fix
* dbcc7aa a first legacy evolution
| * 55aac85 another main evol
| | * 47e6ee1 a second bug10 fix
(show only the last 5 commits)
(仅显示最近 5 次提交)
What I do not like about the current selected solution is:
我不喜欢当前选择的解决方案的是:
git log --graph
It displayed way too much info (when I want only to look at a quick summary):
它显示了太多信息(当我只想查看快速摘要时):
PS D:\git\tests\finalRepo> git log --graph
* commit 4919b681db93df82ead7ba6190eca6a49a9d82e7
| Author: VonC <[email protected]>
| Date: Sat Nov 14 13:42:20 2009 +0100
|
| a second bug10 fix
|
* commit 3469e13f8d0fadeac5fcb6f388aca69497fd08a9
| Author: VonC <[email protected]>
| Date: Sat Nov 14 13:41:50 2009 +0100
|
| a first bug10 fix
|
gitk
is great, but forces me to leave the shell session for another window, whereas displaying the last n commits quickly is often enough.
gitk
很好,但迫使我离开 shell 会话到另一个窗口,而快速显示最后 n 次提交通常就足够了。