Git and Github Part-3
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:
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.
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.
Edit
main.py:# main.py def add_numbers(a, b): return a + bCheck the branch status:
git statusThis 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.pyStage the changes:
git add main.pyCommit 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.
Switch back to
main:git checkout mainCreate a new branch:
git checkout -b quick-fixMake and commit the fix:
# main.py def multiply_numbers(a, b): return a * bThen stage and commit:
git add main.py git commit -m "Fixed multiplication function bug"Merge the
quick-fixbranch back intomain:git checkout main git merge quick-fixMerging Your Feature Branch Back to Main
After finishing the feature in
add-feature-x, you’ll merge it intomain.Switch to
main:git checkout mainMerge
add-feature-x:git merge add-feature-xDelete 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
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
mainbranch and create afeaturebranch from it, and only work onfeaturewithout any new changes tomain, Git will perform a fast-forward merge when mergingfeatureback intomain.
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.
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
Create a new branch for adding a feature.
git checkout -b add-featureAdd new code in a Python file.
# main.py in add-feature branch def new_feature(): print("This is a new feature")Stage and commit the changes in the
add-featurebranch.git add main.py git commit -m "Added new feature function"Switch to the main branch to simulate fixing a bug there.
git checkout mainEdit the same file in
mainbranch 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
Switch to
mainbranch if you’re not already on it.git checkout mainAttempt the merge of
add-featurebranch intomain.git merge add-feature
Since main.py was modified in both branches, Git will likely generate a merge conflict.
Resolving Merge Conflicts
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-featureResolve 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")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
Choose the correct branch to merge into: Always be on the branch you want to merge changes into.
Start the merge: Use
git merge <branch-name>to begin merging the specified branch.Resolve conflicts: If conflicts occur, Git will mark them in the files; resolve them by choosing which code to keep.
Finalize the merge: Stage the resolved files and commit the merge to complete the process.
Handling Merge Conflicts Efficiently
Use
git diffto inspect conflicts:git diffshows what has changed in the conflicting areas, making it easier to resolve.Abort a merge if needed: If the merge becomes too complicated or you need to start over, use
git merge --abort.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.

