Greg McGuffey
Greg McGuffey

Reputation: 3316

How do you update upstream with changes from fork?

I have a main repository A, that I forked to repository B. I've made some changes in B that I want to move up to A. I'm having a hard time figuring out how to do this.

First, what are the git commands to do this?

Second, can I do a pull request in github to facilitate this? How would that work?

Finally, I actually had a branch in B that I wanted to merge. Should I merge the branch in B back into my master branch before doing merge/pull request back to upstream repository (A)?

Upvotes: 0

Views: 57

Answers (1)

Sébastien Dawans
Sébastien Dawans

Reputation: 4616

There are two main ways of doing this

  1. Merge client side and interact with multiple remotes
  2. Merge server side with Pull Requests, and only interact with 1 remote

Before you read on I'd just like to say that your question is relatively open-ended and there is not a single best answer to it. The following answer just reflect some of the workflows I've used to give you some insight.

Client Side

Supposing you are working on a clone of B, you can add A as a second remote:

git remote add repo_a <repo A URL>

repo_a is an alias for your remote repository, just as origin is likely your currrent alias for remote B. Let's rename origin to repo_b for the sake of clarity:

git remote rename origin repo_b

List your remotes:

git remote -v

Fetch stuff from both remotes

git fetch repo_a
git fetch repo_b

At this point, you can create local branches to track branches from both remote repositories without any restrictions on which way to merge, or what to merge. How you define your workflow will add certain constraints.

If, say, you'd like to merge changes from Repo B's feature_1 branch into repo_a's master branch, you could do:

git checkout -b master_a repo_a/master --track
git merge feature_1
git push repo_a master_a:master

You're biggest friend is gitk or any other graphical tool that will display local branches and remote branches in the same view. I suggest opening a detached gitk and refreshing it as often as you need. Don't forget the --all option.

gitk --all &

Server Side (Pull Requests)

In this next example, the client-side commands take place in a local clone of the repo_b. We will push features to repo_b on GitHub and use pull requests to merge.

When I work with pull requests I like to have repo_b's master branch always in sync with the parent repository and it's common to use upstream and origin in repo_b's client side clone:

git remote add upstream <repo A URL>

Reset your master branch to the upstream:

git fetch upstream
git fetch origin
git checkout master
git reset --hard upstream/master
git checkout -b feature_2

Implement your feature and push to Github:

git push origin feature_2

Now in the web interface you should have the option to submit a pull request when viewing that branch. I won't go through the details but it should be intuitive enough. Select repo_b/feature_2 as source and repo_a/master as destination.

When the merge is done, you can update your master branch in your clone of repo_b:

git fetch origin
git checkout master
git merge origin/master

Upvotes: 1

Related Questions