DEV Community

loading...

Push to a forked pull request branch on GitHub—Am I doing this right?

ben profile image Ben Halpern ・1 min read

I want to ask about something I know how to do, but I'm not sure if there's a more straightforward way to do it.

Let's say I want to collaborate on a pull request with @andy , this would be my flow...

First I add Andy's repo...

git remote add andy https://github.com/Zhao-Andy/dev.to
Enter fullscreen mode Exit fullscreen mode

Then I push to their repo like this...

git push andy my-branch-name:his-branch-name
Enter fullscreen mode Exit fullscreen mode

It's kind of straightforward but requires me to add his repo, copy and paste his branch name, and just doesn't seem elegant.

Are there other ways to do this or is this just how you do it?

Discussion (9)

pic
Editor guide
Collapse
nikoheikkila profile image
Niko Heikkilä

You could also push with git push -u andy his-branch-name which would set the upstream correctly for future pushes.

It doesn't necessarily make it any easier, though. Perhaps, GitHub flow is not the best solution to this and you two should just open a pair-coding session with VS Code Live Share or similar tool.

Collapse
erikthered profile image
Erik Nelson

Setting the upstream branch was what came to mind for me too. You can also setup the branch to track when you initially check it out:

git checkout -b my-branch-name andy/his-branch-name

Collapse
napoleon039 profile image
Nihar Raote

I actually haven't done this many times. Just 2 times IIRC. To explain what I do using your example -

I fork Andy's repo, then add the repo on my github:

git remote add myrepo https://github.com/napoleon039/repo

I then push the changes to my own repo:

git add . && git commit -m "Commit message" && git push myrepo master

And lastly, I go to github and do a pull request

This worked for me. I didn't know there were other ways to do this. I hope I can find better ways to push a pull request to a forked repo.

Collapse
cjbrooks12 profile image
Casey Brooks

You could use hub, created by GitHub as a wrapper around the Git CLI to make it more natural to work with GitHub OSS projects. hub.github.com/

Alternatively, I find Git GUIs to be especially good at working with remotes and managing PRs, especially GitKraken. There's a lot I still do with my local Git CLI, but having a GUI for the less-frequently-used stuff is quite an invaluable tool.

Collapse
gklijs profile image
Gerard Klijs

If you like rebasing it's better to each work in your own forked branch, and periodically rebase your fork on the other ones. Ideally you never need to do this, as the stories would be small enough, are you would pair instead.

Collapse
jenc profile image
jen chan

Was just wondering this and also thinking, a fork is really just another branch right?

Collapse
brandinchiu profile image
Brandin Chiu • Edited

Essentially, yes.

You get a personal copy of the repo, but can leave it attached to the source to quickly and easily pull their changes into your copy or push yours to theirs:

github.community/t5/Support-Protip...

Collapse
coolshaurya profile image
Shaurya • Edited

A fork is not a branch, but a clone of the original repo with some github sugar added.

Collapse
miniscruff profile image
miniscruff

A bit different but you could create another copy and clone the forked project side by side. I used to do this with SVN all the time. Not really required for git but sometimes helpful.