Isn't this case only relevant if lastSegment equals ".git" ?

Could we write this instead as:

  String result = elements[elements.length - 1];
  if (DOT_GIT.equals(result))
    result = elements[elements.length - 2];
  else if (result.endsWith(DOT_GIT))
    result = result.substring(0, result.length() - DOT_GIT.length());
  return result;

as it shouldn't be possible for result to be an empty string at this point?