2016-04-09 54 views
4

我发现git examples with fzf(fuzzy finder)他们确实很好。 像:如何用模糊查找器获取git的分支?

# fbr - checkout git branch 
fbr() { 
    local branches branch 
    branches=$(git branch -vv) && 
    branch=$(echo "$branches" | fzf +m) && 
    git checkout $(echo "$branch" | awk '{print $1}' | sed "s/.* //") 
} 

# fbr - checkout git branch (including remote branches) 
fbr() { 
    local branches branch 
    branches=$(git branch --all | grep -v HEAD) && 
    branch=$(echo "$branches" | 
      fzf-tmux -d $((2 + $(wc -l <<< "$branches"))) +m) && 
    git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##") 
} 

我有这个在我的的.bashrc

bind '"\C-b": "fbr \n"' 

后,我按按Ctrl-B我能选择一个git的分支并切换之后我按回车,但有没有办法键入东西,如git push staging(然后获取分支列表,并将选定的分支右移到分支列表前的光标所在位置,然后按Enter键将所选分支推送到staging

例: git push staging按Ctrl-B - 选择一个分支),我希望得到这个输出 - git push staging selected_branch

+1

考虑git完成? https://github.com/git/git/blob/master/contrib/completion/git-completion.bash – webb

+0

@webb非常感谢你。 – whitesiroi

回答

4

这些都是我在bash使用的键绑定

  • CTRL- GCTRL-F - 文件列表git status
  • CTRL-GCTRL-B - 门类
  • CTRL-GCTRL-T - 标签
  • CTRL-GCTRL-H - 提交散列
  • CTRL-GCTRL- R - 遥控器

请注意如果您使用tmux,则不需要。

is_in_git_repo() { 
    git rev-parse HEAD > /dev/null 2>&1 
} 

gf() { 
    is_in_git_repo && 
    git -c color.status=always status --short | 
    fzf --height 40% -m --ansi --nth 2..,.. | awk '{print $2}' 
} 

gb() { 
    is_in_git_repo && 
    git branch -a -vv --color=always | grep -v '/HEAD\s' | 
    fzf --height 40% --ansi --multi --tac | sed 's/^..//' | awk '{print $1}' | 
    sed 's#^remotes/[^/]*/##' 
} 

gt() { 
    is_in_git_repo && 
    git tag --sort -version:refname | 
    fzf --height 40% --multi 
} 

gh() { 
    is_in_git_repo && 
    git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph | 
    fzf --height 40% --ansi --no-sort --reverse --multi | grep -o '[a-f0-9]\{7,\}' 
} 

gr() { 
    is_in_git_repo && 
    git remote -v | awk '{print $1 " " $2}' | uniq | 
    fzf --height 40% --tac | awk '{print $1}' 
} 

bind '"\er": redraw-current-line' 
bind '"\C-g\C-f": "$(gf)\e\C-e\er"' 
bind '"\C-g\C-b": "$(gb)\e\C-e\er"' 
bind '"\C-g\C-t": "$(gt)\e\C-e\er"' 
bind '"\C-g\C-h": "$(gh)\e\C-e\er"' 
bind '"\C-g\C-r": "$(gr)\e\C-e\er"' 
+0

+一个Aaawesome,它完美的工作^^谢谢你队友^^ – whitesiroi

+1

没问题。更新了gb()以包含远程分支。 –

+0

非常感谢:) – whitesiroi