15

如果我在 git 中有以下分支

1194-qa-server
master
remotes/origin/1178-authentication
remotes/origin/1194-qa-server
remotes/origin/HEAD -> origin/master
remotes/origin/master

我想使用 --just-- 数字切换到分支,即使这需要调用脚本 例如:

switch_branch 1178

并且脚本/解决方案应该执行以下操作

  1. git branch -a (在我的存储库中查找所有本地和远程分支)
  2. 按给定参数过滤(上面的“1178”)
  3. 提取git可以使用的分支名称
  4. 切换到那个分支

无需手动执行所有这些步骤的推荐方法是什么?

我正在使用 Mac OSX,如果这很重要的话。

更新——bash-it (github.com/revans/bash-it) 符合我的目的

Welcome to Bash It!

Here is a list of commands you can use to get help screens for specific pieces of Bash it:

  rails-help                  list out all aliases you can use with rails.
  git-help                    list out all aliases you can use with git.
  todo-help                   list out all aliases you can use with todo.txt-cli
  brew-help                   list out all aliases you can use with Homebrew
  aliases-help                generic list of aliases.
  plugins-help                list out all functions you have installed with bash-it
  bash-it-plugins             summarize bash-it plugins, and their installation status
  reference <function name>   detailed help for a specific function
4

3 回答 3

13

您想要结帐的情况很少remotes/origin/*。它们存在,但出于此快捷方式的目的,我们不必担心它们。这会让你在 OSX 上得到你想要的:

git config --global alias.sco '!sh -c "git branch -a | grep -v remotes | grep $1 | xargs git checkout"'

然后,您可以发出git sco <number>以签出包含<number>但不包括“远程”的分支。你可以改变sco成你想要的任何东西。我只是为“超级结帐”选择了它。

当然,如果您有多个匹配的分支,这将不会很好地工作<number>。然而,它应该是一个不错的起点。

于 2012-07-09T03:54:39.867 回答
5

Here is my solution with fuzzy checkout: Add the alias to your ~/.gitconfig by run

git config --global alias.fc '!f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f'

The command above will add the alias to your ~/.gitconfig:

[alias]
    # fuzzy checkout branch, e.g: git cb feature 739, will checkout branch feature/PGIA-739-deploy-maximum
    fc = "!f() { git branch -a | grep -m1 -e ${1}.*${2} | sed \"s/remotes\\/origin\\///\" | xargs git checkout; }; f" 

The alias can have 2 parameters for the fuzzy matching, you can use it like:

git fc <keyword1> <keyword2>

It will find the checkout the branch first match

For example, if you want to checkout your branch 1178, you can run:

git fc 1178

the alias fc supports two parameters, if you want more accurate matching, you also can run:

git fc 1178 auth

You also can find my other favorite snippets here

于 2020-04-19T18:40:05.110 回答
3

这是我为自己想出的解决方案。

[ ${#} -ne 1 ] && { echo -e "Please provide one search string" ; exit 1 ; }
MATCHES=( $(git branch -a --color=never | sed -r 's|^[* ] (remotes/origin/)?||' | sort -u | grep -E "^((feature|bugfix|release|hotfix)/)?([A-Z]+-[1-9][0-9]*-)?${1}") )
case ${#MATCHES[@]} in
  ( 0 ) echo "No branches matched '${1}'" ; exit 1  ;;
  ( 1 ) git checkout "${MATCHES[0]}"      ; exit $? ;;
esac
echo "Ambiguous search '${1}'; returned ${#MATCHES[@]} matches:"

for ITEM in "${MATCHES[@]}" ; do
  echo -e "  ${ITEM}"
done
exit 1

我称它为git-rcheckout(“r”代表正则表达式,因为想要一个更好的名字)并将它放在我的路径中(它有点太长了,无法插入我的.gitconfig.)

它将尝试匹配本地和远程分支(尽管只检查本地分支),并且会容忍(IE 出于搜索目的而忽略)一些 JIRA 样式,例如以通用前缀开头的分支和类似 JIRA 票证 ID 的样式。

例如输入这个:

git rcheckout this

应该匹配类似的东西

this-branch
feature/this-branch
bugfix/JIRA-123-this-branch
JIRA-123-this-branch
remotes/origin/this-branch
remotes/origin/feature/this-branch
remotes/origin/bugfix/JIRA-123-this-branch
remotes/origin/JIRA-123-this-branch

但是我使用的正则表达式足够宽容,你也可以这样做:

git rcheckout JIRA-123

访问:

bugfix/JIRA-123-this-branch
JIRA-123-this-branch
remotes/origin/bugfix/JIRA-123-this-branch
remotes/origin/JIRA-123-this-branch

它默认搜索分支前缀,但实际上,如果需要,您可以使用正则表达式来做更有趣的事情,如下所示:

git rcheckout '.*bran'
git rcheckout '.*is-br.*h'
于 2017-03-24T15:25:43.757 回答