如何对 git 分支输出进行版本排序(与通常的 alphabetical/lexicographic 排序相比)
How to version-sort git branch output (vs the usual alphabetical/lexicographic sorting)
我使用git branch -a 来显示分支。
我假设 git 分支 -a 没有按字母顺序排序。
需要 git branch -a
以数字排序,例如 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
而不是 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9
考虑一下,我无法重命名分支,例如将前缀 0
重命名为 Part-02
作为解决方法。
我对第三方命令行 git 客户端开放,也是最后的手段。
正在做 git branch -a
当前输出:
remotes/origin/Part-10_LoadStateListener
remotes/origin/Part-11_Navigating-to-the-Detail-Screen
remotes/origin/Part-12_Implementing-the-DetailsFragment
remotes/origin/Part-13_Handling-Process-Death
remotes/origin/Part-1_Project-Setup
remotes/origin/Part-2_Layouts-&-Model-Class
remotes/origin/Part-3_Navigation-Component
remotes/origin/Part-4_API-Interface
remotes/origin/Part-5_Dependency-Injection-with-Hilt
remotes/origin/Part-6_PagingSource-&-PagingData
remotes/origin/Part-7_PagingDataAdapter
remotes/origin/Part-8_Header-&-Footer
remotes/origin/Part-9_Search-Functionality
预期输出:
remotes/origin/Part-1_Project-Setup
remotes/origin/Part-2_Layouts-&-Model-Class
remotes/origin/Part-3_Navigation-Component
remotes/origin/Part-4_API-Interface
remotes/origin/Part-5_Dependency-Injection-with-Hilt
remotes/origin/Part-6_PagingSource-&-PagingData
remotes/origin/Part-7_PagingDataAdapter
remotes/origin/Part-8_Header-&-Footer
remotes/origin/Part-9_Search-Functionality
remotes/origin/Part-10_LoadStateListener
remotes/origin/Part-11_Navigating-to-the-Detail-Screen
remotes/origin/Part-12_Implementing-the-DetailsFragment
remotes/origin/Part-13_Handling-Process-Death
我希望有一个像 --numeric-sort
这样的标志,这样我就可以使用 git branch -a --numeric-sort
git 本身不支持像这样的命令的丰富排序选项(编辑:事实证明这不再是真的,详情请参阅 Hasturkun 的回答),但感谢 shell 您可以轻松地使用外部程序为您对输出进行排序。
而且都是 GNU sort and BSD sort provide the -V
option which works similarly to natural sort order,所以类似
git branch -a | sort -V
应该给你这样的输出:
remotes/origin/Part-1_Project-Setup
remotes/origin/Part-2_Layouts-&-Model-Class
remotes/origin/Part-3_Navigation-Component
remotes/origin/Part-4_API-Interface
remotes/origin/Part-5_Dependency-Injection-with-Hilt
remotes/origin/Part-6_PagingSource-&-PagingData
remotes/origin/Part-7_PagingDataAdapter
remotes/origin/Part-8_Header-&-Footer
remotes/origin/Part-9_Search-Functionality
remotes/origin/Part-10_LoadStateListener
remotes/origin/Part-11_Navigating-to-the-Detail-Screen
remotes/origin/Part-12_Implementing-the-DetailsFragment
remotes/origin/Part-13_Handling-Process-Death
您可以要求 git 为您对引用名进行版本排序(从 git 2.7.0 开始):
git branch -a --sort=v:refname
remotes/origin/foo_1_bar
remotes/origin/foo_2_bar
remotes/origin/foo_3_bar
remotes/origin/foo_4_bar
remotes/origin/foo_5_bar
remotes/origin/foo_6_bar
remotes/origin/foo_7_bar
remotes/origin/foo_8_bar
remotes/origin/foo_9_bar
remotes/origin/foo_10_bar
remotes/origin/foo_11_bar
对比:
git branch -a
remotes/origin/foo_10_bar
remotes/origin/foo_11_bar
remotes/origin/foo_1_bar
remotes/origin/foo_2_bar
remotes/origin/foo_3_bar
remotes/origin/foo_4_bar
remotes/origin/foo_5_bar
remotes/origin/foo_6_bar
remotes/origin/foo_7_bar
remotes/origin/foo_8_bar
remotes/origin/foo_9_bar
我使用git branch -a 来显示分支。
我假设 git 分支 -a 没有按字母顺序排序。
需要 git branch -a
以数字排序,例如 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
而不是 10, 11, 1, 2, 3, 4, 5, 6, 7, 8, 9
考虑一下,我无法重命名分支,例如将前缀 0
重命名为 Part-02
作为解决方法。
我对第三方命令行 git 客户端开放,也是最后的手段。
正在做 git branch -a
当前输出:
remotes/origin/Part-10_LoadStateListener
remotes/origin/Part-11_Navigating-to-the-Detail-Screen
remotes/origin/Part-12_Implementing-the-DetailsFragment
remotes/origin/Part-13_Handling-Process-Death
remotes/origin/Part-1_Project-Setup
remotes/origin/Part-2_Layouts-&-Model-Class
remotes/origin/Part-3_Navigation-Component
remotes/origin/Part-4_API-Interface
remotes/origin/Part-5_Dependency-Injection-with-Hilt
remotes/origin/Part-6_PagingSource-&-PagingData
remotes/origin/Part-7_PagingDataAdapter
remotes/origin/Part-8_Header-&-Footer
remotes/origin/Part-9_Search-Functionality
预期输出:
remotes/origin/Part-1_Project-Setup
remotes/origin/Part-2_Layouts-&-Model-Class
remotes/origin/Part-3_Navigation-Component
remotes/origin/Part-4_API-Interface
remotes/origin/Part-5_Dependency-Injection-with-Hilt
remotes/origin/Part-6_PagingSource-&-PagingData
remotes/origin/Part-7_PagingDataAdapter
remotes/origin/Part-8_Header-&-Footer
remotes/origin/Part-9_Search-Functionality
remotes/origin/Part-10_LoadStateListener
remotes/origin/Part-11_Navigating-to-the-Detail-Screen
remotes/origin/Part-12_Implementing-the-DetailsFragment
remotes/origin/Part-13_Handling-Process-Death
我希望有一个像 --numeric-sort
这样的标志,这样我就可以使用 git branch -a --numeric-sort
git 本身不支持像这样的命令的丰富排序选项(编辑:事实证明这不再是真的,详情请参阅 Hasturkun 的回答),但感谢 shell 您可以轻松地使用外部程序为您对输出进行排序。
而且都是 GNU sort and BSD sort provide the -V
option which works similarly to natural sort order,所以类似
git branch -a | sort -V
应该给你这样的输出:
remotes/origin/Part-1_Project-Setup
remotes/origin/Part-2_Layouts-&-Model-Class
remotes/origin/Part-3_Navigation-Component
remotes/origin/Part-4_API-Interface
remotes/origin/Part-5_Dependency-Injection-with-Hilt
remotes/origin/Part-6_PagingSource-&-PagingData
remotes/origin/Part-7_PagingDataAdapter
remotes/origin/Part-8_Header-&-Footer
remotes/origin/Part-9_Search-Functionality
remotes/origin/Part-10_LoadStateListener
remotes/origin/Part-11_Navigating-to-the-Detail-Screen
remotes/origin/Part-12_Implementing-the-DetailsFragment
remotes/origin/Part-13_Handling-Process-Death
您可以要求 git 为您对引用名进行版本排序(从 git 2.7.0 开始):
git branch -a --sort=v:refname
remotes/origin/foo_1_bar
remotes/origin/foo_2_bar
remotes/origin/foo_3_bar
remotes/origin/foo_4_bar
remotes/origin/foo_5_bar
remotes/origin/foo_6_bar
remotes/origin/foo_7_bar
remotes/origin/foo_8_bar
remotes/origin/foo_9_bar
remotes/origin/foo_10_bar
remotes/origin/foo_11_bar
对比:
git branch -a
remotes/origin/foo_10_bar
remotes/origin/foo_11_bar
remotes/origin/foo_1_bar
remotes/origin/foo_2_bar
remotes/origin/foo_3_bar
remotes/origin/foo_4_bar
remotes/origin/foo_5_bar
remotes/origin/foo_6_bar
remotes/origin/foo_7_bar
remotes/origin/foo_8_bar
remotes/origin/foo_9_bar