V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
royeo
V2EX  ›  Go 编程语言

如何更优雅地切换 Git 分支

  •  
  •   royeo ·
    royeo · 2019-02-13 22:13:03 +08:00 · 1951 次点击
    这是一个创建于 1936 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在日常开发中,我们经常需要在不同的 Git 分支之间来回切换,特别是业务需求比较多的开发人员。在分支较多的情况下,分支名的 tab 自动补全会比较糟糕,切换时我们不免需要复制或手打分支名,那么有没有更优雅的方式了呢?

    为了提高切换 Git 分支的效率,我用 Golang 写了 git-checkout-branch 这个小工具,可以交互式的切换分支,并自带搜索功能,帮助你更优雅的进行分支切换。

    概览

    Github 地址: https://github.com/royeo/git-checkout-branch 欢迎 star。

    说明:

    • 使用箭头键 进行移动,也支持 jk 的上下移动
    • 使用 / 切换搜索

    安装

    使用 go get 安装 git checkout-branch 命令,确保 $GOPATH/bin 路径在 PATH 中。

    go get -u github.com/royeo/git-checkout-branch
    

    如果你正在使用 GO1.11 Module,使用以下命令进行安装:

    GO111MODULE=off go get -u github.com/royeo/git-checkout-branch
    

    建议为 checkout-branch 设置别名,例如 cb, 这样就可以直接使用 git cb 来进行分支切换。

    git config --global alias.cb checkout-branch
    

    帮助

    使用 git checkout-branch help 获取帮助信息。

    Checkout git branches more efficiently.
    
    Usage:
      git checkout-branch [flags]
    
    Flags:
      -a, --all          List both remote-tracking branches and local branches
      -r, --remotes      List the remote-tracking branches
      -n, --number       Set the number of branches displayed in the list (default 10)
          --hide-help    Hide the help information
    
    第 1 条附言  ·  2019-02-18 17:04:07 +08:00

    支持直接下载安装:

    curl -sSL https://github.com/royeo/git-checkout-branch/releases/download/v0.2.0/git-checkout-branch-`uname -s`-`uname -m` -o /usr/local/bin/git-checkout-branch && chmod +x /usr/local/bin/git-checkout-branch
    
    2 条回复    2019-03-12 22:16:59 +08:00
    sunjourney
        1
    sunjourney  
       2019-03-06 20:35:47 +08:00
    zsh 有插件呀
    royeo
        2
    royeo  
    OP
       2019-03-12 22:16:59 +08:00
    zsh 的插件我也在用,不过相比之下,还是觉得交互式的分支切换更方便些
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1057 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:12 · PVG 07:12 · LAX 16:12 · JFK 19:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.