Branch branch in git by partial name

If I have the following branches in git

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

I want to switch to a branch using -just-- a number, even if it requires a script to be called for example:

 switch_branch 1178 

and the script / solution should do the following

  • git branch -a (find all branches local and remote in my repository)
  • filter by this parameter ('1178' above)
  • extract the name of the branch in which git can use
  • go to this thread

What is the recommended way to do this without having to complete all of these steps manually?

I use Mac OSX if that matters here.

update - bash -it (github.com/revans/bash-it) serves my purpose

 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 
+6
source share
3 answers

I switched to git-flow workflow and have been happy about it ever since.

0
source

There are very few cases where you want to check remotes/origin/* . They exist, but for the purposes of this shortcut, do not worry about them. This will give you what you want on OSX:

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

You can then issue git sco <number> to check for a branch that includes <number> but excludes "remotes". You can change sco as you like. I just chose it for a "super test."

Of course, this will not work very well if you have multiple branches that match <number> . However, it should be a worthy starting point.

+9
source

Here is a solution that I came up with for myself.

 [ ${#} -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)/)?([AZ]+-[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 

I called it git-rcheckout ("r" for regex, without a better name) and put it in my path (it's too long to include shoehorn in my .gitconfig .)

It will try to map local and remote branches (although it checks the locale) and will tolerate (ignoring IE for search purposes) some JIRA styles, such as branches starting with common prefixes, and things decorated as JIRA ticket identifiers.

eg. By entering this:

 git rcheckout this 

Things must fit like

 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 

But the regular expressions that I used are tolerant enough, which you can also do:

 git rcheckout JIRA-123 

To access:

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

By default, branch prefix search is used, but in fact you can use regular expressions to do more interesting things if you want, for example:

 git rcheckout '.*bran' git rcheckout '.*is-br.*h' 
+3
source

Source: https://habr.com/ru/post/919727/


All Articles