Merge pull request #3066 from gaojunran/fix/branch-base-created-from-head

fix(git): don't treat bare HEAD as a branch diff base
This commit is contained in:
Bohdan Triapitsyn
2026-08-27 23:13:50 +03:00
committed by GitHub
2 changed files with 13 additions and 2 deletions
+5 -2
View File
@@ -2660,7 +2660,8 @@ const BRANCH_CREATION_SOURCE_RE = /^branch: Created from (.+)$/;
* Parse a branch reflog (`git reflog show --format=%gs <branch>`) and return the
* ref the branch was created from, when that source is itself a named ref.
*
* Returns null when the branch was created from `HEAD@{...}` or a raw commit
* Returns null when the branch was created from `HEAD` (bare, as `git switch -c`
* / `git checkout -b` without an explicit start point record) or a raw commit
* (detached start): the original branch name is not recorded anywhere in that
* case, and guessing a base from commit topology would be a heuristic, not an
* answer. Callers should ask the user to pick a base instead.
@@ -2675,7 +2676,9 @@ export function parseBranchCreationSource(reflogText) {
const match = lines[index].match(BRANCH_CREATION_SOURCE_RE);
if (!match) continue;
const source = match[1].trim();
if (!source || /^HEAD@/.test(source) || /^[0-9a-f]{7,40}$/i.test(source)) {
// Bare `HEAD` (`git switch -c` from the current branch) and `HEAD@{...}`
// (detached start) both lack a named source; a raw commit hash does too.
if (!source || /^HEAD(@|$)/.test(source) || /^[0-9a-f]{7,40}$/i.test(source)) {
return null;
}
return source;
@@ -1354,6 +1354,14 @@ describe('parseBranchCreationSource', () => {
expect(parseBranchCreationSource(reflog)).toBeNull();
});
it('returns null when the branch was created from the current HEAD without a named source', () => {
// `git switch -c <branch>` / `git checkout -b <branch>` from the current
// branch record `branch: Created from HEAD` in the reflog (git 2.x). The
// source branch name is not recorded, so no base can be derived from it.
const reflog = 'branch: Created from HEAD';
expect(parseBranchCreationSource(reflog)).toBeNull();
});
it('returns null when the branch was created from a raw commit', () => {
const reflog = 'branch: Created from 9a3b2c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b';
expect(parseBranchCreationSource(reflog)).toBeNull();