i'm writing shell script switches specified commit, makes copy of index in /tmp, resets head original position, runs command in temporary directory:
orig_head=$(git rev-parse -q head) # "refs/heads/master" git checkout "$1" # copy index /tmp/... git checkout "$orig_head" # run command in /tmp/... however, script gives me same "'detached head' state" message when run git checkout refs/heads/master:
note: checking out 'refs/heads/master'. in 'detached head' state. can around, make experimental changes , commit them, , can discard commits make in state without impacting branches performing checkout. if want create new branch retain commits create, may (now or later) using -b checkout command again. example: git checkout -b new_branch_name head @ c82ad67... use vector , binary search dictionary how can store , restore position of head properly?
i prefer avoid using reset head@{1}, seems messy.
most git commands don't care whether name branch name. obvious exceptions (there may more, these off top of head) git branch and, of course, git checkout.
for non-exceptions, rules resolving branch names sha-1s listed in gitrevisions. git branch it's easier: arguments branch-names, e.g., in git branch new/branch/name, new/branch/name branch name, though presumably doesn't exist yet.
the checkout command can't that: given git checkout xyz, xyz might branch name, or tag name, or of other forms in gitrevisions. of course, 1 of funny syntax forms head~5 has have special ~ character in it, unadorned name might branch name, or might not. (if follows -b, in git checkout -b new/branch is branch-name, that's not case here.)
anyway, short answer git checkout has own special rules, different listed in gitrevisions: name branch name if adding refs/heads/ in front works turn existing branch name.
this fails refs/heads/master since refs/heads/refs/heads/master not existing branch name.1 hence need strip off refs/heads/ part yourself.
you after-the-fact, there's easier version: git symbolic-ref has --short leave refs/heads/ part off. since head should symbolic reference branch (never symbolic ref tag instance), , know you're inquiring head, do:
orig_head=$(git symbolic-ref -q --short head) you need 1 more bit, remember whether system in detached head state start with, , if so, sha-1 goes with. so:
sha1=$(git rev-parse head) || exit 1 # should never fail orig_head=$(git symbolic-ref -q --short head) && symbolic=true || symbolic=false or along lines.
1you can create such branch. don't. :-)