Skip to content

[BUG]: pre-push hook misdetects remote branch existence due to substring matching #11372

@Om-A-osc

Description

@Om-A-osc

Description

pre-push hook determines whether a remote branch exists using:

git branch -r | grep "${GIT_CURRENT_BRANCH}"

This uses substring matching, which can lead to false positives when branch names share prefixes or substrings.

Observed Behavior

In certain cases, the hook reports that a remote branch exists even when the exact branch does not exist.

Prefix collision

Remote branch:

refactor/stats-base-dists-chi

Local branch:

refactor/stats-base-dists

Running:

git branch -r | grep "refactor/stats-base-dists"

matches refactor/stats-base-dists-chi, causing the hook to incorrectly assume that the remote branch exists.

This leads to:

git log origin/refactor/stats-base-dists..refactor/stats-base-dists

which fails with:

fatal: bad revision 'origin/refactor/stats-base-dists..refactor/stats-base-dists'

Related Issues

NA

Questions

Is this behavior intentional, or would it be preferable to switch to an exact ref check (e.g., using git rev-parse --verify refs/remotes/<remote>/<branch>) to avoid false positives?

Demo

No response

Reproduction

1. Create and push a branch to the remote:
   
   git checkout -b refactor/stats-base-dists-chi
   git push origin refactor/stats-base-dists-chi
   

2. Create a new local branch whose name is a prefix of the existing remote branch:
   
   git checkout -b refactor/stats-base-dists
   

3. Make a commit:
   
   git commit --allow-empty -m "test commit"
   

4. Attempt to push:
   
   git push origin refactor/stats-base-dists
   

5. Observe that the pre-push hook reports that the remote branch exists and fails with:
   
   fatal: bad revision 'origin/refactor/stats-base-dists..refactor/stats-base-dists'
   

This occurs even though `origin/refactor/stats-base-dists` does not actually exist.

Expected Results

Should push without error

Actual Results

fatal: bad revision 'origin/refactor/stats-base-dists..refactor/stats-base-dists'

Version

No response

Environments

N/A

Browser Version

No response

Node.js / npm Version

No response

Platform

No response

Checklist

  • Read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions