6. Merge Conflicts

Git merge conflicts arise when you try to merge changes from two branches, but Git can’t automatically determine how to combine modifications made to the same file in both branches. This typically happens when different developers edit the same lines of code or content within a file.

  • Causes:
    • Editing the same lines of code: If two developers work on the same section of code in separate branches and both make changes, Git won’t know how to combine those edits.
    • Deleting and modifying a file: If one developer deletes a file in one branch while another modifies the same file in another branch, Git can’t automatically decide what to do.
  • Identifying Conflicts: When a merge conflict occurs, Git will halt the merge process and provide information about the conflicted files. You’ll typically see error messages or markers within the affected files highlighting the conflicting changes.
  • Resolving Conflicts: To complete the merge, you’ll need to manually edit the conflicted files. Here’s the general workflow:
    1. Identify the conflict markers within the file. Git usually uses markers like <<<<<<<, =======, and >>>>>>> to distinguish the different versions of the conflicting code sections from each branch (yours, the other developer’s, and the common ancestor).
    2. Analyze the changes and decide how to integrate them. You can choose to keep your changes, the other developer’s changes, or merge them together.
    3. Edit the file to remove the conflict markers and create the desired merged version.
    4. Once you’ve resolved all conflicts in all affected files, you can use git add to stage the resolved files and then git commit to complete the merge.
  • Resources for Help: There are many resources available to help you with resolving merge conflicts, including tutorials, cheat sheets, and visual guides. You can search online for “git merge conflict resolution” or refer to the Git documentation for specific instructions https://docs.github.com/articles/resolving-a-merge-conflict-on-github.

Git have support for many tools that help manual merge, but you can also use a simple text editor.

Example

$ git merge b3
Auto-merging file_1.txt
CONFLICT (content): Merge conflict in file_1.txt
Automatic merge failed; fix conflicts and then commit the result.
$

5. Merge branches

Git merge is a command used to combine changes from different branches into a single branch in your version control system. It essentially merges the work done on separate branches into a unified history.

  • Merging Branches: Imagine you’re working on a feature branch while the main codebase continues to be developed on the master branch. When your feature is complete, you can use git merge to integrate your changes from the feature branch into the master branch.
  • Fast-Forward vs. Three-Way Merge: There are two main ways Git can perform a merge:
    • Fast-Forward: In simpler scenarios, where your feature branch incorporates changes directly following the master branch’s history, Git can simply fast-forward the master branch pointer to the commit of your feature branch. This is because there’s no conflicting work to merge.
    • Three-Way Merge: When there are overlapping changes between the branches (i.e., both branches modified the same files), Git performs a more complex three-way merge. It analyzes the common ancestor commit (the last commit before the branches diverged) and the commits specific to each branch. It then attempts to automatically combine the changes.
  • Merge Conflicts: If Git encounters conflicting changes where both branches modify the same lines of code, it will pause the merge process and flag the conflicts. You’ll then need to manually edit the files to resolve the conflicts and tell Git how you want the merged code to look.

In summary, git merge is a powerful tool for integrating work from different development branches. It can be a straightforward process for unconflicting changes, but it might involve manual conflict resolution for overlapping edits.

To merge a branch into another you simple use: git merge [branch]

Example

$ git switch main
Switched to branch 'main'
$ git merge b2
Updating 06f9046..5e58a4b
Fast-forward
 file3.txt  | 2 ++
 file_2.txt | 1 +
 2 files changed, 3 insertions(+)
 create mode 100644 file3.txt
 create mode 100644 file_2.txt

4. Branches

Branches are a fundamental concept in version control. They act as independent lines of development within your project, allowing you to experiment with changes, fix bugs, or develop new features without affecting the main codebase.

Think of branches like different forks in a road. The main road represents the main branch (often named main or master) of your project, which holds the stable, publicly-available version. Branching allows you to create new roads (branches) to explore different directions without altering the main road.

Key Functions:

  • Creating branches: You use the git branch <branch_name> command to create a new branch. This essentially creates a pointer to a specific commit in your project’s history, which becomes the starting point for your branch’s development.
  • Switching branches: You can switch between branches using the git checkout <branch_name> command. This tells Git to focus on the specified branch and its history.
  • Committing changes: When you make changes and commit them, you’re creating a new snapshot of your project’s state specific to the branch you’re currently working on.
  • Merging branches: Once you’re satisfied with the changes in a branch, you can merge them back into the main branch using git merge <branch_name>. This integrates the commits from your branch into the main branch’s history.
  • Deleting branches: When a branch is no longer needed, you can delete it using git branch -d <branch_name>. However, it’s important to ensure the branch has been merged into the main branch or another relevant branch before deletion.

Benefits of using Git branches:

  • Isolation: Branches allow you to experiment and isolate changes without impacting the main codebase. This prevents accidental corruption of the stable version.
  • Collaboration: Team members can work on different features or bug fixes simultaneously on separate branches. This promotes efficient development and faster project completion.
  • Feature development: Branches enable you to develop new features without affecting the main codebase until the feature is ready for integration.
  • Bug fixing: You can create a dedicated branch to fix a bug without affecting ongoing development in the main branch.

Example…

$ git switch -c b2
Switched to a new branch 'b2'
$ touch file.txt
$ git add file.txt
$ git commit -m "comment"
$ git push -u origin b2
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 311 bytes | 155.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for b2, visit:
remote:   https://gitserver/jonas/demo/-/merge_requests/new?merge_request%5Bsource_branch%5D=b2
remote:
To https://gitserver/jonas/demo.git
 * [new branch]      b2 -> b2
Branch 'b2' set up to track remote branch 'b2' from 'origin'.