gh stack init silently recreates an existing branch from trunk (instead of adopting it) when only a remote-tracking ref exists, and submit force-pushes over the real branch, destroying its history and closing its PR
@skarim is already working on this.
Since Sep 2, 2026.
Assessment
This issue has not been assessed yet.
Description
Environment
- gh-stack v0.1.0
- gh 2.97.0
- git 2.50.1
- macOS
Summary
gh stack init <branch> treats a branch as "new" (and creates it fresh from trunk) if no local branch of that exact name exists — even when a remote-tracking ref for it does exist, with real commits and an open PR behind it. gh stack submit then force-pushes that empty, trunk-tip branch over the real remote branch, silently deleting its commits and causing GitHub to auto-close its PR (empty diff → nothing to merge).
Root cause (traced in source)
internal/git/gitops.goBranchExistscallsclient.HasLocalBranch(...)— it never checksrefs/remotes/<remote>/<name>.cmd/init.go'sresolveArgBranchestreats "not found locally" as "doesn't exist," and runsgit.CreateBranch(name, parent)(parent = trunk for the bottom-most branch) — i.e.git branch <name> <trunk>, discarding any relationship to the real branch/PR of the same name.- The success message still prints "Adopted N branches" regardless of whether a branch was actually adopted or freshly (and destructively) created — no warning is shown.
cmd/submit.go:227then unconditionally force-pushes every branch (git.Push(..., force=true, ...), using--force-with-leaseunder the hood) — the lease only guards against a concurrent change by someone else, not against gh-stack pushing an unrelated commit on purpose.
Repro
feature-xexists onoriginwith real commits and an open PR, based onmain.- Without ever creating a local branch named
feature-x, branch off it under a different name:git checkout -b my-work origin/feature-x. gh stack init feature-x my-work→ prints "Adopted 2 branches" but actually rangit branch feature-x mainforfeature-x.gh stack submit→ force-pushes the emptyfeature-xtoorigin, wiping its real commits. GitHub auto-closesfeature-x's PR since the diff is now empty.
Impact
Silent, unwarned history loss on a shared branch with real, reviewed work behind it. In our case the original commits happened to still be reachable via another local branch built on top before the incident, so we could restore them — anyone without that would have lost the work (short of GitHub's own object retention).
Suggested fix
Before classifying a branch as new in init, also check for a remote-tracking ref (or fetch first); if one exists, create the local branch from it (git branch <name> <remote>/<name> or equivalent) instead of from trunk. Separately, submit/push force-pushing should sanity-check that it isn't collapsing a branch's diff to zero relative to what's currently on the remote, or at minimum surface a loud warning before doing so.
- Dominant language
- Go
- Stars
- 1.5k
- Forks
- 73
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 7
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from github/gh-stack
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
feature request topic: cli - general
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
feature request topic: auto-merge
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
bug topic: docs
Difficulty 1/5 Under an hour Newbie friendliness 68/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NVIDIA/gpu-operator#2955 ·
-
agentic-workflows
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
kovidgoyal/kitty#10516 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 90/100
cisagov/vulnrichment#337 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100