gitgithub

How to create a git alias which automatically prepends a certain string to a branch checkout?


I'm working on a project which requires me to create a number of Git branches with identical starting components (something like x/y/z/). This slows down tab completion a lot, since there are remotes on this project for other people working on it that have similar but distinct starting components. Is there a way to create a git alias which would automatically prepend my particular branch start to git checkouts?

I tried the very straightforward option (git config --global alias.precheckout "checkout x/y/z/"), but unsurprisingly this failed, as it resulted in a space between x/y/z/ and the name of the branch I actually want to checkout.


Solution

  • I'm working on a project which requires me to create a number of Git branches with identical starting components (something like x/y/z/)

    All refs are local. Use the ones convenient for you in your work, add the prefix when talking to the remote repository, tell Git how you want to do things

    git config push.default upstream
    git checkout -b mybranch -t origin/x/y/z/mybranch 
    

    and Git will figure it out from there. Locally, you work with mybranch. Talking to the upstream (origin here) Git adds the prefix.

    Git has more flexibility on tap, but this little starter kit looks like just what the doctor ordered here. Say git help glossary and hunt up refspec (which will point you at the push and fetch docs, this is just an excuse to get you looking at the glossary).