如何使用模糊查找器获取 git 的分支?

How to get a git's branch with fuzzy finder?

我找到了 git examples with fzf(fuzzy finder),它们确实很好用。 喜欢:

# fbr - checkout git branch
fbr() {
  local branches branch
  branches=$(git branch -vv) &&
  branch=$(echo "$branches" | fzf +m) &&
  git checkout $(echo "$branch" | awk '{print }' | 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 的分支,它在我按下 enter 后立即切换,但是有没有办法输入首先像git push staging(然后获取分支列表并将选定的分支放在调用分支列表之前光标所在的位置,然后我按回车键将选定的分支推送到staging

例如: git push staging (Ctrl-b - 选择一个分支) 我想得到这个输出 - git push staging selected_branch

这些是我在 bash

中使用的键绑定
  • CTRL-GCTRL-F - git status
  • 中列出的文件
  • CTRL-GCTRL-B - 分支机构
  • CTRL-GCTRL-T - 标签
  • CTRL-GCTRL-H - 提交哈希值
  • CTRL-GCTRL-R - 遥控器

请注意,如果您使用的是 tmux,则 redraw-current-line 不是必需的。

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 }'
}

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 }' |
    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  " " }' | uniq |
    fzf --height 40% --tac | awk '{print }'
}

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"'

如果您使用的是 Windows,请尝试 git checkout @(git branch -a | fzf).trim()