{"record":{"id":"bf7c531bcc98b87b","repo":"pcottle/learnGitBranching","slug":"cannot-fetch-to-ref-when-checked-out-on","errorCode":null,"errorMessage":"cannot fetch to ' + ref + ' when checked out on ' + ref","messagePattern":"cannot fetch to ' \\+ ref \\+ ' when checked out on ' \\+ ref","errorType":"exception","errorClass":"GitError","httpStatus":null,"severity":"error","filePath":"src/js/git/commands.js","lineNumber":53,"sourceCode":"};\n\nvar validateOriginBranchName = function(engine, name) {\n  return engine.origin.validateBranchName(name);\n};\n\nvar validateBranchNameIfNeeded = function(engine, name) {\n  if (engine.refs[name]) {\n    return name;\n  }\n  return validateBranchName(engine, name);\n};\n\nvar assertNotCheckedOut = function(engine, ref) {\n  if (!engine.refs[ref]) {\n    return;\n  }\n  if (engine.HEAD.get('target') === engine.refs[ref]) {\n    throw new GitError({\n      msg: intl.todo(\n        'cannot fetch to ' + ref + ' when checked out on ' + ref\n      )\n    });\n  }\n};\n\nvar assertIsBranch = function(engine, ref) {\n  assertIsRef(engine, ref);\n  var obj = engine.resolveID(ref);\n  if (!obj || obj.get('type') !== 'branch') {\n    throw new GitError({\n      msg: intl.todo(\n        ref + ' is not a branch'\n      )\n    });\n  }\n};","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/pcottle/learnGitBranching/blob/5b09d0ff9678cad164f5a7488a279a44f90d7256/src/js/git/commands.js#L35-L71","documentation":"assertNotCheckedOut throws when a fetch/push-like command would write to a remote-tracking ref that corresponds to the branch currently checked out (engine.HEAD points at it). Mirrors git's refusal to update the branch you're on via fetch.","triggerScenarios":"Fetching into a ref (e.g. git fetch origin main:main) while HEAD targets that same branch/ref in the simulated engine.","commonSituations":"Practicing refspec fetches in LearnGitBranching remote levels; forgetting to switch branches before simulating a fetch-to-ref.","solutions":["git checkout another branch first, then fetch into the ref","Fetch without a destination refspec (just git fetch origin) and let o/ tracking refs update","Push instead of fetch if your intent was to update origin"],"exampleFix":"// before\ngit checkout maingit fetch origin main:main\n// after\ngit checkout -b tempgit fetch origin main:main","handlingStrategy":"validation","validationCode":"function canFetchInto(engine, ref) { return !!engine.refs[ref] && engine.HEAD.get('target') !== engine.refs[ref]; }","typeGuard":null,"tryCatchPattern":"try { fetchInto(ref); } catch (e) { if (e instanceof GitError && /checked out/.test(e.msg)) checkoutTempThenRetry(); else throw e; }","preventionTips":["Check engine.HEAD target before fetch-to-ref","Default to refspec-less fetch which never hits this"],"tags":["git","fetch","remote","head"],"backgroundTag":"refusing-to-update-checked-out-branch","analyzedSha":"5b09d0ff9678cad164f5a7488a279a44f90d7256","analyzedAt":"2026-08-27T13:53:49.057Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}