Istopopoki
Istopopoki

Reputation: 1762

Merge a pull request on github and keep history aligned

My question concerns Github :

I want to be able to keep the fork and the upstream identical, meaning "with the same commits history".

If a commit is made on the upstream repo, I have a message in the fork saying that the fork is now 1 commit behind the upstream.

I create a PR from the upstream into the fork, and merge it. I have tried the different merge strategies, but let say I merge it using the "rebase" technique.

At this point, what I expected to have is a fork up-to-date with the upstream. It is the case, if you look at the repo content, but in term of commit history, the fork is now 1 commit behind, and 1 ahead the upstream. In fact we are talking about the same commit, but its sha changed during the merge process. So Github consider those are 2 different commits.

This wouldn't bother me, except that now, I can't repeat the process. If a new commit is made to the upstream, I create again a PR towards the fork, but this PR now has merging conflicts that cannot be solved automatically !

How can I keep my fork up-to-date with the upstream (without deleting it every time) ?

Edit : I should have been more precise, I am hoping to find a workflow with the Github API only, and without any local git repo / git CLI involved.

Upvotes: 2

Views: 1099

Answers (1)

Julian
Julian

Reputation: 36840

This is a common problem andere there are some ways to prevent recreating the fork when using git locally.

Most important is you never change your the content of the main branch (e.g. master, main or maybe dev - I assume main here), as you like to keep them fully identical to the original source. So every feature needs in a feature branch.

So the flow will work:

  1. Create a new branch on your fork (maybe first locally), add some changes/commits
  2. Create a PR to the original github repo.
  3. After merge of the PR, update your main branch to have it identical to main branch of the original source. See steps below.

Update your main branch to have it identical to main branch

For this you need first also a connection from your local clone to the original source.

Initial setup

When cloning your fork locally you have the, fork as origin remote:

git remote -v

Will return something like

origin  https://github.com/YourUserName/Repo.git (fetch)
origin  https://github.com/YourUserName/Repo.git (push)

You need first also the remote of the original source, commonly called upstream. This need to be done only once for each clone.

git remote add upstream https://github.com/Original/Repo.git

Now git remote -v will return:

origin    https://github.com/YourUserName/Repo.git (fetch)
origin    https://github.com/YourUserName/Repo.git (push)
upstream  https://github.com/Original/Repo.git (fetch)
upstream  https://github.com/Original/Repo.git (push)

Update your main branch

Now after the main branch from the original repo has been updated, you need to do:

git checkout main 
git fetch upstream
git merge upstream/main --ff-only
git push

This will do:

  • move to main branch
  • fetch the source of the upstream remote
  • merge with the main of the upstream and with fast forward only option. This ensures there are no other
  • push it to your fork (origin)

When there are changes in your main, the merge will fail because of the fast forward option. To fix this, I think this answer is better for that

Upvotes: 1

Related Questions