top of page

Merging Branches

Objective:The objective of this assignment is to help you understand and practice merging branches in Git. You will work with multiple branches, merge changes, and handle potential conflicts.


Instructions:


Part 1: Repository Setup 


1. Task 1:Create a new directory/folder on your computer for this assignment and name it "GitMergingAssignment."


2. Task 2:Navigate to the "GitMergingAssignment" directory in your terminal or command prompt.


3. Task 3:Initialize a new Git repository in this directory.


4. Task 4:Create a new text file named "README.md" in the repository.


Part 2: Creating Branches 


1. Task 1:Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.


2. Task 2:Add some content to the "README.md" file. For example:

   

   # Git Merging Assignment


   This is the main README file.

   


3. Task 3:Save the changes to the "README.md" file and exit the text editor.


4. Task 4:Create a new branch named "feature/branch-A."


   

   git branch feature/branch-A

   


5. Task 5:Switch to the new branch.


   

   git checkout feature/branch-A

   


6. Task 6:Open the "README.md" file again and add some content specific to branch A.


7. Task 7:Save the changes and exit the text editor.


Part 3: Creating Another Branch


1. Task 1:Create a new branch named "feature/branch-B."


   

   git branch feature/branch-B

   


2. Task 2:Switch to the new branch.


   

   git checkout feature/branch-B

   


3. Task 3:Open the "README.md" file and add some content specific to branch B.


4. Task 4:Save the changes and exit the text editor.


Part 4: Merging Branches 


1. Task 1:Switch back to the main branch.

   

   git checkout main

   


2. Task 2:Open the "README.md" file and add some content to reflect changes made to the main branch.


3. Task 3:Save the changes and exit the text editor.


4. Task 4:Merge the "feature/branch-A" branch into the main branch.

   

   git merge feature/branch-A

   


5. Task 5:Merge the "feature/branch-B" branch into the main branch.

   

   git merge feature/branch-B

   


Part 5: Handling Merge Conflicts 


1. Task 1:Switch back to the main branch.

   

   git checkout main

   


2. Task 2:Create a new branch named "feature/branch-C."

   

   git branch feature/branch-C

   


3. Task 3:Switch to the new branch.

   

   git checkout feature/branch-C   


4. Task 4:Open the "README.md" file and make conflicting changes in the same region where "feature/branch-B" made changes.


5. Task 5:Save the changes and exit the text editor.


6. Task 6:Attempt to merge "feature/branch-C" into the main branch.

   

   git merge feature/branch-C   


7. Task 7:Handle any merge conflicts that occur. Edit the conflicted file(s) to resolve conflicts.


8. Task 8:Commit the resolved changes.



Solutions

Part 1: Repository Setup


1. Task 1: Create a new directory for the assignment.   

   mkdir GitMergingAssignment

   


2. Task 2: Navigate to the "GitMergingAssignment" directory.   

   cd GitMergingAssignment

   


3. Task 3: Initialize a new Git repository.   

   git init

   


4. Task 4: Create a new text file named "README.md."   

   touch README.md

   


Part 2: Creating Branches


1. Task 1: Open the "README.md" file using a text editor or a command-line text editor like Vim or Nano.   

   nano README.md  # Use your preferred text editor

   


2. Task 2: Add some content to the "README.md" file. For example:

   

   # Git Merging Assignment


   This is the main README file.

   


3. Task 3: Save the changes to the "README.md" file and exit the text editor.


4. Task 4: Create a new branch named "feature/branch-A."   

   git branch feature/branch-A

   


5. Task 5: Switch to the new branch.   

   git checkout feature/branch-A

   


6. Task 6: Open the "README.md" file again and add some content specific to branch A.

  

   nano README.md

   


7. Task 7: Save the changes and exit the text editor.



Part 3: Creating Another Branch


1. Task 1: Create a new branch named "feature/branch-B."   

   git branch feature/branch-B

   


2. Task 2: Switch to the new branch.   

   git checkout feature/branch-B

   


3. Task 3: Open the "README.md" file and add some content specific to branch B.   

   nano README.md

   


4. Task 4: Save the changes and exit the text editor.



Part 4: Merging Branches


1. Task 1: Switch back to the main branch.   

   git checkout main

   


2. Task 2: Open the "README.md" file and add some content to reflect changes made to the main branch.   

   nano README.md

   


3. Task 3: Save the changes and exit the text editor.


4. Task 4: Merge the "feature/branch-A" branch into the main branch.   

   git merge feature/branch-A

   


5. Task 5: Merge the "feature/branch-B" branch into the main branch.   

   git merge feature/branch-B

   


Part 5: Handling Merge Conflicts


1. Task 1: Switch back to the main branch.   

   git checkout main

   


2. Task 2: Create a new branch named "feature/branch-C."   

   git branch feature/branch-C

   


3. Task 3: Switch to the new branch.   

   git checkout feature/branch-C

   


4. Task 4: Open the "README.md" file and make conflicting changes in the same region where "feature/branch-B" made changes.  

   nano README.md

   


5. Task 5: Save the changes and exit the text editor.


6. Task 6: Attempt to merge "feature/branch-C" into the main branch.   

   git merge feature/branch-C

   


7. Task 7: Handle any merge conflicts that occur. Edit the conflicted file(s) to resolve conflicts.


8. Task 8: Commit the resolved changes.



bottom of page