pcottle/learnGitBranching · error · GitError

git-error-reset-detached

Error message

git-error-reset-detached

What it means

Thrown by `git reset` when HEAD is in a detached state. LearnGitBranching simulates git in-memory; since `git reset <target>` (especially with --hard) needs a branch to move, the app refuses when HEAD points directly at a commit.

Source

Thrown at src/js/git/commands.js:675

      var generalArgs = command.getGeneralArgs();

      if (commandOptions['--soft']) {
        throw new GitError({
          msg: intl.str('git-error-staging')
        });
      }
      if (commandOptions['--hard']) {
        command.addWarning(
          intl.str('git-warning-hard')
        );
        // don't absorb the arg off of --hard
        generalArgs = generalArgs.concat(commandOptions['--hard']);
      }

      command.validateArgBounds(generalArgs, 1, 1);

      if (engine.getDetachedHead()) {
        throw new GitError({
          msg: intl.str('git-error-reset-detached')
        });
      }

      engine.reset(generalArgs[0]);
    }
  },

  revert: {
    regex: /^git +revert($|\s)/,
    description: 'Revert some existing commits',
    execute: function(engine, command) {
      var generalArgs = command.getGeneralArgs();

      command.validateArgBounds(generalArgs, 1, Number.MAX_VALUE);
      engine.revert(generalArgs);
    }
  },

View on GitHub (pinned to 5b09d0ff96)

Solutions

  1. Reattach HEAD first: `git checkout main` (or any branch), then rerun reset
  2. Use `git checkout <ref>` to move a detached HEAD instead of reset
  3. In scripting/tests, check engine.getDetachedHead() before issuing reset

Example fix

// before (detached HEAD)
git reset --hard c2
// after
git checkout main
git reset --hard c2
Defensive patterns

Strategy: validation

Validate before calling

if (!engine.getDetachedHead()) { engine.reset('main'); } else { engine.checkout('main'); }

Prevention

When it happens

Trigger: Running `git reset` (or `git reset --hard <ref>`) after checking out a commit or otherwise detaching HEAD; engine.getDetachedHead() returns true.

Common situations: User checks out a commit SHA in the visualization, then tries reset instead of checkout to move; also common after switch --detach demonstrations.

Related errors


AI-assisted analysis of pcottle/learnGitBranching@5b09d0ff96 (2026-08-27). Data as JSON: /api/errors/590c2b848bbac662. Report an issue: GitHub.