loading...

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

twitter logo github logo ・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

Then I push to their repo like this...

git push andy my-branch-name:his-branch-name

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?

twitter logo DISCUSS (10)
markdown guide
 

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.

 

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

 

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.

 

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.

 

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.

 

If I understand correctly you want to add commits to another person's PR on your repo from their forked repo? I wrote an article about that dev.to/codypearce/how-to-add-commi...

git push git@github.com:remoteUser/remoteRepo localBranch:remoteBranch
 

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

 

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...

 

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

 

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.

Classic DEV Post from Jul 20 '19

What Makes You a Great Programmer on The Team?

Majority of software developers are aspired to be not only a competent professional but also a great one.

Ben Halpern profile image
A Canadian software developer who thinks he’s funny. He/Him.