You checked out a commit, a tag or a remote branch instead of a local branch. Git is fine. HEAD just does not point at a branch right now, so new commits have no branch name to live on.
Done exploring? git switch - takes you back to where you were. Or git switch main.
Keep them: git switch -c new-branch while you are still here. The commits come along.
Find the commit with git reflog, then git branch rescue COMMIT. Commits are not deleted right away.
Git keeps unreachable commits for a while, so a mistake here can nearly always be recovered. Do not wait weeks, though.
you@laptop my-project % git checkout 8092211
Note: switching to '8092211e5c90a717edcdb8fc7ed579175b231453'.
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 switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
HEAD is now at 8092211 base
Git prints this advice itself, and it names the two exits. The commit hash will differ in your repository. The behaviour is described in the git switch documentation, under --detach. Newer Git versions can word the note slightly differently.
git switch -git switch -c my-workgit reflog then git branch rescue COMMITgit switch maingit switch with a branch name, not a commit hash or tag, to avoid detaching.Run git status. The first line says On branch NAME when you are attached, and HEAD detached at HASH when you are not. git branch marks the current branch with a star, or shows (HEAD detached at HASH).
error: pathspec 'main' did not match any file(s) known to gitBranch name is wrong or not local yetRun git branch -a to see the real names, then switch again.error: Your local changes to the following files would be overwritten by checkoutUncommitted work is in the wayCommit it, or park it with git stash, then switch.HEAD points straight at a commit instead of at a branch name. Git works normally, but new commits are not on any branch.
Run git switch - to go back, or git switch main. To keep commits you made, run git switch -c new-branch first.
Not right away. git reflog shows where HEAD has been, and git branch NAME COMMIT gives a commit a branch again.
Usually from checking out a commit hash, a tag, or a remote branch such as origin/main, instead of a local branch.