I'm writing a shell script which switches to a specified commit, makes a copy of the index in /tmp
, resets HEAD
to original position, then runs a command in the temporary directory:
orig_head=$(git rev-parse -q HEAD) # "refs/heads/master"
git checkout "$1"
# copy index to /tmp/...
git checkout "$orig_head"
# run command in /tmp/...
However, this script gives me the same "'detached HEAD' state" message as when I run git checkout refs/heads/master
:
Note: checking out 'refs/heads/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at c82ad67... Use vector and binary search for dictionary
How can I store and restore the position of HEAD
properly?
I would prefer to avoid using reset HEAD@{1}
, as that seems messy.