Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the standard way of keeping a fork in sync with upstream on collaborative projects?

Newbie open-source contributor here.

I forked the TortoiseGit repository on GitLab, then cloned it on my computer, edited one file, and committed to branch master.

A few days have passed and I want to update my local working copy with the latest changes from upstream, before pushing to my remote fork and opening a merge request (and of course doing more development/testing etc).

I added a remote called upstream to my repo and now I'm not sure what would be the recommended action:

  1. git pull from upstream/master to my checked-out branch master
  2. git pull --rebase //
  3. git fetch followed by git rebase.

These are the approaches I found during my research. Unfortunately I could not find a comprehensive review of each, nor a recommendation as to which one is typical practice when working in projects from GitHub, GitLab or even those like the Linux kernel.

I tried methods 1 and 3. Method 1 (pull) generates a merge commit (--ff-only is not possible) and my history is, in a way, polluted. It also creates conflicts. Method 3 (rebase) does neither, but I'm not sure how rebase behaves after commits are pushed to remote and so I'm afraid it might cause problems going forward.

So there's my question.
Thank you.

like image 429
Marc.2377 Avatar asked Apr 03 '19 18:04

Marc.2377


People also ask

How do you keep your fork in sync with an upstream repository?

Syncing a fork branch from the web UI On GitHub, navigate to the main page of the forked repository that you want to sync with the upstream repository. Select the Sync fork dropdown. Review the details about the commits from the upstream repository, then click Update branch.

How do you fork fetch upstream?

Go to your fork, click on Fetch upstream , and then click on Fetch and merge to directly sync your fork with its parent repo. You may also click on the Compare button to compare the changes before merging.


2 Answers

TortoiseGit team member here.

I added a remote called upstream to my repo and now I'm not sure what would be the recommended action:
1. git pull from upstream/master to my checked-out branch master
2. git pull --rebase //
3. git fetch followed by git rebase.

Different team uses different workflow.
See Pro Git (v2) - 5.1 Distributed Git - Distributed Workflows

In TortoiseGit team, we prefer keep the history simple, and contributor usually has the responsibility for resolving the conflicts while rebasing.

So, most of the time, we use "git fetch followed by git rebase", especially when contributing. Then, as you said, creating pull/merge request (by using git push), or updating the pull/merge request (by using git push with force) on GitHub/GitLab.

See How Can I Contribute? and HowToContribute.txt for other detail information.

like image 122
Yue Lin Ho Avatar answered Nov 10 '22 17:11

Yue Lin Ho


I forked the TortoiseGit repository on GitLab, then cloned it on my computer, edited one file, and committed to branch master A few days have passed and I want to update my local working copy with the latest changes from upstream

If by upstream you're referring to the main repo, you can

  1. git fetch origin
  2. git rebase origin/master

assuming origin points to the main repo

Rebasing replays the commits of your current branch (your local master branch) on top of the branch you're rebasing origin/master in this case, which is a reference to the current master branch of origin.

For example:

master branch before your commit:

  • c
  • b
  • a

master branch after your commit:

  • xxx <- your commit
  • c
  • b
  • a

After a few days, TortoiseGit git commits to origin/master which now looks like this.

  • 3
  • 2
  • 1
  • c
  • b
  • a

You sync the changes by fetching and rebasing on top of origin/master. Your local master branch will now look like this:

  • xxx <- your commit
  • 3
  • 2
  • 1
  • c
  • b
  • a

I added a remote called upstream to my repo and now I'm not sure what would be the recommended action

then you can simply git push upstream master to update your fork.

like image 45
Noel Avatar answered Nov 10 '22 17:11

Noel