diff --git a/README.md b/README.md index 11f9515..743d732 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/). For a steady stream of TILs from a variety of rocketeers, checkout [til.hashrocket.com](https://til.hashrocket.com/). -_793 TILs and counting..._ +_794 TILs and counting..._ --- @@ -227,6 +227,7 @@ _793 TILs and counting..._ - [Stash Everything](git/stash-everything.md) - [Stashing Only Unstaged Changes](git/stashing-only-unstaged-changes.md) - [Stashing Untracked Files](git/stashing-untracked-files.md) +- [Switch To A Recent Branch With FZF](git/switch-to-a-recent-branch-with-fzf.md) - [Untrack A Directory Of Files Without Deleting](git/untrack-a-directory-of-files-without-deleting.md) - [Untrack A File Without Deleting It](git/untrack-a-file-without-deleting-it.md) - [Update The URL Of A Remote](git/update-the-url-of-a-remote.md) diff --git a/git/switch-to-a-recent-branch-with-fzf.md b/git/switch-to-a-recent-branch-with-fzf.md new file mode 100644 index 0000000..ce9337f --- /dev/null +++ b/git/switch-to-a-recent-branch-with-fzf.md @@ -0,0 +1,24 @@ +# Switch To A Recent Branch With FZF + +[Git](https://git-scm.com/) and [FZF](https://github.com/junegunn/fzf) make +a powerful team. Once you've installed FZF on your machine, there are all +kinds of add-on scripts that you can include in your shell configuration. + +This one, for instance, is a personal favorite. + +```bash +fbr() { + local branches branch + branches=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)") && + branch=$(echo "$branches" | + fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) && + git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##") +} +``` + +When you run the `fbr` command from your shell, FZF will compile a list of +the most recently committed to branches (including remote branches). That +list will then be available in a standard FZF prompt so that you can choose +the branch you want to switch to. + +[source](https://github.com/junegunn/fzf/wiki/examples)