Skip to main content

Command Palette

Search for a command to run...

Git and Github Part-3

Published
•7 min read•View as Markdown
U

Transformative Tech Leader | Serial Entrepreneur & Machine Learning Engineer Leveraging 3+ years of expertise in Machine Learning and a background in Web Development, I drive innovation through building, mentoring, and educating. Passionate about harnessing AI to solve real-world problems."

Working with Git Branches

In Git, branches allow developers to work on separate versions of a repository simultaneously. This approach is especially useful when working on a large project with multiple features or updates. Let’s see how Git branching makes it easier to manage these changes without disrupting the main codebase.

Understanding the Role of Branches

Imagine you're developing a Python project, and you need to implement a new feature or update existing functionality. Here's how branching can simplify this process:

  1. Without Git:

    • You’d manually create copies of files you want to modify.

    • Any urgent bug fixes would require you to track which files have been duplicated, potentially missing updates when merging files back into the main version.

    • It’s easy to make mistakes and forget which file is the latest.

  2. With Git:

    • You can create a new branch for the new feature, edit the code there, and later merge it back with the main branch without affecting other branches.

    • Urgent fixes can be done in a separate branch and merged into the main project, so each branch remains isolated.

Creating and Working with Git Branches

Let's walk through working with branches in a Python project.

Creating a New Branch

When you want to start a new feature, create a branch:

git branch add-feature-x

This command creates a new branch called add-feature-x. You can see a list of all branches by running:

git branch

You'll see output similar to:

  add-feature-x
* main

The * indicates the branch you’re currently on. To switch to your new branch, use:

git checkout add-feature-x

Or, to create and switch to a new branch in one command:

git checkout -b add-feature-x

Adding Code to Your New Branch

Suppose we're adding a new feature to a Python file, main.py. Let's create a function that adds two numbers, then commit it to the new branch.

  1. Edit main.py:

     # main.py
     def add_numbers(a, b):
         return a + b
    
  2. Check the branch status:

     git status
    

    This will show the modified file:

     On branch add-feature-x
     Changes not staged for commit:
       (use "git add <file>..." to update what will be committed)
       modified:   main.py
    
  3. Stage the changes:

     git add main.py
    
  4. Commit the changes:

     git commit -m "Added add_numbers function"
    

Handling Urgent Fixes with a New Branch

Suppose there's an urgent bug in the main branch. Instead of abandoning your work in add-feature-x, you can save your changes, switch to the main branch, and create a separate branch for the fix.

  1. Switch back to main:

     git checkout main
    
  2. Create a new branch:

     git checkout -b quick-fix
    
  3. Make and commit the fix:

     # main.py
     def multiply_numbers(a, b):
         return a * b
    

    Then stage and commit:

     git add main.py
     git commit -m "Fixed multiplication function bug"
    

    Merge the quick-fix branch back into main:

     git checkout main
     git merge quick-fix
    

    Merging Your Feature Branch Back to Main

    After finishing the feature in add-feature-x, you’ll merge it into main.

    1. Switch to main:

       git checkout main
      
    2. Merge add-feature-x:

       git merge add-feature-x
      
    3. Delete the feature branch (optional, to keep things organized):

       git branch -d add-feature-x
      

Handling Merge Conflicts

If there’s a conflict, Git will indicate which files need attention. Open the file and look for conflict markers like:

    code<<<<<<< HEAD
    current code in main branch
    =======
    code in add-feature-x branch
    >>>>>>> add-feature-x

Modify the file to your preference, then stage and commit the resolved file:

    git add main.py
    git commit -m "Resolved merge conflict in main.py"

Understanding Merging in Git

Merging in Git is the process of integrating changes from one branch into another. It allows you to combine separate lines of development, such as when a feature branch is completed and ready to be merged into the main branch. Merging is especially helpful when multiple developers are working on different parts of a project, or even when a single developer wants to isolate new features or fixes in different branches.

Key Concepts in Merging

  1. Fast-forward Merging:

    • A fast-forward merge occurs when there are no new commits on the target branch since the branch you're merging was created. This is essentially a simple update where the target branch "catches up" with the commits on the other branch by advancing its pointer forward.

    • Example: If you have a main branch and create a feature branch from it, and only work on feature without any new changes to main, Git will perform a fast-forward merge when merging feature back into main.

  2. Three-way Merge:

    • A three-way merge is performed when the target branch and the branch you're merging both have new commits since the branches diverged. Git will identify the common ancestor of both branches and compare the changes made on each branch to combine them.

    • This type of merge may result in a merge conflict if changes have been made to the same part of a file in both branches.

  3. Merge Conflict:

    • Merge conflicts occur when Git cannot automatically reconcile differences between branches. When a file has been modified differently in two branches, Git requires you to manually resolve these conflicts. After resolving, you need to commit the merge to finalize it.

Let’s go through these types with examples and commands to illustrate each step.

Merging a Branch - Step-by-Step Example

Imagine we have a Python project, and you’re working on a feature branch to add a new function. Meanwhile, a bug was fixed in the main branch. After completing your work on the feature branch, you now want to merge these changes into main.

Initial Setup

# Navigate to your Git repository
cd your_project_directory

# Check out the main branch
git checkout main

Creating and Working on a New Branch

  1. Create a new branch for adding a feature.

     git checkout -b add-feature
    
  2. Add new code in a Python file.

     # main.py in add-feature branch
     def new_feature():
         print("This is a new feature")
    
  3. Stage and commit the changes in the add-feature branch.

     git add main.py
     git commit -m "Added new feature function"
    
  4. Switch to the main branch to simulate fixing a bug there.

     git checkout main
    
  5. Edit the same file in main branch for the bug fix and commit it.

     # main.py in main branch
     def bug_fix():
         print("This is a bug fix")
    
     git add main.py
     git commit -m "Fixed a bug"
    

Merging the add-feature Branch into main

  1. Switch to main branch if you’re not already on it.

     git checkout main
    
  2. Attempt the merge of add-feature branch into main.

     git merge add-feature
    

Since main.py was modified in both branches, Git will likely generate a merge conflict.

Resolving Merge Conflicts

  1. Git will indicate the files with conflicts. Open the file in your editor to resolve the conflict.

     # main.py (after merge conflict)
     def new_feature():
         print("This is a new feature")
    
     <<<<<<< HEAD
     def bug_fix():
         print("This is a bug fix")
     =======
     def another_function():
         print("This is another function")
     >>>>>>> add-feature
    
  2. Resolve the conflict by editing the file to keep both functions as needed.

     # Resolved main.py
     def new_feature():
         print("This is a new feature")
    
     def bug_fix():
         print("This is a bug fix")
    
     def another_function():
         print("This is another function")
    
  3. Stage the resolved file and complete the merge.

     git add main.py
     git commit -m "Merged add-feature with bug fix"
    

Checking the Merge Results

After merging, you can confirm the history of changes and validate that both the bug fix and the new feature are present:

git log --oneline --graph --all

Summary of Merging Process

  1. Choose the correct branch to merge into: Always be on the branch you want to merge changes into.

  2. Start the merge: Use git merge <branch-name> to begin merging the specified branch.

  3. Resolve conflicts: If conflicts occur, Git will mark them in the files; resolve them by choosing which code to keep.

  4. Finalize the merge: Stage the resolved files and commit the merge to complete the process.

Handling Merge Conflicts Efficiently

  1. Use git diff to inspect conflicts: git diff shows what has changed in the conflicting areas, making it easier to resolve.

  2. Abort a merge if needed: If the merge becomes too complicated or you need to start over, use git merge --abort.

  3. Practice safe merging: Regularly commit changes and create branches for distinct features or fixes to minimize conflicts.

Using Git branches and merging allows you to work on multiple aspects of a project safely, keep changes isolated, and integrate them when you’re ready.

Working with Multiple Branches

By branching, you can seamlessly work on various features, perform bug fixes, and even test new ideas without risking the stability of the main code. Each branch operates independently, and only when you’re ready, you can merge changes back into main.

Branching allows for a cleaner, more organized project structure and ensures safer development. Try practicing with branches to become familiar with switching, merging, and resolving conflicts. This flexibility is one of Git’s most powerful features.

More from this blog

Coder Connect

23 posts

Join the coding revolution: learn, innovate, transform. The only limit is your mind."