Git Merge: friend or fiend?

peterjgrainger

Peter Grainger

Posted on February 15, 2020

Git Merge: friend or fiend?

A little knowledge is a dangerous thing. When I first learnt to drive, I thought I was a 17 year old version of Michael Schumacher, one of the best formula one racers of the time. I look back on those years of driving around with my friends and I'm terrified! How did I survive!

I feel a little like that now looking back on what I "knew" about Git last week and what I know now. git merge started out on Monday as a fiend but on Friday I welcomed it into my life like a long lost friend because of some great power features that make my life much easier now I understand it a little better.

Git Merge Comes As Standard

git merge is a command that comes as standard when you install git. It's used for merging one or more commits into the branch you have currently checked out. It's most commonly used for merging one branch into another one. For example merging your feature back into the master branch.

Merge Lots of Branches At the Same Time

git merge <commit-sha-1> <commit-sha-2> will merge as many commits as you like! This uses the merge strategy octopus, by default. You'll probably not need to know what it does exactly, but it's the default-and it sounds cool.

Use the Branch or Tag Identifier Instead of SHA

You can use a branch identifier like develop or a tag like v1.0.0 instead of going to the trouble of finding a specific commit SHA :)

git merge v1.2.3

I needed to merge multiple tags together and it would have been a real pain if this feature wasn't there.

Specify Different Merge Strategies

By default git merge uses a recursive merge. All you need to know is that it is the one that avoids the most conflicts.

There are other strategies. Using the strategy ours uses the current branch changes over the one being merged in if there are any conflicting changes.

git merge --strategy ours 

You can also specify theirs as a strategy to take the changes from the incoming branch.

Merge Branches That Have No Common History

Most of the time you will be using git merge inside one repository but you don't have to limit yourself. By adding a second remote you can merge any branch from any other repository with the flag allow-unrelated-histories. One caveat, if you have the same filenames in both repositories get ready for lots of merge conflicts. :)

git remote add other-repo git@github.com:peterjgrainger/other-repo.git
git merge other-repo/master --allow-unrelated-histories

Loads More Great Features

I realise now that I know nothing about git, I've only scratched the surface. If you want to read more about git merge see the documentation https://git-scm.com/docs/git-merge. It's not the most interesting read but a good reference.

💖 💪 🙅 🚩
peterjgrainger
Peter Grainger

Posted on February 15, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Git Merge: friend or fiend?
gitgitmergemerge Git Merge: friend or fiend?

February 15, 2020