Skip to content

git commit --amend on shallow clone pollutes diff #344

@sentry-junior

Description

@sentry-junior

Junior corrupted a PR by running git commit --amend on a shallow clone checkout, causing the amended commit to include every file in the repo as new additions (139 files changed).

Sequence

  1. Shallow-cloned getsentry/skills to branch main
  2. Created branch, made targeted edits (2 files), committed
  3. Pushed branch, opened PR — diff was correct (+6/-2)
  4. Needed to revise the commit — ran rm -rf on the clone dir
  5. Re-cloned with --depth=1 -b jr/fix-placeholder-issue-refs (shallow clone of the feature branch)
  6. Made edits, ran git commit --amend --no-edit
  7. The shallow clone had no shared history with main, so the amend treated every tracked file as new in the commit
  8. Force-pushed — PR diff ballooned to 22k+ insertions across 139 files, PR was closed

Root cause

git commit --amend on a depth-1 clone of a feature branch produces a commit whose tree diff against main includes every file, because the shallow clone lacks the merge-base. The correct approach is either:

  • Re-clone from main (not the feature branch), recreate the branch, and make a new commit
  • Or avoid amending entirely — just add a fixup commit

Proposed guardrail

Before running git commit --amend or git rebase, verify the clone has sufficient history to compute a clean diff against the PR base branch. If depth=1, either deepen (git fetch --deepen) or use a new commit instead of amending.

Observed in getsentry/skills#147 (closed).

Action taken on behalf of David Cramer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions