vijayinani
vijayinani

Reputation: 2634

How to merge changes from one branch to another in git

I am new to GIT, so please bear with my technical terms related to GIT.

Steps: a. I created a new branch (Y) on the remote server from base branch (X) and then checked out the same on my local machine.

b. I did few changes in 'Y' on my local.

c. Someone has created a new branch (Z) on the remote server from 'X'.

Q) Now, I want to copy all changes from Z to Y, both on local and remote, how do I do that? Also, need to make sure that my changes done in Y are not lost.

Please elaborate. Thanks!

Upvotes: 0

Views: 504

Answers (1)

Mostafa Talebi
Mostafa Talebi

Reputation: 9173

You should do git fetch {repository} Z from remote to local [REM] to [LOC]

Then do git branch Z [LOC]

Then do git checkout Z [LOC]

And after that you've got to merge the FETCH_HEAD [LOC]

git merge FETCH_HEAD [LOC]

Then switch to branch Y in your local. [LOC]

And do git merge Y [LOC]

The above process can be inscribed in words as follows:

You download the newly created branch from your remote repository to your local repository using 'git fetch', this allows you to have your branch/repo downloaded to your local repo, but without any further operation such as merging. It is just kept apart in pseudo-branch called FETCH_HEAD. You then create a branch in your local, merge the content of of FETCH_HEAD (which in your case is branch Z of remote repository. Now you have a separated branch containing the latest changes made through branch Z of remote repo. In the end, you merge the content of your new local branch into whatever branch you like.

Upvotes: 1

Related Questions