git plumbing team for this
git symbolic-ref HEAD refs/heads/existing_branch
You cannot go into the state of an individual head (he needs a link, not a SHA). However, you can switch to a non-existent ref (unknown branch). To prevent this, perhaps use the following git alias :
git config --global alias.switch '!f() { git show-ref --heads --verify "refs/heads/$1" && git symbolic-ref -m "switch to branch $1 not touching workdir" HEAD "refs/heads/$1"; }; f'
Then you can use it as a git switch existing_branch . Note that you need the -m option to see the entry in git reflog , this is usually what you want.
Notes:
git reflog HEAD shows the ref HEAD log (you will again see the comment specified with the -m option). The full file (which includes before SHA in case you want to find the lost commit) is stored in $(git rev-parse --git-dir)/logs/HEAD (for HEAD )git show-ref --heads lists all the links you can use in RHS. As shown in alias , you can use it with --verify to check if the argument is a valid (existing) ref (aka. Branch).- Beware:
git symbolic-ref can point to any other symbol, for example refs/tags/ or refs/remotes/ or even inside packed links. This is probably not what you want, so the alias limits this to the value of refs/heads/ .
Tino source share