gh-stack icon indicating copy to clipboard operation
gh-stack copied to clipboard

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

Open jwilkerson128 opened this issue 3 months ago • 6 comments

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.go BranchExists calls client.HasLocalBranch(...) — it never checks refs/remotes/<remote>/<name>.
  • cmd/init.go's resolveArgBranches treats "not found locally" as "doesn't exist," and runs git.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:227 then unconditionally force-pushes every branch (git.Push(..., force=true, ...), using --force-with-lease under the hood) — the lease only guards against a concurrent change by someone else, not against gh-stack pushing an unrelated commit on purpose.

Repro

  1. feature-x exists on origin with real commits and an open PR, based on main.
  2. Without ever creating a local branch named feature-x, branch off it under a different name: git checkout -b my-work origin/feature-x.
  3. gh stack init feature-x my-work → prints "Adopted 2 branches" but actually ran git branch feature-x main for feature-x.
  4. gh stack submit → force-pushes the empty feature-x to origin, wiping its real commits. GitHub auto-closes feature-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.

jwilkerson128 avatar Aug 28 '26 17:08 jwilkerson128

I just spent over an hour cleaning up the fallout from this problem =(

EDIT (explanation for why it took this long to recover):

Branches are created from the previous branch in the stack, not from trunk. gh stack init -b a b c d created each missing branch from the one before it — the reflogs each show a single entry: - b@{0}: branch: Created from a - c@{0}: branch: Created from b - d@{0}: branch: Created from c

So the whole stack collapses onto one commit and every PR ends up with head == base, not merely empty against trunk. .git/gh-stack recorded head == base == for all of them.

Restoring the branches is not sufficient — the PRs cannot be reopened. GitHub freezes a closed PR's head/base snapshot and evaluates reopen against that, ignoring live refs. After force-pushing the real commits back:

gh pr reopen → Could not open the pull request; REST gives 422 … There are no new commits on the branch (the branch demonstrably had commits the base did not) changing the base → 422 Cannot change the base branch of a closed pull request toggling draft → silent no-op The only way out we found is to make each head a descendant of the collapsed commit — merging the stack upward, bottom to top, pushing each — after which reopen succeeds with all review history intact.

I am gonna go and turn on --force protection for all our branches...

AreWeDreaming avatar Sep 01 '26 01:09 AreWeDreaming

Hit this with gh stack add too (v0.1.1). My colleague's branch only existed as origin/<branch> after a fetch, so gh stack add <branch> made a fresh one off the stack tip and the next push force-pushed straight over their commits 😬 Looks like the same local-only BranchExists check, so hopefully one fix covers both. It's really easy to hit because git checkout <branch> would just pick up the remote branch here.

If you've been bitten, it's recoverable. GitHub's activity API records force pushes with before/after SHAs, even after the branch is deleted:

gh api "repos/OWNER/REPO/activity?ref=refs/heads/<branch>" \
  --jq '.[] | [.timestamp, .activity_type, .before, .after] | @tsv'

The before on the force_push entry is the lost tip. If you'd fetched it, it's likely still local. Otherwise git fetch origin <sha> usually works for a while. Then git branch <branch> <sha> and push it back.

elyobo avatar Oct 02 '26 23:10 elyobo

This bug is an absolute land mine. It is possible to prioritize?

mortona42 avatar Oct 12 '26 18:10 mortona42

My team has stopped using stacks as a result of this. Seems like it has potential to be a nice feature, but we don't feel the benefits outweigh the risks yet.

jwilkerson128 avatar Oct 12 '26 18:10 jwilkerson128

I don't mean to be a complainer here, but this tool has screwed me over many times, in completely unexpected ways.

I am getting the impression that this is a community built tool, even though github is a paid service. The quality and development pace lags behind my expectations for what we are paying for. This is a real conversation happening among developers and colleagues.

This comment is not directed towards any devs, but to the people managing product and resource decisions.

mortona42 avatar Oct 12 '26 18:10 mortona42

Conceptually, "stacks" are a good idea, and potentially helpful. However, this bug has destroyed work, so I would advise people avoid the "stack" feature at this time. This bug could be disasterous for others.

bdanin avatar Oct 12 '26 20:10 bdanin