按部分名称在git中切换分支
如果我在git中有以下分支按部分名称在git中切换分支,git,shell,console,bash-it,Git,Shell,Console,Bash It,如果我在git中有以下分支 1194-qa-server master remotes/origin/1178-authentication remotes/origin/1194-qa-server remotes/origin/HEAD -> origin/master remotes/origin/master 我想切换到一个使用——只是——数字的分支,即使这需要调用脚本 例如: switch_branch 1178 脚本/解决方案应该执行以下操作 git分支-a(在我的存储库中
1194-qa-server
master
remotes/origin/1178-authentication
remotes/origin/1194-qa-server
remotes/origin/HEAD -> origin/master
remotes/origin/master
我想切换到一个使用——只是——数字的分支,即使这需要调用脚本
例如:
switch_branch 1178
脚本/解决方案应该执行以下操作
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
欢迎使用Bash It!
下面是一个命令列表,您可以使用这些命令获取Bash it特定部分的帮助屏幕:
rails帮助列出了可用于rails的所有别名。
git帮助列出了可以与git一起使用的所有别名。
todo帮助列出所有可用于todo.txt-cli的别名
brew帮助列出可用于自制的所有别名
别名帮助创建别名的通用列表。
插件帮助列出了使用bash-it安装的所有功能
bash-it插件概述bash-it插件及其安装状态
请参考特定功能的详细帮助
在很少的情况下,您希望签出remotes/origin/*
。它们是存在的,但是为了这个捷径,我们不必担心它们。这将使您在OSX上获得所需的内容:
git config --global alias.sco '!sh -c "git branch -a | grep -v remotes | grep $1 | xargs git checkout"'
然后,您可以发出gitsco
以签出包含
但不包含“remotes”的分支。您可以将sco
更改为您想要的任何内容。我只是为了“超级结帐”选的
当然,如果您有多个与
匹配的分支,那么这将不会非常有效。不过,这应该是一个不错的起点。以下是我为自己想出的解决方案
[ ${#} -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”代表regex,因为没有更好的名字),并把它放在我的路径中(它太长了,无法塞进我的.gitconfig
)
它将尝试匹配本地和远程分支(尽管只检查本地分支),并将容忍(即为了搜索而忽略)一些JIRA样式,例如以公共前缀开头的分支和类似JIRA票证ID的样式
e、 g.键入以下内容:
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'
以下是我的模糊结账解决方案: 通过运行将别名添加到
~/.gitconfig
git config --global alias.fc '!f() { git branch -a | grep -m1 -e ${1}.*${2} | sed "s/remotes\/origin\///" | xargs git checkout; }; f'
上面的命令将把别名添加到您的~/.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"
别名可以有2个模糊匹配参数,您可以像这样使用它:
git fc <keyword1> <keyword2>
alias fc支持两个参数,如果需要更精确的匹配,还可以运行:
git fc 1178 auth
您还可以使用bash找到我最喜欢的其他代码片段,您可以使用git checkout 1178[TAB];)使用一些它只是
gco 1178[TAB]
对我来说不起作用。我确实在网上找到了一些关于hash自动完成的参考资料,但这不是我要找的。你实际上需要安装git autocomplete forgit checkout 1178[TAB]
才能工作。这里的更多信息:它确实起到了作用,但我发现bash更好。在末尾调用head-1
,即使有多个匹配项,你也应该做得很好。对于通常由Jira创建的大型分支名称,这种方法将有效。还支持git流
范式。杰出的
git fc 1178 auth