Home > Enterprise >  Git rebase with forking
Git rebase with forking

Time:07-10

I am reading this about article about forking:

https://timonweb.com/misc/how-to-update-a-forked-repo-from-an-upstream-with-git-rebase-or-merge/

Listing the following steps:

git remote add upstream https://github.com/django/django.git
  1. Download master branch from upstream:

    git fetch upstream master

  2. Overwrite your master with upstream's master via git rebase:

    git rebase upstream/master

  3. Push to master, please note --force here:

    git push origin master --force

I do not understand Step 3: If i rebase, i put my master branch on top of the upstream/master.

In step 4) i am pushing origin master, but since i only updated origin/master and put my changes on top of it, i do not understand why this would change anything.

Additionally to my confusion, it says below, that you can do everything with merging as well.

What do i understand wrong here?

CodePudding user response:

This is upstream

A->B->C

This is master

A->E->F

What git rebase upstream/master does is:

A->B->C->E'->F'

Note the ', there meaning is that the diff between E and it's parent is the same as the diff between E' and it's parent (if there was no conflict in the rebase operation), so one can say that those commit contains the same thing¹, hence we give them a similar name.

But E and E' doesn't have the same parent, and because the parent hash is used to compute the commit hash (it's a block chain, Merkel tree), E and E' doesn't have the same hash, hence are different commit, and therefore, F and F' are different commits, etc...

So basically a rebase doesn't change the (diff from parent of the) commit that are on top, but because the block chain is not the same below, the hash are not the same.

The merge introduce a node with two parents (C and F). Fun fact, git support nodes with n parents.

[1] It's a misunderstanding caused by the fact that commits are represented by diff to the parent, in fact a commit contains the full repo.

CodePudding user response:

Ah, i think i understand. I just put my commits on top of the current master.

I think, i got a bit mixed up. I interpreted the the upstream/master as a remote branch, but it is not, it is just another branch, which gets its commit from another source than origin/master.

Origin and upstream have both remote branches, but i am not rebasing on them but on their local counterpart (e.g. remote/upstream/master)

This was my confusion.

  • Related