Skip to content

Instantly share code, notes, and snippets.

@sphvn
Last active June 19, 2018 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sphvn/9d84be0f950ef6ccd6c8f98703079905 to your computer and use it in GitHub Desktop.
Save sphvn/9d84be0f950ef6ccd6c8f98703079905 to your computer and use it in GitHub Desktop.
branch switching

https://github.com/junegunn/fzf#using-homebrew-or-linuxbrew

$ brew install fzf

Add the following to your bashrc/zshrc

# fzf between local branches with commit previews z
s () {
  S_BRANCH=$(git for-each-ref --sort=-committerdate --format="%(refname)" refs/heads | fzf --preview="git log {}")
  if [ ! -z "S_BRANCH" ]
  then
    R_BRANCH=$(echo $S_BRANCH | sed -e 's@refs/heads/@@')
    if [ ! -z "$R_BRANCH" ]
    then
      git checkout $R_BRANCH
    fi
  fi
}
# fzf between remote branches with commit previews
sr () {
  git fetch -p
  S_BRANCH=$(git for-each-ref --sort=-committerdate --format="%(refname)" refs/remotes | fzf --preview="git log {}")
  if [ ! -z "S_BRANCH" ]
  then
    R_BRANCH=$(echo $S_BRANCH | sed -e 's@refs/remotes/@@')
    if [ ! -z "$R_BRANCH" ]
    then
      git checkout $R_BRANCH
    fi
  fi
}
# fzf through commits on branch
show() {
  git log --graph --color=always \
      --format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
  fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
      --bind "ctrl-m:execute:
                (grep -o '[a-f0-9]\{7\}' | head -1 |
                xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
                {}
FZF-EOF"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment