top of page

Branching

You need to work on a new feature or fix a bug in isolation, so you create a new branch in your Git repository to make changes without affecting the main codebase


Objective: The objective of this assignment is to help you understand and practice branching in Git. You will create, switch between, merge, and delete branches while working on a sample project.



Instructions:


Part 1: Repository Setup 


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


2. Task 2: Navigate to the "GitBranchingAssignment" 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:


   markdown

   # Git Branching 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/new-feature."

   

   git branch feature/new-feature   


5. Task 5: Switch to the new branch.

   

   git checkout feature/new-feature   


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


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


Part 3: 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/new-feature" branch into the main branch.


   

   git merge feature/new-feature

   


Part 4: Deleting Branches 


1. Task 1: List all branches to confirm that the "feature/new-feature" branch is merged.

   

   git branch   


2. Task 2: Delete the "feature/new-feature" branch.

   

   git branch -d feature/new-feature

   



Solutions

Part 1: Repository Setup


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

   

   mkdir GitBranchingAssignment

   


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

   

   cd GitBranchingAssignment

   


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:


   markdown

   # Git Branching 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/new-feature."

   

   git branch feature/new-feature

   


5. Task 5: Switch to the new branch.

   

   git checkout feature/new-feature   


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

   

   nano README.md   


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



Part 3: 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/new-feature" branch into the main branch.

   

   git merge feature/new-feature

   


Part 4: Deleting Branches


1. Task 1: List all branches to confirm that the "feature/new-feature" branch is merged.

   

   git branch

  

2. Task 2: Delete the "feature/new-feature" branch.


      git branch -d feature/new-feature

   



bottom of page