Day 4: Branching in Git

Day 4: Branching in Git

Brief Overview

On Day 4, we will cover:

  1. Understanding branches in Git.

  2. Creating and switching branches.

  3. Merging branches.

  4. Resolving merge conflicts.

  5. Deleting branches.

Detailed Explanation with Code

  1. Understanding Branches in Git:

    Branches allow you to work on different versions of your project simultaneously. The main (or master) branch is the default branch created when you initialize a Git repository. You can create new branches to develop features, fix bugs, or experiment with new ideas without affecting the main codebase.

  2. Creating and Switching Branches:

    • Creating a New Branch: Use the git branch command to create a new branch.

        git branch <branch-name>
      

      Example:

        git branch feature-branch
      
    • Switching to a Different Branch: Use the git checkout command to switch to the new branch.

        git checkout <branch-name>
      

      Example:

        git checkout feature-branch
      
    • Creating and Switching in One Command: Use the git checkout -b command to create and switch to a new branch in one step.

        git checkout -c <branch-name>
      

      Example:

        git checkout -c feature-branch
      
  3. Merging Branches:

    • Switch to the Branch You Want to Merge Into: For example, switch to the main branch.

        git checkout main
      
    • Merge the Branch: Use the git merge command to merge the changes from another branch into the current branch.

        git merge <branch-name>
        //merge the changes from another branch into the current branch i.e main
      

      Example:

        git merge feature-branch
      
  4. Resolving Merge Conflicts: Sometimes, merging branches can lead to conflicts when the same lines of code have been changed in different ways. Git will mark these conflicts in the affected files.

    • Identify the Conflicted Files: Use git status to see which files have conflicts.

        git status
      

      Example:

        git status
      

      Output:

        On branch main
        You have unmerged paths.
          (fix conflicts and run "git commit")
          (use "git merge --abort" to abort the merge)
      
        Unmerged paths:
          (use "git add <file>..." to mark resolution)
          both modified:   hello.txt
      
    • Resolve Conflicts: Open the conflicted files in a text editor and resolve the conflicts. Git marks conflicts with <<<<<<<, =======, and >>>>>>>.

      Example:

        <<<<<<< HEAD
        This is a new line from main branch.
        =======
        This is a new line from feature-branch.
        >>>>>>> feature-branch
      

      Choose the correct version or merge the changes manually, then remove the conflict markers.

    • Stage and Commit the Resolved Changes: After resolving conflicts, stage and commit the changes.

        git add <file>
        git commit -m "Resolve merge conflicts"
      

      Example:

        git add hello.txt
        git commit -m "Resolve merge conflicts in hello.txt"
      
  5. Deleting Branches: Once a branch is no longer needed, you can delete it.

    • Delete a Branch Locally: Use the -d flag to delete a branch.

        git branch -d <branch-name>
      

      Example:

        git branch -d feature-branch
      
    • Force Delete a Branch: If the branch has unmerged changes, use the -D flag to force delete it.

        git branch -D <branch-name>
      

      Example:

        git branch -D feature-branch
      

Example Workflow

  1. Initialize a New Git Repository:

     mkdir my-git-project
     cd my-git-project
     git init
    
  2. Create and Commit a File:

     echo "Hello, Git!" > hello.txt
     git add hello.txt
     git commit -m "Add hello.txt with initial content"
    
  3. Create a New Branch:

     git checkout -c feature-branch
    
  4. Make Changes in the New Branch:

     echo "This is a new line in the feature branch." >> hello.txt
     git add hello.txt
     git commit -m "Add a new line in feature-branch"
    
  5. Switch Back to the main Branch:

     git checkout main
    
  6. Merge the Feature Branch into main:

     git merge feature-branch
    
  7. Resolve Merge Conflicts (if any):

     git status
     # Open conflicted files, resolve conflicts, then
     git add hello.txt
     git commit -m "Resolve merge conflicts in hello.txt"
    
  8. Delete the Feature Branch:

     git branch -d feature-branch
    

By the end of Day 4, readers should understand how to create, switch, merge, and delete branches in Git. This knowledge is essential for managing parallel development and collaborating effectively on different features or fixes within a project.