Branching, Merging and Options

Follow Us

Our Communities

Module 3 – Branching, Merging, and Options

In this module, we dive deeper into the features of Git that allow for non-linear development via branches, as well as combining these branches through merging.

Understanding Git Branching

A branch in Git is essentially an independent line of development. Think of it as a new context — it represents a separate ‘thread’ for changes. Branching is fast, as Git only needs to create a new pointer to the existing commits.

  • Creating a Branch: The git branch [branch-name] command creates a new branch. It does not switch you to the new branch; it just creates it. The new branch points to the same commit you’re currently on.

  • Switching Between Branches: The git checkout [branch-name] command switches you from one branch to another. Git will update your working directory to reflect the snapshot of the commit that the branch you’re checking out points to.

  • Creating and Switching to a New Branch: The git checkout -b [branch-name] command creates a new branch and immediately switches you to that branch. It’s a shorthand for the two commands we mentioned above.

  • Deleting a Branch: The git branch -d [branch-name] command deletes a branch. It’s a safe operation because Git prevents you from deleting the branch if it has changes that haven’t been merged.

  • Listing All Branches: The git branch command, when used without any arguments, lists all the branches in your repository.

Merging in Git

Merging is Git’s way of putting a forked history back together. You can take the contents of a source branch and integrate it with the target branch.

  • Merging a Branch: The git merge [branch-name] command merges the specified branch into the current branch. Git will attempt to auto-merge changes unless there are conflicts.

Understanding Merge Conflicts and Resolution

A merge conflict is an event that occurs when Git is unable to automatically resolve differences in code between two commits.

  • Identifying a Merge Conflict: When a conflict occurs, Git marks the conflicted area in the file by enclosing it in <<<<<<< HEAD and >>>>>>> [branch-name]. The part before the ======= is the HEAD (your current branch), and the part after is from the branch you’re merging.

  • Resolving Merge Conflicts: To resolve the conflict, you edit the file to fix the conflicting changes and then add the resolved file to the staging area using git add. After resolving all files that had conflicts, you can continue the merge process with git commit.

  • Using Merge Tools: For more complicated conflicts, Git offers tools like git mergetool which opens a GUI that helps you in resolving these merge conflicts.

Merge Strategies and Options

Git uses different strategies to merge branches. The default is the ‘recursive’ strategy, which can handle complex merging scenarios.

  • –no-commit: By default, Git automatically creates a new commit that records the result of the merge. The --no-commit option prevents this automatic commit, giving you a chance to inspect the merge before committing.

  • –squash: The --squash option produces a set of changes similar to what a merge would have produced, but without making a commit or moving any branches.

  • –abort: If there are conflicts you’re not ready to resolve, or if you decide to quit the merge, you can use git merge --abort to stop the merge and bring your branch back to the state before the merge began.

Exercises and Practice

Exercise 1: Create a new branch, named ‘feature’, in a Git repository. Make some commits, then switch back to the ‘main’ branch.

Exercise 2: Merge the ‘feature’ branch into the ‘main’ branch. Pay attention to any messages Git might print out during this process.

Exercise 3: Create a situation where a merge conflict will occur (for instance, by making different changes to the same line of the same file on two different branches). Try to merge these branches and resolve the resulting conflict.

Exercise 4: Create two new branches, ‘branch1’ and ‘branch2’. Add some unique commits to each branch to simulate different lines of development. Switch between the branches and observe how the working directory changes.

Exercise 5: On ‘branch1’, modify a file that exists on ‘branch2’. On ‘branch2’, delete the same file. Merge ‘branch1’ into ‘branch2’. This will create a specific kind of conflict called a modify/delete conflict. Try to resolve this conflict.

Exercise 6: Create another new branch ‘branch3’ from ‘main’. Make some changes and commit them. Now, switch back to ‘main’ and make some more commits, ensuring that ‘main’ has progressed ahead of ‘branch3’. Now, try to merge ‘branch3’ into ‘main’. This is called a “fast-forward” merge. Understand the difference between a “fast-forward” merge and a “three-way” merge.

Exercise 7: Practice with the --no-commit option. Create a new branch, make some changes, and then merge it back into the main branch with the --no-commit option. Explore the state of the repository before making the final commit.

Exercise 8: Experiment with the --squash option. Create a new branch and make multiple commits. Then, merge it into the main branch using the --squash option. Observe how all changes are squashed into a single commit on the main branch.

Exercise 9: Explore the use of the --abort option. Start a merge that you know will cause conflicts. Once the conflict arises, use git merge --abort to cancel the merge and return to the state before the merge was attempted.

These exercises will give you hands-on experience with branching, merging, and resolving conflicts in Git. The more you practice, the more you will understand these operations and their importance in the development process.

Git Configuration


Rebase Regularly and Interactively