Skip to content

gh pr create modifies the upstream ref of the branch #11503

Open

Description

@bugfood

Describe the bug

When creating a PR via gh pr create, the upstream reference for the current branch is altered.

For context, upstream is set by either:

  • creating a new branch via: git checkout -b <new-branch> -t <other-branch>
  • setting uptream on the current branch via: git branch --set-upstream-to=<other-branch>

Affected version

$ gh version
gh version 2.46.0 (2025-01-13 Debian 2.46.0-3)
https://github.com/cli/cli/releases/tag/v2.46.0

Steps to reproduce the behavior

  1. Create a local branch which tracks another branch.
  2. Make commit(s).
  3. Check the upstream, just to confirm: git rev-parse --abbrev-ref --symbolic-full-name 'HEAD@{upstream}'
  4. Make a PR: gh pr create
  5. Check the upstream again: git rev-parse --abbrev-ref --symbolic-full-name 'HEAD@{upstream}'
  6. Note that upstream is now different.

Expected vs actual behavior

As a user of the gh tool, I expect that that a command called pr create will create a PR on github--no more, no less. I do not expect modifications to my local git repository (at least not by default).

$ gh pr create --help | head -n 2
Create a pull request on GitHub.

(I I truncated that, but the full output doesn't state anything about local repo modification)

Currently, gh is modifying the upstream. This was confusing for me at first, since the results are not immediately apparent. I was only finding that the upstream was incorrect when I later ran something which depended on upstream being set.

Logs

$ git checkout -b gh-example-branch -t origin/master
branch 'gh-example-branch' set up to track 'origin/master'.
Switched to a new branch 'gh-example-branch'

$ echo foo > bar

$ git add bar

$ git commit bar -m 'example commit'
[gh-example-branch 04d36eb529] example commit
 1 file changed, 1 insertion(+)
 create mode 100644 bar

$ git rev-parse --abbrev-ref --symbolic-full-name 'HEAD@{upstream}'
origin/master

$ gh pr create
? Where should we push the 'gh-example-branch' branch? [redacted]

Creating pull request for gh-example-branch into master in [redacted]

? Title test pr
? Body <Received>
? What's next? Submit
remote: 
remote: 
To github.com: [redacted].git
 * [new branch]            HEAD -> gh-example-branch
branch 'gh-example-branch' set up to track 'origin/gh-example-branch'.
https://github.com/[redacted]/pull/1280

$ git rev-parse --abbrev-ref --symbolic-full-name 'HEAD@{upstream}'
origin/gh-example-branch

I suspect this is due to git push --set-upstream as specified here:
https://github.com/cli/cli/blob/1b083c2/git/client.go#L808
...and called here:
https://github.com/cli/cli/blob/1b083c2/pkg/cmd/pr/create/create.go#L1218

I do not know if the current behavior is due to use of the common Push function, which potentially had --set-upstream for another purpose, or if some people's workflows actually benefit from modifying the upstream. This behavior is harmful for my own workflow, though. I would prefer if changing upstream were to not happen by default, or at least could be disabled via an argument.

Thanks,
Corey

Activity

  1. added
    bug
    on Aug 13, 2025
  2. added
    needs-triage
    on Aug 13, 2025
  3. added
    needs-investigation
    gh-pr
    on Aug 27, 2025
  4. self-assigned this
    on Aug 27, 2025
  5. babakks commented on Aug 27, 2025

    @babakks
    Member

    Thanks for reporting this and pointing to the underlying piece of code, @bugfood! 🙏

    @williammartin and I looked into this and we can confirm there's a bug here. The tracking branch should not change when pr create performs git push.

    That said, a workaround is to push the branch before running pr create. In that case the tracking ref is kept as it is. Could you please try it and let me know if that works for you? Also please upgrade gh to the latest version since there have been lots of changes since v2.46.0 that you run.

    Note on the potential fix

    It might be a simple check for upstream tracking, if any, and reuse that when calling GitClient.Push here:

    if err := ctx.GitClient.Push(root, headRemote.Name, ref, git.WithStderr(w)); err != nil {

    However, we need to make sure there are no other code paths that end up git-pushing.

  6. added
    priority-2
    core
    and removed
    needs-investigation
    needs-triage
    on Aug 27, 2025
  7. removed their assignment
    on Aug 27, 2025
  8. williammartin commented on Aug 27, 2025

    @williammartin
    Member

    However, we need to make sure there are no other code paths that end up git-pushing.

    gh repo create --source will push the current branch and tracking will be setup.

  9. changed the title [-]`gh pr create` modifies the `upstream` ref o fthe branch[/-] [+]`gh pr create` modifies the `upstream` ref of the branch[/+] on Aug 27, 2025
  10. babakks commented on Aug 27, 2025

    @babakks
    Member

    gh repo create --source will push the current branch and tracking will be setup.

    That seems to be an okay case, because it doesn't make sense to have the branch tracking another repo/remote while there's no repo.

  11. bugfood commented on Aug 27, 2025

    @bugfood
    Author

    Thanks. Yes, for a workaround I have been using this alias:

        # git ghprc
        # Create github pr with defaults suitable for use in a non-forking
        # development model.
        ghprc = "!f() { set -e ; git push ; gh pr create --head \"$(git show-branchname)\"; } ; f"
        # git show-branchname
        # Show the current branch.
        show-branchname = "rev-parse --abbrev-ref HEAD"
    

    Debian's gh package is still on 2.46.0.3 and the workaround is ok there. If you have a fix you would like me to test, I should be able to compile gh locally, when the time comes.

    Thanks,
    Corey

  12. self-assigned this
    on Oct 2, 2025
  13. added
    priority-3
    and removed
    priority-2
    on Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugcoregh-prpriority-3

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions