Git Merge: Merging Changes from other Branches
Git provides a powerful tool by letting us create branches. When our branch is done, we’ll often want to merge it back into our
master branch - or perhaps even merge it into another branch entirely. Merging with git is straightforward. In this guide, we’ll cover the
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:
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:
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:
git checkout main
Then add and commit any files on your branch:
git add -A git commit -m "Some merge message"
Then finally, you are ready to merge something into your branch:
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:
You can avoid a fast forward merge by using the
git merge --no-ff my-branch
There are also a few other
--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.
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 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
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:
git add -A git commit -m "New commit message"
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. Otherwise, check out more git content here.
More Tips and Tricks for Git
- How to force overwrite local changes with 'git pull'
- How to Auto Compress Your CSS and JS with Git Hooks
- How to make Git ignore file permission (chmod) changes
- Resolving Git Merge Conflicts
- How to undo a git pull
- How to move your Githooks to your Repo
- How to amend and update a git commit
- Git Stash - Everything about stashing changes in git
- Setting upstream with Git
- Git blame - How to find out who modified a line with Git