2016-04-09 67 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