【问题标题】:How can I get a list of Git branches that I've recently checked out?如何获取我最近签出的 Git 分支列表?
【发布时间】:2014-09-25 12:46:59
【问题描述】:

在 Git 分支之间移动时,我有时会忘记我最近所在的分支的名称。如何按结帐顺序显示最近签出的分支/标签/提交列表?

【问题讨论】:

    标签: git git-branch git-checkout


    【解决方案1】:

    总结:

    您可以使用 Git 的 reflog按结帐顺序显示最近的动作:git reflog

    脚本:

    这是一个脚本,您可以通过 git recent 从任何 Git 存储库中下载和使用:https://gist.github.com/jordan-brough/48e2803c0ffa6dc2e0bd

    用法:

    $ (master) git recent -n 5
    
    1) master  4) deleted-branch
    2) stable  5) improve-everything
    3) fun
    Choose a branch: 2
    
    $ (stable) …
    

    有关更多详细信息/选项,请参阅the gist

    详情:

    这基本上是脚本为使 reflog 输出更有用所做的工作:

    $ git reflog | egrep -io "moving from ([^[:space:]]+)" | awk '{ print $3 }' | awk ' !x[$0]++' | egrep -v '^[a-f0-9]{40}$' | head -n5
    master
    stable
    fix-stuff
    some-cool-feature
    feature/improve-everything
    

    【讨论】:

    • 以 Jordan 的要点为基础,我制作了一个脚本,通过从列表中选择一个最近的分支来检查它:gist.github.com/fritz-c/c1e528b09bc1c0827a3c
    • 谢谢@Chris!我已经更新了我的脚本以融入这个想法,并且我已将你列为脚本的贡献者。
    • 更新的脚本是这样的,顺便说一句:gist.github.com/jordan-brough/48e2803c0ffa6dc2e0bd
    • 你能把这个答案合并到一个 git 别名中吗?我不知道如何在 gitconfig 别名中使用 grep 和其他 cli 工具。最好使用选项 - like git log?
    • @kuga 的要点有关于如何使其可用的说明,如git recent(下载脚本,使其可执行,将其放在您的路径中)。您是否有理由要改用 git 别名?
    【解决方案2】:

    我的 zshell 中有一个类似的衬里,它需要一个 arg 来指定历史应该多长时间,默认为 10。

    alias bstack='f() { git reflog | grep checkout | cut -d " " -f 8 | uniq | head ${1} | cat -n };f'
    

    例如,列出最后 3 个分支

    bstack -3
    
    
     1  my-current-branch
     2  my-previous-branch
     3  my-third-most-recent-branch
    

    我从中导出了几个有用的快捷方式

    alias bjmp='fn() { bstack ${1} | tail -1 | cut -f 2 | xargs git checkout  }; fn'
    

    允许我从上面的数字中指定要签出的分支

    bjmp -3
    

    将结帐“my-third-most-recent-branch”

    alias b="bstack -1"
    alias bpop="bjmp -2"
    

    在一次击键中查看当前分支也很有用(尽管这不是最简单的方法),并且只检查前一个分支。

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2014-11-25
      • 2010-12-19
      相关资源
      最近更新 更多