Repository navigation
Description
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
- Create a local branch which tracks another branch.
- Make commit(s).
- Check the upstream, just to confirm:
git rev-parse --abbrev-ref --symbolic-full-name 'HEAD@{upstream}' - Make a PR:
gh pr create - Check the upstream again:
git rev-parse --abbrev-ref --symbolic-full-name 'HEAD@{upstream}' - 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
babakks commentedon Aug 27, 2025
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 createperformsgit 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 upgradeghto 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.Pushhere:cli/pkg/cmd/pr/create/create.go
Line 1218 in 21b08bd
However, we need to make sure there are no other code paths that end up
git-pushing.williammartin commentedon Aug 27, 2025
gh repo create --sourcewill push the current branch and tracking will be setup.[-]`gh pr create` modifies the `upstream` ref o fthe branch[/-][+]`gh pr create` modifies the `upstream` ref of the branch[/+]babakks commentedon Aug 27, 2025
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.
bugfood commentedon Aug 27, 2025
Thanks. Yes, for a workaround I have been using this alias:
Debian's
ghpackage 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 compileghlocally, when the time comes.Thanks,
Corey