Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#489 1 comentario 1 reacción 1 asignado Ver en GitHub

@skarim ya está trabajando en esto.

Desde el 2/9/2026.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

bug topic: cli - init

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.

Lenguaje dominante
Go
Estrellas
1.5k
Forks
73
Merge medio
1 d 8 h
PR fusionados (30 d)
7

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de github/gh-stack

Todos los issues de github/gh-stack

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.