Basj Basj - 19 days ago 6
Git Question

How to merge specific branches with git?

Note: this is somehow linked to this question but it requires additional attention to branches.

Let's say I have a Github project https://github.com/basjj/myproject.git with 2 branches (

master
and
feature1
) and a local repository on my computer.

A user named
blah
proposed a pull-request. He has 3 branches (
master
,
feature1
,
feature2
) on https://github.com/blah/myproject.git.

Case #1



I would like to try his
feature2
branch here on my computer, slightly modify it, and merge it into my own
master
. Are the following commands correct?

git checkout -b feature2
git pull https://github.com/blah/myproject.git # it pulls only his feature2 branch or all?
# review the new files, make changes, etc.
git add .
git commit
git checkout master
git merge feature2


Does the 2nd line (
git pull...
) pull all of his branches or only his
feature2
?

Something else: will user
blah
appear in Github's contributor list or not?

Case #2



I would like to try his
feature1
branch here on my computer, slightly modify it, and merge it into my own
master
. How to do it, given that I already have a
feature1
branch myself, that I don't want to overwrite(!) ?

Answer

All branches are in their own namespaces, based on the remote name.

So it is best to add a remote for the repo you want to try out:

git remote add blah https://github.com/blah/myproject.git

Then you can fetch from blah

git fetch blah

And create your feature2 from blah/feature2:

git checkout -b feature2 blah/feature2

As for feature1, you can simply merge blah/feature1 to your master.
Or you can create your own local branch:

git checkout -b blah_feature1 blah/feature1
Comments