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'
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..
Git’s official documentation for those features:
@{-<n>}
, e.g.@{-1}
means “the nth branch/commit checked out before the current one.”<branch>
argument ofgit 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.)I instead have a script at
~/bin/git-bs
, with contents:This allows me to see a list of all branches, sorted by Most Recently Used, by typing:
git bs
.In a similar vein, I use a
git-bsel
script that runs peco:That gives me interactive selection with search.
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.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
toco
, 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).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-worktreeI 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.