如果我在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脚本/解决方案应该执行以下操作
不需要手动执行所有这些步骤的推荐方法是什么?
我正在使用Mac,如果这在这里重要的话。
更新-- 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发布于 2012-07-09 03:54:39
在很少的情况下,您想要签出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>但不包括"remotes“的分支。你可以把sco变成你想要的任何东西。我选它是为了“超级结账”。
当然,如果您有多个与<number>匹配的分支,这将不会很好地工作。然而,这应该是一个不错的起点。
发布于 2020-04-19 18:40:05
下面是我的模糊签出解决方案:通过运行将别名添加到~/.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" 别名可以有两个参数用于模糊匹配,您可以使用它如下:
git fc <keyword1> <keyword2>它将找到签出分支首先匹配。
例如,如果要签出分支1178,可以运行:
git fc 1178别名fc支持两个参数,如果要进行更精确的匹配,还可以运行:
git fc 1178 auth你也可以找到我最喜欢的片段这里
发布于 2017-03-24 15:25:43
这是我自己想出的解决办法。
[ ${#} -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样式(IE忽略搜索的目的),例如以通用前缀开头的分支和类似JIRA票证ID的东西。
例如,输入以下内容: 去检查一下这个 应该匹配这样的东西 这个-分支特性/这个-分支程序/JIRA-123-这个-分支远程器/原点/这个-分支远程器/原点/特征/这个-分支远程器/原点/修复/JIRA-123-这个-分支远程器/起源/JIRA-123-这个-分支远程器/这个-分支 但是,我使用的regexes非常宽容,所以您也可以这样做: git rcheckout JIRA-123 进入: bugfix/JIRA-123-这个-分支JIRA-123-这个-分支远程/这个/bugfix/JIRA-123-这个-分支远程/原产/JIRA-123这个-分支
它默认用于搜索分支前缀,但实际上,如果需要,可以使用regexes执行更好的操作,如下所示:
git rcheckout '.*bran'
git rcheckout '.*is-br.*h'https://stackoverflow.com/questions/11340309
复制相似问题