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?
- What is Git?
- Installing Git
- Basic Git commands to get started
- Inspect a Git repository
- Git File Operations
- Undoing commits and changes
- Rewriting history
- 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:
git checkout main
Now I am in the branch I want to merge my changes into I type:
git merge new-feature
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.
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.
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 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.
git log --graph --oneline
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 available for different use cases.
These are the typical merge strategies:
- Fast Forward
Each strategy has its own use case, for example when we merged the three branches into the main one Git used the Fast Forward merge strategy. Whereas recursive is the default merge strategy when merging one branch.
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 and join me for that step in the learning journey!
You can follow along here: https://github.com/weeyin83/14daysofgit