Disable warning about detached HEAD
Asked Answered
U

2

95

In git if you checkout a commit directly you get a big fat warning starting with:

"You are in 'detached HEAD' state. You can look around ..."

It's fine - I intend to be in detached HEAD state. However I am using this in a script and I don't want this warning in the output logs but I do want the normal output.

My "ugly" workaround now is to run the same command twice, first with -q to hide the warning, and one more time to get the normal output: HEAD is now at deadbeef... Message since the warning is only printed once.

Can the warning be disabled so I can avoid workarounds or parsing the output ?

Unrepair answered 22/4, 2016 at 12:57 Comment(0)
D
87

You have the config for this task:

Set the desired message off by setting the config value to false:

# turn the detached message off
git config --global advice.detachedHead false

detachedHead

Advice shown when you used git-checkout(1) to move to the detach HEAD state, to instruct how to create a local branch after the fact.


advice.*

These variables control various optional help messages designed to aid new users. All advice.* variables default to true, and you can tell Git that you do not need help by setting these to false:

You can set any of the following after the advice:

git config --global advice.<...>


pushUpdateRejected
    Set this variable to false if you want to disable 
        pushNonFFCurrent,
        pushNonFFMatching, 
        pushAlreadyExists, 
        pushFetchFirst, and 
        pushNeedsForce simultaneously.

pushNonFFCurrent
    Advice shown when git-push(1) fails due to a non-fast-forward update
    to the current branch.

pushNonFFMatching
    Advice shown when you ran git-push(1) and pushed matching refs
    explicitly (i.e. you used :, or specified a refspec that isn’t your
    current branch) and it resulted in a non-fast-forward error.

pushAlreadyExists
    Shown when git-push(1) rejects an update that does not qualify
    for fast-forwarding (e.g., a tag.)

pushFetchFirst
    Shown when git-push(1) rejects an update that tries to overwrite a
    remote ref that points at an object we do not have.

pushNeedsForce
    Shown when git-push(1) rejects an update that tries to overwrite a
    remote ref that points at an object that is not a commit-ish, or make
    the remote ref point at an object that is not a commit-ish.

statusHints
    Show directions on how to proceed from the current state in the output
    of git-status(1), in the template shown when writing commit messages in
    git-commit(1), and in the help message shown by git-checkout(1) when
    switching branch.

statusUoption
    Advise to consider using the -u option to git-status(1) when the command
    takes more than 2 seconds to enumerate untracked files.

commitBeforeMerge
    Advice shown when git-merge(1) refuses to merge to avoid overwriting
    local changes.

resolveConflict
    Advice shown by various commands when conflicts prevent the operation
    from being performed.

implicitIdentity
    Advice on how to set your identity configuration when your information
    is guessed from the system username and domain name.

detachedHead
    Advice shown when you used git-checkout(1) to move to the detach HEAD
    state, to instruct how to create a local branch after the fact.

amWorkDir
    Advice that shows the location of the patch file when git-am(1) fails
    to apply it.

rmHints
    In case of failure in the output of git-rm(1), show directions on
    how to proceed from the current state.
Dorinda answered 22/4, 2016 at 13:9 Comment(5)
To temporarily disable this warning (i.e. without changing config), do something like git -c advice.detachedHead=false checkout <refspec>.Interest
@Interest should make than an answer for free rep ;). Most of the time I want such commands are for one-off things in a shell script and global modifications are a) not portable, b) mess with "my" environment.Complicacy
@NickT agreed, I just did that because it was super useful to me too ^^Replicate
For what it is worth, you can also use the option --quiet to avoid the warning.Brisbane
@Brisbane --quiet alone does not work with git clone --branch <some-tag> but @Interest suggestion does.Kenogenesis
R
149

Shamelessly copying mjs's comment to post it as an answer on its own:

git -c advice.detachedHead=false checkout <refspec>

The -c advice.detachedHead=false parameter will allow you to suppress the warning without having to change the global config. It will only apply to the executed command. Here's the list of all advice that can be suppressed.

Replicate answered 12/8, 2017 at 15:49 Comment(0)
D
87

You have the config for this task:

Set the desired message off by setting the config value to false:

# turn the detached message off
git config --global advice.detachedHead false

detachedHead

Advice shown when you used git-checkout(1) to move to the detach HEAD state, to instruct how to create a local branch after the fact.


advice.*

These variables control various optional help messages designed to aid new users. All advice.* variables default to true, and you can tell Git that you do not need help by setting these to false:

You can set any of the following after the advice:

git config --global advice.<...>


pushUpdateRejected
    Set this variable to false if you want to disable 
        pushNonFFCurrent,
        pushNonFFMatching, 
        pushAlreadyExists, 
        pushFetchFirst, and 
        pushNeedsForce simultaneously.

pushNonFFCurrent
    Advice shown when git-push(1) fails due to a non-fast-forward update
    to the current branch.

pushNonFFMatching
    Advice shown when you ran git-push(1) and pushed matching refs
    explicitly (i.e. you used :, or specified a refspec that isn’t your
    current branch) and it resulted in a non-fast-forward error.

pushAlreadyExists
    Shown when git-push(1) rejects an update that does not qualify
    for fast-forwarding (e.g., a tag.)

pushFetchFirst
    Shown when git-push(1) rejects an update that tries to overwrite a
    remote ref that points at an object we do not have.

pushNeedsForce
    Shown when git-push(1) rejects an update that tries to overwrite a
    remote ref that points at an object that is not a commit-ish, or make
    the remote ref point at an object that is not a commit-ish.

statusHints
    Show directions on how to proceed from the current state in the output
    of git-status(1), in the template shown when writing commit messages in
    git-commit(1), and in the help message shown by git-checkout(1) when
    switching branch.

statusUoption
    Advise to consider using the -u option to git-status(1) when the command
    takes more than 2 seconds to enumerate untracked files.

commitBeforeMerge
    Advice shown when git-merge(1) refuses to merge to avoid overwriting
    local changes.

resolveConflict
    Advice shown by various commands when conflicts prevent the operation
    from being performed.

implicitIdentity
    Advice on how to set your identity configuration when your information
    is guessed from the system username and domain name.

detachedHead
    Advice shown when you used git-checkout(1) to move to the detach HEAD
    state, to instruct how to create a local branch after the fact.

amWorkDir
    Advice that shows the location of the patch file when git-am(1) fails
    to apply it.

rmHints
    In case of failure in the output of git-rm(1), show directions on
    how to proceed from the current state.
Dorinda answered 22/4, 2016 at 13:9 Comment(5)
To temporarily disable this warning (i.e. without changing config), do something like git -c advice.detachedHead=false checkout <refspec>.Interest
@Interest should make than an answer for free rep ;). Most of the time I want such commands are for one-off things in a shell script and global modifications are a) not portable, b) mess with "my" environment.Complicacy
@NickT agreed, I just did that because it was super useful to me too ^^Replicate
For what it is worth, you can also use the option --quiet to avoid the warning.Brisbane
@Brisbane --quiet alone does not work with git clone --branch <some-tag> but @Interest suggestion does.Kenogenesis

© 2022 - 2024 — McMap. All rights reserved.