1. 22

this is a small pair of utilities (pushb/popb), written in rust, that saves a stack of git branches for the current repository, to make it easier to switch back and forth between branches.

Example:

$ # on branch "master"
$ pushb -b tmp-branch
Switched to a new branch 'tmp-branch'
$ # do work, git commit, git push, etc
$ popb
Switched to branch 'master'
  1. 21

    While not exactly the same, git natively supports checking out arbitrary branches in your ‘last checked out’ stack. For example, pass a single dash (i.e. ‘git checkout -’) to checkout the last checked out branch, or pass a number to it to checkout any checked out branch in your ‘last checked out’ stack (e.g. ‘git checkout @{-4}’ for the branch you checked out 4 checkouts ago).

    There may be a way to achieve similar functionality to pushb and popb using builtin git functionality + git/shell aliases, but I haven’t explored that..

    1. 4

      Git’s official documentation for those features:

      • When specifying a revision, @{-<n>}, e.g. @{-1} means “the nth branch/commit checked out before the current one.”
      • For the <branch> argument of git checkout, “you may also specify ‘-’ which is synonymous to ‘@{-1}’”. (I just submitted an email to the Git mailing list suggesting improvements to the formatting of this documentation.)
    2. 3

      I instead have a script at ~/bin/git-bs, with contents:

      git branch --sort=-committerdate "$@"
      

      This allows me to see a list of all branches, sorted by Most Recently Used, by typing: git bs.

      1. 2

        In a similar vein, I use a git-bsel script that runs peco:

        git branch | peco --query "$*" | cut -c 2- | xargs git checkout
        

        That gives me interactive selection with search.

      2. 3

        you can do git checkout - to switch the previous branch. you’ll go back and forth, not down a stack, but usually im only switching one and then going back.

        1. 2

          This is interesting, but at least for my workflow, I find it hard to imagine I’d use it much. I prefer to know which branch I’m checking out.

          The way I ease branch switching is by aliasing checkout to co, and creating branch aliases if I have to deal with particularly long branch names. For example, git co rel to checkout our current release branch (whose name is significantly longer, and changes with each release).

          1. 1

            I recently started using git-worktree more and more. It basically allows you to create a second “view” on a cloned repository which has a different branch checked out: https://git-scm.com/docs/git-worktree

            1. 1

              I had my own crappy git-worktree implementation for a while based on clones. Cool to see it fully implemented in git! Thanks for the link, I’m not sure I knew this existed.

              As a fan of filesystems, I always like branches having their own directories, easier for my brain and my text editor and my shell etc.