Git Merge is a way of putting back two previously branched lines of development together. When one branch (source) is merged with another branch (destination), all the changes made to the source branch are integrated into the destination branch. Such integration creates a new commit provided the modifications don’t overlap.
The branch merge, usually, takes two commit pointers (branch tips) to identify a common base commit. And when Git identifies a common base commit between the two commit pointers, it creates a new “merge commit”.
The commit created in the destination branch contains everything that the source branch had.
In this tutorial, I’ll take you through the process of merging two branches in Git.
–commit is used for committing the result once it’s merged. –no-commit can be overridden through –commit.
These two options open up an editor before committing is done. The auto-generated merge message is edited by it.
–signoff adds the signed-off-by-line after the entire commit message.
It’s is used for not adding the signed-off-by-line by the committer.
–ff helps resolve the merge faster. This branch pointer matches the merged branch but restricts any new merge commit creation.
It’s used in the merge commit creation in each case of the merge.
–ff-only resolves the merge quickly if possible otherwise. And it resists the merge and closes with a non-zero status.
–stat gives a diffstat once the merge is finished.
You use these options if you don’t want to display diffstat.
–help provides a list containing all the merge options.
You can overwrite all the ignored files through this option. It’s a default behavior where information is taken from the merge result.
The following command checks the branches of the local repository “upload-file”.
git branch
Then, run the command lines given below for merging the content of two branches.
git checkout master
git add upload4.php
git commit -m "Upload text file."
git checkout main
git merge master
Of the above set of commands:
Once the content is merged, in case, the “master” branch needs not to exist in the local repository, you can delete it. To do that execute:
git branch
ls
git branch -d master
ls
git branch
Of the above commands:
Now, use the following commands.
git checkout -b temp
git add upload5.php
git commit -m “Upload image file”
Of the above code set:
Often, the changes made in two branches conflict with each other. Whenever a conflict occurs, Git informs you about it. After the merge, the git status command is run. This shows all the conflict-containing files. In such cases, you need to resolve those conflicts before moving ahead with your next action. Generally, you need to resolve the conflicts by printing “both modified:” beside the file names.
Git uses 3 visual indicators, such as <<<<<<<, =======, and >>>>>>> to mark the conflicted content. This helps programmers identify the conflicts during a merge. And it follows this format:
<<<<<<< destination-branch-name
...changes from the destination branch...
=======
...changes from the source branch...
>>>>>>> source-branch-name
Below is an example of a conflict representation.
<<<<<<< main
this is conflicted text from main
=======
this is conflicted text from feature branch
>>>>>>> feature branch;
Once you’ve fixed up the conflicts, you need to run “git add” on the files with conflicts. And then execute a “git commit” for generating the merge commit.
And that’s how you can merge two branches in Git. I hope you find this tutorial helpful.