{"record":{"id":"f30cec99c8063d64","repo":"theonedev/onedev","slug":"branch-not-exist","errorCode":null,"errorMessage":"Branch not exist: ","messagePattern":"Branch not exist: ","errorType":"exception","errorClass":"ExplicitException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/git/GitUtils.java","lineNumber":154,"sourceCode":"\t\t\t\t\t&& headRef.getObjectId() != null) {\n\t\t\t\treturn Repository.shortenRefName(headRef.getTarget().getName());\n\t\t\t} else {\n\t\t\t\treturn null;\n\t\t\t}\n\t\t} catch (IOException e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t}\n\n\tpublic static void setDefaultBranch(Repository repository, String defaultBranch) {\n\t\tvar defaultBranchRef = branch2ref(defaultBranch);\n\t\ttry {\n\t\t\tPreconditions.checkArgument(!defaultBranch.contains(\"..\"));\n\t\t\tif (repository.findRef(defaultBranchRef) != null) {\n\t\t\t\tRefUpdate refUpdate = getRefUpdate(repository, \"HEAD\");\n\t\t\t\tlinkRef(refUpdate, branch2ref(defaultBranch));\n\t\t\t} else {\n\t\t\t\tthrow new ExplicitException(\"Branch not exist: \" + defaultBranch);\t\n\t\t\t}\n\t\t} catch (IOException e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t}\n\n\tpublic static void diff(Repository repository, AnyObjectId oldRevId, AnyObjectId newRevId, @Nullable String excludedFiles, OutputStream stdout) {\n\t\ttry (var diffFormatter = new DiffFormatter(stdout);\n\t\t\t var revWalk = new RevWalk(repository);\n\t\t\t var reader = repository.newObjectReader()) {\n\t\t\tconfigureDiffFormatter(diffFormatter, repository);\n\n\t\t\tvar oldTreeParser = new CanonicalTreeParser();\n\t\t\tif (!oldRevId.equals(ObjectId.zeroId()))\n\t\t\t\toldTreeParser.reset(reader, revWalk.parseCommit(oldRevId).getTree());\n\n\t\t\tvar newTreeParser = new CanonicalTreeParser();\n\t\t\tif (!newRevId.equals(ObjectId.zeroId()))","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/git/GitUtils.java#L136-L172","documentation":"GitUtils.setDefaultBranch points the repository HEAD at the given branch. If the requested branch ref (refs/heads/<branch>) does not exist in the repository, it throws this ExplicitException instead of leaving HEAD dangling. It guards against setting a default branch to a name that has no commits.","triggerScenarios":"Calling setDefaultBranch(repository, branch) where repository.findRef(branch2ref(defaultBranch)) returns null — the branch was never created, was deleted, or the name is misspelled/has wrong case.","commonSituations":"Import scripts setting a default branch before pushing any commits; renaming the default branch and forgetting to create the new one; automated project templates referencing a branch name that differs per repo; typo like 'master' vs 'main'.","solutions":["Verify the branch exists: git ls-remote <repo> refs/heads/<branch> or git branch -a","Create/push the branch first, then set it as default","Fix the branch name spelling/case passed to setDefaultBranch","If the branch was deleted, restore it from its last known commit or pick an existing branch as default"],"exampleFix":"// before\nGitUtils.setDefaultBranch(repository, \"main\"); // main not pushed yet\n\n// after\ntry (RevWalk revWalk = new RevWalk(repository)) {\n    GitUtils.createBranch(repository, \"main\", headCommitId);\n}\nGitUtils.setDefaultBranch(repository, \"main\");","handlingStrategy":"validation","validationCode":"Ref branchRef = repository.findRef(\"refs/heads/\" + branch);\nif (branchRef == null) {\n    throw new IllegalArgumentException(\"Branch \" + branch + \" does not exist; cannot set as default\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  GitUtils.setDefaultBranch(repository, branch);\n} catch (ExplicitException e) {\n  if (e.getMessage().startsWith(\"Branch not exist:\")) {\n    createBranchThenRetry(repository, branch);\n  }\n}","preventionTips":["Push/create the branch before making it the default","Standardize default branch names (main vs master) across templates","List existing refs before scripted default-branch changes"],"tags":["git","branch","resource-not-found","onedev"],"backgroundTag":"resource-not-found","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}