2

My team works on separate branches and, commonly, I need to switch from branches quickly to help them. But I need to spend a lot of time finding which branch they are working on and I need to retype the whole branch names (and they are very long).

So I found a handy command that lets me get the branch name without cycling through the whole list: git branch -a | find "72" and it returns me this: remotes/origin/72-js-remove-the-player-from-the-game. Our branch names always have the merge request number of GitLab in the front of the name, so it's always a unique number.

I would like to shorten this to a simple git find 20. I thought that git CLI was similar to bash shell but to add aliases with arguments, I need to access .bashrc (Which I'm unable to find for the Git CLI).

Also, I would like to maybe shorten the whole process of searching for a branch, retyping it into the CLI and switching to it with a single command similar to that (something like git qswitch 20), if that's possible.

YesThatIsMyName
  • 1,291
  • 3
  • 19
  • 24
BT_Code
  • 31
  • 5

1 Answers1

2

I am not sure about aliasing like the way you requested but there is an alias option in the git config where you can do something similar

git config --global alias.fin 'branch -a | find'

Git doesn’t automatically infer your command if you type it in partially. If you don’t want to type the entire text of each of the Git commands, you can easily set up an alias for each command using git config.

I am not sure how well it would work with piping to be honest

So basically your command would become git fin "72"

refer https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases https://www.atlassian.com/blog/git/advanced-git-aliases

Edit: thanks to @torek

If we wanted to use pipes the above method would not actually work, here is a working example

git config --global alias.fin !f(){\ git branch -a | find $1 ; \};f

Y4glory
  • 802
  • 2
  • 16
  • 1
    To get shell-like capabilities, such as pipes, in a Git alias, you must invoke the shell from that Git alias (leading `!` in the expansion). – torek Apr 07 '20 at 19:51
  • Hey thanks, I did some digging into what you said. And have edited the answer to include that – Y4glory Apr 08 '20 at 07:41