I'm writing a shell script to checkout branches or tags in multiple Git repositories. Sometimes, the checkout is to move HEAD to a tagged commit which does not have a branch associated with it. That is intentional, and it causes a detached HEAD state. While looking for a way to suppress the lengthy detached HEAD warning just for the current script (not in the config), I found
How can this code be modified to suppress the detached HEAD message just for the script?
CodePudding user response:
The order of flags to git
matters:
git <flag-set-1> <command> <flag-set-2> <arguments>
runs git
with flag-set-1
so that git
runs command
with flag-set-2 arguments
while obeying flag-set-1
. The -c advice.detachedHead=false
argument is an argument to git
, not to any of the command
commands. That is, you want:
git -C "$path" -c advice.detachedHead=false checkout ...
rather than:
git -C "$path" checkout -c advice.detachedHead=false ...
(The git clone
command accepts very similar -c
arguments in its flag-set-2, but don't let that mislead you!)