Git: How To Automatically Create Upstream Branches - Adam Johnson
Git: How To Automatically Create Upstream Branches - Adam Johnson
You started a new feature branch, worked hard on initial commits, and you’re ready to
send it for review. You try to push and:
$ git push
fatal: The current branch cheese has no upstream branch.
To push the current branch and set the remote as upstream, use
Oh no! You forgot to use --set-upstream to create the remote branch again.
You can copy-paste Git’s suggested command there, and carry on. Or, you can follow the
hint and configure Git to always automatically create the branch, and never see this
message again.
The push.autoSetupRemote option and the corresponding hint text were added in Git
2.37, which was released months ago in June 2022. To enable the option, run:
[push]
autoSetupRemote = true
From then on, git push on new branches will automatically create the branch:
$ git push
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 175 bytes | 175.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'cheese' on GitHub by visiting:
remote: https://github.com/adamchainz/example/pull/new/cheese
remote:
To github.com:adamchainz/example.git
* [new branch] cheese -> cheese
branch 'cheese' set up to track 'origin/cheese'.
This option is not enabled by default, because it only makes sense if you’re using a
centralized Git workflow. But you probably are, with a host like GitHub.
Fin
Thanks to Tao Klerks for authoring this feature in commit 05d5775 and Junio C Hamano
for reviewing it. And thanks to James Ide for advertising it on Twitter.
—Adam
Improve your Django develompent experience with my new book.
Related posts:
Tags: git