Bug Fix·Pushed May 1, 2026·S
Default branch fallback added
GitHub Actions workflows can now resolve the default branch even when origin/HEAD is unavailable after a fresh checkout.
GitHub Actions workflows now handle a common edge case more gracefully. When running after a fresh [[code]]actions/checkout[[/code]], the [[code]]origin/HEAD[[/code]] symbolic reference isn't always available, causing the default branch resolver to fail. A fallback to [[code]]HEAD[[/code]] has been added so the action works in more scenarios without crashing.
The change makes the code more resilient by catching the git command failure and returning a sensible default instead of propagating an exception. Workflows relying on default branch detection will now complete successfully even after minimal checkouts.
Technical description
The [[code ref=1]]defaultBranch[[/code]] function in [[code]]action/src/git.ts[[/code]] now gracefully handles the case where [[code]]origin/HEAD[[/code]] is unavailable.
Previously, the function ran [[code]]git symbolic-ref --short refs/remotes/origin/HEAD[[/code]] and let any exception propagate up. This caused failures in GitHub Actions after minimal checkouts where the remote HEAD reference hadn't been populated yet.
The fix wraps the command in a try/catch block. When the git command fails, the function returns [[code]]'HEAD'[[/code]] as a fallback instead of throwing. The [[code]]stdio[[/code]] option was also adjusted to suppress stderr during the command execution.
````diff
file=action/src/git.ts
export function defaultBranch(repoDir: string): string {
- return execSync('git symbolic-ref --short refs/remotes/origin/HEAD', {
- cwd: repoDir,
- encoding: 'utf8',
- })
- .trim()
- .replace(/^origin\//, '');
+ try {
+ return execSync('git symbolic-ref --short refs/remotes/origin/HEAD', {
+ cwd: repoDir,
+ encoding: 'utf8',
+ stdio: ['ignore', 'pipe', 'ignore'],
+ })
+ .trim()
+ .replace(/^origin\//, '');
+ } catch {
+ return 'HEAD';
+ }
}
````
This is a defensive improvement that prevents workflow failures in scenarios where the remote HEAD reference is not yet available.
**Files at a Glance**
- [[code]]action/src/git.ts[[/code]]: Added fallback to [[code]]'HEAD'[[/code]] when [[code]]origin/HEAD[[/code]] symbolic ref is unavailable
Categories
- Bug Fix (80%) — Fixes a failure mode where the action would crash when origin/HEAD is unavailable after a fresh actions/checkout
- Maintenance (20%) — Improves robustness by adding a graceful fallback instead of propagating exceptions