Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebase local branch with upstream branch

Chain of events after forking a repo:

  1. Created a branch
  2. Made some changes
  3. Submitted PR of my branch to upstream master
  4. Repo owner creates a branch on his repository and merges my PR along with some other changes.

I still have work to do on the PR, but now I have to rebase my branch on the branch that the repo owner created to test some things before my branch can be merged with his master.

How can I rebase the branch in my local repo with the branch created in the remote? I basically want one of my branches to be the same as one of the branches in the upstream repo (not master).

Apologies in advance for the potentially poor use of terminology.

like image 850
pbreach Avatar asked Dec 25 '22 03:12

pbreach


2 Answers

How can I rebase the branch in my local repo with the branch created in the remote? I basically want one of my branches to be the same as one of the branches in the upstream repo (not master).

The usual way:

git rebase upstream/the_branch

This will change the history of your branch that you used in the PR. But that's fine. Since your PR is not accepted yet, and as you said it needs more work, it's ok to rewrite its underlying branch, when you're done, with:

git push -f origin yourbranch_for_pr

As a side note, after this, in the PR you will see all your changes made before the rebase, and after, but also the other changes made by the repo owner if those changes were not yet merged into master. It will be ideal if the repo owner merges his changes into master first, and then your PR will contain only your work, which will be more clear.

like image 65
janos Avatar answered Jan 16 '23 18:01

janos


If you want to sync your branch with upstream and you have really no commits locally that you want to keep, the easier is to reset:

git reset --hard upstream/the_branch

You could also specify directly a sha1.

like image 33
Philippe Avatar answered Jan 16 '23 18:01

Philippe