Git merge - learn by example

Sam - Dec 19 '22 - - Dev Community

Git merge is a powerful tool that allows developers to integrate changes from different branches into a single branch. In this article, I'll explain different ways to merge branches with Git, using a simple example to illustrate each method.

Before we move on, I suggest taking a look at https://github.com/dotenx/dotenx, an open-source tool to visually build websites, APIs and much more and publish them in no time.


To get started, let's create a new Git repository and add a file to it:

$ git init
$ echo "Hello, world!" > greeting.txt
$ git add greeting.txt
$ git commit -m "Add greeting"
Enter fullscreen mode Exit fullscreen mode

Next, let's create a new branch and make some changes to the file:

$ git branch new-feature
$ git checkout new-feature
$ echo "Bonjour, le monde!" >> greeting.txt
$ git commit -am "Add French greeting"
Enter fullscreen mode Exit fullscreen mode

Now we have two branches in our repository: the master branch, which contains the original "Hello, world!" greeting, and the new-feature branch, which contains the additional "Bonjour, le monde!" greeting.

There are several ways to merge these changes back into the master branch. One way is to use the git merge command with the --no-ff option, which creates a new merge commit even if a fast-forward merge is possible. This is useful when you want to preserve the entire branch history, rather than just fast-forwarding the master branch to the latest commit in the new-feature branch.

$ git checkout master
$ git merge --no-ff new-feature
Enter fullscreen mode Exit fullscreen mode

The output of this command will look something like this:

Merge made by the 'recursive' strategy.
 greeting.txt | 1 +
 1 file changed, 1 insertion(+)
Enter fullscreen mode Exit fullscreen mode

If we run git log, we can see that a new merge commit has been created, which brings together the changes from both branches:

$ git log --oneline

e7f1234 (HEAD -> master, new-feature) Merge branch 'new-feature'
b5c9123 Add French greeting
3e2a111 Add greeting
Enter fullscreen mode Exit fullscreen mode

Another way to merge the new-feature branch is to use the git merge command with the --squash option. This option combines all the commits in the new-feature branch into a single commit, which is then merged into the master branch. This is useful when you want to clean up the commit history, or when you want to merge a feature branch into a release branch and preserve a linear history.

$ git checkout master
$ git merge --squash new-feature
$ git commit -m "Merge new-feature"
Enter fullscreen mode Exit fullscreen mode

The output of this command will look similar to the previous example, but if we run git log, we can see that there is only one commit for the merged changes:

$ git log --oneline

e7f1234 (HEAD -> master) Merge new-feature
3e2a111 Add greeting
Enter fullscreen mode Exit fullscreen mode

One tricky situation that can arise when merging branches is a conflict. This occurs when the same lines of code have been modified in both branches, and Git is unable to automatically resolve the differences. In this case, Git will mark the conflicting lines in the file and ask you to resolve the conflict manually.

To demonstrate this, let's create another branch and make some changes to the same lines of code that we modified in the new-feature branch:

$ git branch conflict
$ git checkout conflict
$ sed -i 's/Hello, world!/Hola, mundo!/' greeting.txt
$ git commit -am "Add Spanish greeting"
Enter fullscreen mode Exit fullscreen mode

Now let's try to merge the conflict branch into master:

$ git checkout master
$ git merge conflict
Enter fullscreen mode Exit fullscreen mode

Git will display a message indicating that there is a conflict, and the greeting.txt file will contain markers showing the conflicting lines:

Copy codeAuto-merging greeting.txt
CONFLICT (content): Merge conflict in greeting.txt
Automatic merge failed; fix conflicts and then commit the result.
Enter fullscreen mode Exit fullscreen mode
Copy code<<<<<<< HEAD
Bonjour, le monde!
=======
Hola, mundo!
>>>>>>> conflict
Enter fullscreen mode Exit fullscreen mode

To resolve the conflict, we need to edit the file and remove the markers. For example, we could keep both greetings by adding a line break between them:

Bonjour, le monde!

Hola, mundo!
Enter fullscreen mode Exit fullscreen mode

Then we can commit the resolved conflict:

$ git add greeting.txt
$ git commit -m "Resolve conflict"
Enter fullscreen mode Exit fullscreen mode

Bonus

One trick for avoiding conflicts when merging branches is to ensure that each branch focuses on a separate aspect of the codebase. For example, if one branch is working on the front-end interface and another branch is working on the back-end logic, there is less likelihood of conflicts occurring.

Another tricky situation can arise when merging a branch that contains a large number of commits. In this case, it may be difficult to review and understand the changes that are being merged. One way to simplify the process is to use the git merge command with the --squash option, as we saw earlier. This will combine all the commits into a single commit, which can be easier to review and understand.


Finally, I encourage you to join the open-source movement to make a long-lasting impact and contribute to the development of software that benefits society as a whole. Participating in open-source projects can also help boost your career progress by demonstrating your skills and expertise to potential employers and allowing you to collaborate with other experienced developers.

https://blog.dotenx.com/how-to-contribute-to-open-source-with-any-skill-level

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .