Merging with Git - 14 days of Git
Tags: 14daysofgit, git, devops
Published: 2022-09-29 14:33:54

It is Day 10 of my learning journey into Git.  So far on my learning journey has taken me on the following path:

* [What is version control?](https://www.techielass.com/what-is-version-control)
* [What is Git?](https://www.techielass.com/what-is-git)
* [Installing Git](https://www.techielass.com/installing-git/)
* [Basic Git commands to get started](https://www.techielass.com/basic-git-commands-to-get-started)
* [Inspect a Git repository](https://www.techielass.com/inspect-a-git-respository)
* [Git File Operations](https://www.techielass.com/git-file-operations)
* [Undoing commits and changes](https://www.techielass.com/undoing-commits-changes)
* [Rewriting history](https://www.techielass.com/rewriting-git-history/)
* [Git Branches](https://www.techielass.com/git-branches/)

Have you been following along?  Today I am going to be learning about merging in more detail.

### **What is merging?**

Merging is where you take two branches and combine them.  Git will take the commit pointers it has within the two branches and attempt to find a common base commit between them and then unify the two branches.

### **Merge two Git branches together**

You've got a branch and it's ready to merge into your main one.

The first thing you need to do is make sure you are in the branch you want to merge your changes into.  

In this example I have a branch called "new-feature", and I want to merge it with my main branch.  I first switch over to my main branch:

Now I am in the branch I want to merge my changes into I type:

_When you issue this command you may receive merge conflicts, we will be looking at how to deal with them in[ Day 13 of 14 days of Git](https://github.com/weeyin83/14daysofgit)._

I now have to push this merge from my local repository to my remote repository, I do that with the command:

This is probably the most common use case of merging branches together.

### **Merge more than one branch**

You can merge more than one branch at a time.  There could be conflicts and issues when doing so, and we'll be looking at that in[ Day 13](https://github.com/weeyin83/14daysofgit).

In this example, I have three branches with minor changes, and I want to merge them into my main branch.  I issue the command _git merge branch1 branch2 branch3_


### **View the log of merging**

We saw on[ Day 6](https://www.techielass.com/inspect-a-git-respository/) that there is a command called _git log_ that we can use to find out more information about the current state of the repository.  If we append more options to that command, we can get a graphical version of our commits.

This command gives us an update on the different commits and merges that have happened with a bit of a graphical display down the left-hand side.  Again, we might prefer to do some of these operations within graphical editors, but the command line tool is very powerful and gives you a lot of information.

### **Git merge strategies**

When digging into merging, I found there were different[ merge strategies](https://git-scm.com/docs/merge-strategies) available for different use cases.

These are the typical merge strategies:

* Fast Forward
* Recursive
* Ours
* Octopus
* Resolve
* Subtree

Each strategy has its own use case, for example when we merged the three branches into the main one[ Git](https://git-scm.com/) used the Fast Forward merge strategy. Whereas recursive is the default merge strategy when merging one branch.

I'll be digging into Subtree in more detail on Day 14 of my learning journey.

## **14 days of Git**

Today was quite a learning journey, I had no idea there were so many different ways Git would merge branches together.  But it makes sense, more often than not people will just be merging one branch into another.  But that won't always be the case and Git has to handle larger and more complex cases, especially when large teams are working on different branches at the same time.

Merging is definitely an area to come back to and really understand each strategy and it's use case, but I am looking forward to Day 14 when I look at Subtree!

Tomorrow though I am looking at rebasing, be sure to[ subscribe](https://www.techielass.com/newsletter) and join me for that step in the learning journey!

You can follow along here:[ https://github.com/weeyin83/14daysofgit](https://github.com/weeyin83/14daysofgit)