Question

Is there clever workflow to create a pull_request branch on a fork that is diverged from an upstream repo, so that the pull_request branch always perfectly matches the upstream "master"?

That is,

1) I have a fork which has diverged considerably from the parent remote.

2) I want to keep my fork (no sense in deleting it) but, I also want to cleanly issue pull requests which are minimally divergent from the original upstream repository.

Thus - is there a way to create a branch in my fork which is a perfect replica of another remote's master?

Was it helpful?

Solution

First of all, you shouldn't be modifying your local master branch, you should only be creating feature branches. If you keep your local master unmodified, then you can fetch the latest changes from the upstream repo:

git remote add upstream <url-for-upstream-repo>
git fetch upstream

Now your remote-tracking branch upstream/master is synced with the latest changes from upstream. Optionally, you can also update your local master if you want:

git checkout master
git merge upstream/master

Now you can create feature branches off of upstream/master:

git checkout -b feature upstream/master

When you want to sync your feature branches with the latest changes from upstream, you can use rebase, as long as no one else is also working on the feature branch (otherwise you'll end up forcing them to re-sync with the changed history). rebase is actually ideal for this workflow, because you can usually use it to sync feature branches as frequently as you want without creating a messy history, because they don't create merge commits:

git fetch upstream
git checkout feature
git rebase upstream/master

You could also git merge upstream/master instead of rebasing, but you'll leave behind a merge commit, so over time you'll create a more complicated history by merging instead of rebasing.

When you're ready to submit a pull-request, just push to your origin and make the request against the upstream master.

The original poster asks:

[I]s there a way to create a branch in my fork which is a perfect replica of another remote's master?

As long as your local master hasn't diverged from upstream/master (which it shouldn't if you've been doing your work in feature branches instead of directly in master), then just push your local master to your origin:

git push origin master

Alternatively, you can use a refspec to push your remote-tracking branch upstream/master to your origin/master:

git push origin upstream/master:master

OTHER TIPS

To completely replace fork with origin:

git remote add upstream <upstream_repository_url>
git fetch upstream
git reset --hard upstream/master

Got this from here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top