Git Merge: Merging Changes from other Branches
Tags: git, webdev, tutorial, beginners
Published: 2022-10-25 19:44:47

Git provides a powerful tool by letting us create [branches](https://fjolt.com/article/git-branches). When our branch is done, we'll often want to merge it back into our `main` or `master` branch - or perhaps even merge it into another branch entirely. Merging with git is straightforward. In this guide, we'll cover the `merge` command.


## Merging your git branches
Merging is quite straight forward. Suppose you are on your `main` branch, and you want to merge a branch called `my-branch`. All you have to do is run the following command:
```shell
git merge my-branch
```

To visualise this, take a look at the diagram below. Here, the `main` branch is shown in green. Before we perform our merge, we are on the commit labelled `main commit`. After, `merge` creates a new merge commit labelled as `new merge commit`. That means any time you make a merge, a commit will also be made: 

![Merging Git Branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dj9b1p68z81r0836fyzq.png)

### Performing a merge 
As mentioned, you'll want to use `git merge` to merge another branch into your current branch. However, you will have to commit everything on your current branch before doing that. So first, make sure you checkout the branch you want to merge something into:
```shell
git checkout main
```

Then add and commit any files on your branch:
```shell
git add -A
git commit -m "Some merge message"
```

Then finally, you are ready to merge something into your branch:
```shell
git merge my-branch
```

### Fast forward merging

Since the branches diverged here, only one new commit is made. However, if the branches did not diverge, then something called a "fast forward" merge is committed instead. This is where the entire commit history for the feature is copied onto the `main` branch. For example, if we had a `main` branch that we created a branch of, and then **only** changed the branch, and not `main`, a fast forward merge would be created.

This means the history of our commits would look like this instead:

![Fast forward merges with Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5ccfblcsvs5t8qomo3g.png)

You can avoid a fast forward merge by using the `--no-ff`:
```shell
git merge --no-ff my-branch
```

There are also a few other `ff` options:
- `--ff` - the default - a fast forward will occur when possible.
- `--ff-only` - will only merge if a fast forward is possible. Otherwise nothing will happen.

### Resolving merge conflicts
Merging code will sometimes lead to merge conflicts. This is when Git can't automatically determine what should happen when you try to merge code. In these cases, you'll have to manually resolve a merge before you can complete it. If you don't think you can resolve it right now, you can exit out of a merge by running `git merge --abort`.

[You can learn more about merge conflicts and resolving them here](https://fjolt.com/article/git-merge-conflicts).

### Setting a merge commit message
As I've mentioned, a merge will also create a new commit in your git history. If you want to add a custom message, you can use the `-m` flag just as you would with `git commit`:
```shell
git merge my-branch -m "Some commit message"
```

### Preventing an auto commit 

Sometimes, a merge message is not enough - you want to merge your code into a branch, change it a little, and then finalise your commit. You can merge a branch without creating a commit by using `--no-commit`:
```shell
git merge --no-comit my-branch
```

After that, you can make any changes you want to your code, and then make a new commit at your leisure:
```shell
git add -A 
git commit -m "New commit message"
```

## Conclusion
In this guide we've covered the main themes you'll need to know around merging. In git, merging your code is an important theme of collaborating with others and managing branches. [If you run into merge conflicts, you can read about that here](https://fjolt.com/article/git-merge-conflicts). Otherwise, [check out more git content here](https://fjolt.com/category/git).