Monday, June 24, 2019

Merge Multiple Branches In Git

Lets create two branch. branches name "mybranch2" and "mybranch3" from "master" branch. and then switch over to "mybranch2".

$ git branch mybranch2

$ git branch mybranch3
Now Switch to "mybranch2" branch

$ git checkout mybranch2
 Switched to branch 'mybranch2'
Now check all branch

$ git branch
you will see on screen as below here HEAD point to "mybranch2"

* mybranch2
  mybranch3
  master
now you are in branch "mybranch2". Now make any change into your code and commit the changes.

$ git add -a -m "add my change into mybranch2"

$ git commit -m "this is my first commit"
Now switch to "mybranch3" branch.

$ git checkout mybranch3
Switched to branch 'mybranch3'
Now you are in mybranch3. make any change and commit your code.

$ git add -a -m "add my change into mybranch3"

$ git commit -m "this is my first commit in mybranch3"
In the above code we have make change in two branches "mybranch2" and "mybranch3".
Now we need to merge these two branche to master branch.
So first checkout the master branch and merge one by one.

$ git checkout master
Switched to branch 'master'
now lets merge "mybranch2".

$ git merge mybranch2
Now the above "fast-forward" merge has now been executed.
Now lets try to merge "mybranch3" into "master" branch.
This may give conflict and it will not allow us to automatically merge "mybranch3" with "master" branch.

$ git merge mybranch3
If got conflict then first resolve conflict by go into particular file you will get message somthing like below.

<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> mybranch3
Once we are done with resolving the conflict, let's commit the change.

$ git add -a -m "fix to resolve confilict"
$ git commit -m "fix to resolve confilict ans commit to mybranch3"
Now after this commit. master branch will now pointing to the latest merge commit. This is a non fast-forward merge.



No comments:

Post a Comment