1

I forked branch A and created B. Now, A has been updated by others and I'd like to bring those commits over to my B fork, so I can ensure the stuff I've been doing there still works with that new A content.

How can I do this? Git's terminology (pull, fetch, merge, etc.) is hugely unintuitive, at least early on :(

Luke
  • 9,512
  • 15
  • 82
  • 146

1 Answers1

2

I would advise (if none has already pulled from B):

What you have is a Branch A with new evolution and possible commits done in the upstream repo:

        a--a--a (origin/A)
       /
a--a--a A
 \
  b--b--b (B, local branch) 

First make sure A is up-to-date with the upstream repo content: origin/A:

git checkout A
git pull

That will give you:

a--a--a--a--a--a (A, origin/A)
 \
  b--b--b (B, local branch) 

Then You would rebase your local modifications done on B on top of A (I suppose here that A has an upstream branch, meaning it does track origin/A, which you can check with git branch -avvv)

git checkout B
git rebase A

Which gives you:

a--a--a--a--a--a (A, origin/A)
                \
                 b'--b'--b' (B, local branch) 

Note that changes the history of B, so if B was already pushed, you would have to push --force origin B, which can be dangerous if others already started working on B.
See (for more on the rebase tricks):


Note: "fork a branch" isn't the recommended expression, since fork is more commonly used to refer to a cloned repo on the server side: see "Git fork is git clone?".

Instead, you would say: "I branched A and created B": the operation is "branching" (not "forking").

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Won't that rebase modify A? – Luke Jul 18 '13 at 08:23
  • 1
    @lukech the rebase **will not** modify A, it will recreate B on top of A. – VonC Jul 18 '13 at 08:25
  • @lukech I have added a few schema to better explain the nature of the rebase operation. – VonC Jul 18 '13 at 08:31
  • @lukech note, avoid the term 'fork' when making a new branch; It is simply called "branching". A fork is reserved for a cloned repo on the server side: http://stackoverflow.com/a/6286877/6309 – VonC Jul 18 '13 at 08:46
  • See what I was saying about the terminology being confusing? :) – Luke Jul 18 '13 at 09:53