Merge in Git allows you to join two or more development work created using git branch into a single branch. It incorporates the changes from named commits and diverges them into the current branch. Before making a merge option make sure the receiving branch and the merging branch are up-to-date with the latest remote changes.
What are Merge Strategies?
Git provides various methods to merge different commits into a base commit. These methods are called Merge Strategies. These base commits are combined to form merge commits. A merge commit is just like the regular commit except that it has two parent commits. Out of multiple strategies for the merging process, the git will automatically choose one if not specified explicitly. This automatic selection of the merge strategy is based on the branches provided for merging.
There are various types of merge strategies :
- Fast Forward
Git merge will combine multiple sequences of commits into one unified history. In the most frequent use cases, git merge is used to combine two branches. Any of the upgiven strategies can be used to perform the merging process according to the needs of the project. The most commonly used strategies are Fast Forward Merge and Recursive Merge.
Fast Forward Merge:
In this most commonly used merge strategy, history is just one straight line. When you create a branch, make some commits in that branch, the time you’re ready to merge, there is no new merge on the master. That way master’s pointer is just moved straight forward and history is one straight line.
$ git rebase
In Recursive merge, after you branch and make some commits, there are some new original commits on the ‘master‘. So, when it’s time to merge, git recurses over the branch and creates a new merge commit. The merge commit continues to have two parents.
$ git merge--no-ff
Note: There is nothing right or wrong of either one of the strategies but with fast forward merge you have a straight line of history and with the recursive merge, it is of multiple lines.
Fast-Forward merge vs Recursive merge:
|No new commits on the master||New commits on the master|
|Linear History||Commit 2 parents|
|No merge commits||Merge commit is created|
|git rebase||git merge–no-ff|
This merge strategy resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede the old development history of side branches.
Note: This strategy is different from the -Xours option to the ‘recursive‘ merge strategy.
$ git merge -s ours
Octopus Merge strategy resolves cases with more than two heads but refuses to do a complex merge that needs manual resolution. It is primarily meant to be used for bundling topic branch heads together. This is the default merge strategy when pulling or merging more than one branch.
$ git merge -s octopus
This strategy can only resolve two heads (i.e. the current branch and another branch you pulled from) using a 3-way merge algorithm. It tries to carefully detect criss-cross merge ambiguities and is considered generally safe and fast.
$ git merge -s resolve
Note: ‘-s resolve‘ solves only trivial conditions. If code differs between branches, the conflict has to be solved manually.
This is a modified recursive strategy. When merging trees A and B, if B corresponds to a subtree of A, B is first adjusted to match the tree structure of A, instead of reading the trees at the same level. This adjustment is also done to the common ancestor tree.
$ git merge -s subtree
Manually call named merge strategy
-s<Strategy> and –strategy=<Strategy>: These strategies can be supplied more than once to specify them in the order they should be tried. If there is no -s option, a built-in list of strategies is used instead (git merge-recursive when merging a single head, git merge-octopus otherwise).
$ git merge -s recursive $ git merge --strategy=octopus
- Git-Checkout And Merge
- How to merge two PHP objects?
- ES6 | Merge Objects
- Merge two sets in Java
- How to merge the duplicate value in multidimensional array in PHP ?
- Merge two arrays keeping original keys in PHP
- HashMap merge(key, value, BiFunction) method in Java with Examples
- How to fade in color button from btn-primary to btn-success?
- How to create three boxes in the same div using HTML and CSS ?
- How to create dictionary and add key–value pairs dynamically?
- Which Characters Should Be Escaped Inside A "pre" tag?
- How to get the value of a textarea in jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.