Th3B0Y
Th3B0Y

Reputation: 994

Create new branch from a repository which was forked before

I have forked a public repository A into B. A has a branch called "dev" which is also in B, where I committed to. Now unrelated changes to A need to be done so that I am able to make a PR from B.

How can I get a new "dev" branch into B so that I can commit the needed changes before I send the PR from B ?

To simplify, the scheme is like this:

A

B

Upvotes: 1

Views: 2130

Answers (1)

battlmonstr
battlmonstr

Reputation: 6300

When you fork B, and then clone B and work with it locally, you essentially have 3 separate repositories: A, B and your local C.

In your local repository C you can add A and B as "remotes". Check git remote -v to see which remotes you have. I would give them descriptive names at this point. You can rename a remote and add a new remote to your local repo, let's say: A = origin, B = fork.

If you add these remotes and run git fetch you'll get 3 distinct branches: dev (your local dev), origin/dev ("dev" on A), and fork/dev ("dev" on B).

Now, at this point to avoid confusion I would give your local branches dev and newdev more descriptive names that reflect the work you are doing, and use these names instead. For example if dev has feature foo implemented, and newdev is going to have feature bar implemented, you can do:

# make "foo" branch to refer locally to the "dev" state on B
git branch foo fork/dev 
# make "bar" branch to refer locally to the "dev" state on A
git branch bar origin/dev

Now you can git checkout bar, implement your feature, push it to fork (aka B) with git push fork, go to github and make your PR. The interface will let you choose your source branch (bar) and your destination branch (origin/dev).

Upvotes: 1

Related Questions