I forked a repository on GitHub and set it as my origin remote. The original project (which I call upstream) just created a new branch that I need in my fork.
When I try to create a local branch based on the upstream branch like this:
The problem is that when I run git push, it tries to push back to the upstream repository instead of my fork. This doesn’t work because I don’t have write access to the upstream repo.
What I actually want is to create a local branch that tracks the new upstream branch but pushes to my fork by default. How can I set this up properly so that I can contribute changes back through my forked repository?
When you checkout from upstream and create a local branch, Git sets the upstream reference automatically - which makes your push target the wrong repo. Here’s the cleanest fix: first run git fetch upstream, then git checkout -b new-feature upstream/new-feature, followed by git push -u origin new-feature. You’ll get the upstream code but set the right push destination from the start. I learned this after accidentally trying to push to upstream repos I couldn’t write to. The trick is keeping where you pull code from separate from where you push changes to.
This happens because your local branch is tracking upstream instead of your fork. After you create the branch from upstream, you need to tell Git where to push. Run git branch --set-upstream-to=origin/new-feature after creating your local branch, or use git push --set-upstream origin new-feature on your first push. Git will know your branch came from upstream but pushes should go to your fork. I’ve hit this same issue tons of times with open source contributions - getting the upstream tracking right upfront saves major headaches.
u could also just run git push origin HEAD:new-feature if ya don’t wanna mess with upstream settings. it’ll push your current branch to origin w/o changing tracking configs. great for testing or if you don’t need it to track permanently.
totally get ur confusion! just remember to set the upstream after branching. do git push -u origin new-feature next time u create a new branch. it links ur local branch to ur fork, super handy! gl!