theonedev/onedev · error · ExplicitException

Branch not exist:

Error message

Branch not exist: 

What it means

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.

Source

Thrown at server-core/src/main/java/io/onedev/server/git/GitUtils.java:154

					&& headRef.getObjectId() != null) {
				return Repository.shortenRefName(headRef.getTarget().getName());
			} else {
				return null;
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void setDefaultBranch(Repository repository, String defaultBranch) {
		var defaultBranchRef = branch2ref(defaultBranch);
		try {
			Preconditions.checkArgument(!defaultBranch.contains(".."));
			if (repository.findRef(defaultBranchRef) != null) {
				RefUpdate refUpdate = getRefUpdate(repository, "HEAD");
				linkRef(refUpdate, branch2ref(defaultBranch));
			} else {
				throw new ExplicitException("Branch not exist: " + defaultBranch);	
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void diff(Repository repository, AnyObjectId oldRevId, AnyObjectId newRevId, @Nullable String excludedFiles, OutputStream stdout) {
		try (var diffFormatter = new DiffFormatter(stdout);
			 var revWalk = new RevWalk(repository);
			 var reader = repository.newObjectReader()) {
			configureDiffFormatter(diffFormatter, repository);

			var oldTreeParser = new CanonicalTreeParser();
			if (!oldRevId.equals(ObjectId.zeroId()))
				oldTreeParser.reset(reader, revWalk.parseCommit(oldRevId).getTree());

			var newTreeParser = new CanonicalTreeParser();
			if (!newRevId.equals(ObjectId.zeroId()))

View on GitHub (pinned to d44925c47c)

Solutions

  1. Verify the branch exists: git ls-remote <repo> refs/heads/<branch> or git branch -a
  2. Create/push the branch first, then set it as default
  3. Fix the branch name spelling/case passed to setDefaultBranch
  4. If the branch was deleted, restore it from its last known commit or pick an existing branch as default

Example fix

// before
GitUtils.setDefaultBranch(repository, "main"); // main not pushed yet

// after
try (RevWalk revWalk = new RevWalk(repository)) {
    GitUtils.createBranch(repository, "main", headCommitId);
}
GitUtils.setDefaultBranch(repository, "main");
Defensive patterns

Strategy: validation

Validate before calling

Ref branchRef = repository.findRef("refs/heads/" + branch);
if (branchRef == null) {
    throw new IllegalArgumentException("Branch " + branch + " does not exist; cannot set as default");
}

Try / catch

try {
  GitUtils.setDefaultBranch(repository, branch);
} catch (ExplicitException e) {
  if (e.getMessage().startsWith("Branch not exist:")) {
    createBranchThenRetry(repository, branch);
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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'.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of theonedev/onedev@d44925c47c (2026-09-06). Data as JSON: /api/errors/f30cec99c8063d64. Report an issue: GitHub.