Background
I'm debugging a script which wraps several Git operations, the most significant of which is running git checkout <specifier>
As you know git checkout
accepts either a commit-ish or a branch name:
The script was written based on the "flexibility" (though I now have to say it was an awful idea but anyways...). Due to the "flexibility", the upstream stage sends <specifier>
without any metadata on what it means; it can either be a SHA-1 shorthand like 8e13bb8
or a (remote) branch name such as feature/optimize-foo
. In the latter case, it might not exist as a local branch name at the point of receiving it; git checkout
is so flexible that it can create a local tracking branch automatically (see the command reference above, a paragraph which begins with "If <branch>
is not found but there does exist a tracking branch in exactly one remote")
Problem
Now I need to insert some extra operations only when <specifier>
was a (remote) branch name but I'm struggling to find a way to do so. Something along the lines of
if git this-is-branch-name $specifier; then
git checkout $specifier
git pull
else
git checkout --detach $specifier
fi
(The above example does not reflect the complexity of the actual script I'm dealing with, so please don't "recommend" me nicer ways to write it)
Note
- The new command
git switch
which is meant to partly replacegit checkout
does not (immediately) help me as the original script already conflated several different operations under a singlegit checkout