Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge changes from remote branch

Tags:

git

I started to learn Git and I'm stuck with one problem.

Let's say that there is one master repository and two forks.

master

-> fork1 -> my local clone

-> fork2

I'm working on one fork and I can pull changes from master repository and merge them with my own. Now, let's say that some changes are made in one branch in second fork. How can I pull them and merge into my repo?

fork2 -> merge with my local clone -> push to fork1

Also, can I merge particular commit (by commit hash) from remote branch in second fork and how?

Thanks for your answers.

like image 259
Ivan Škugor Avatar asked May 23 '12 08:05

Ivan Škugor


People also ask

How do you push changes from one remote branch to another?

In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

Which command will automatically fetch and merge the changes from the remote branch into the current local branch?

When you execute a pull, the changes from the remote branch automatically merge into your current local branch. If you want to obtain the remote changes but not have them merged into your current local branch, you can execute the git fetch command.


1 Answers

Define your fork2 as a remote repo in your fork1

git remote add fork2 /path/to/fork2/repo

then fetch the changes from fork2

git fetch fork2

pull the changes from the fork2.

git pull fork2 <branch name>

A word of caution is the above command will update your current branch. So you should better use a tracking remote repo and review the changes and merge it to your branch in fork1 if you are ok with it.

git checkout --track -b branch_fork2 fork2/branch2

Review the code in your local branch named branch_fork2.

Checkout the repo where you'd like to merge your fork2 changes.

git checkout feature_1

Then merge it

git merge branch_fork2

If it results in merge conflicts, solve the merge conflicts and commit it.

like image 95
positron Avatar answered Nov 06 '22 09:11

positron