Skip to content

Instantly share code, notes, and snippets.

@sphvn sphvn/branch-switching.md
Last active Jun 19, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.