🐙
Git Developer Guide
About
  • Overview
  • Scope of this book
  • Table of Content
  • 🐢Introduction to Version Control
    • What is Version Control?
    • Overview of git and it's benefits
    • Setting up Git on Different Platforms
  • 🍼Git Fundamentals
    • Initialising a new Git repository
    • Understanding the Git Workflow
    • Committing Changes and Writing Good Commit Messages
    • Viewing and Navigating Commit History
    • Git Basics - Practice Scenarios
      • Initialising a Git Repository
      • Committing Changes
      • Exploring Commit History
      • Amending and Undoing Commits
  • 🦕Working With Git
    • What is Git Branch?
    • Creating and Switching Between Branches
    • Merging Branches and Resolving Conflicts
    • Best Practices for Branch Management
    • Git Workflows
    • Git Log
    • Git Stash
    • Working with Git - Practice Scenarios
      • Creating and Switching Between Branches
      • Merging Branches and Resolving Conflicts
      • Branching Strategies in a Team Project
      • Rolling Back to a Previous Version
      • Experimenting with Feature Branches
      • Working with Stash
  • 🤝Working with Remote Repositories
    • Cloning a Repository from Remote
    • Pushing and Pulling Changes to and from Remote Repositories
    • Collaborative Workflows - Forking, Branching, and Pull Requests
    • Resolving Conflicts in a Collaborative Environment
    • Collaborating with Git - Practice Scenarios
      • Cloning a Remote Repository
      • Pushing and Pulling Changes
      • Collaborative Workflow with Forking and Pull Requests
      • Resolving Conflicts in a Pull Request
  • 🏆Advanced Git Features
    • Aliases and Custom Configurations
    • Working with Tags and Releases
    • Rewriting Commit History with Interactive Rebase
    • Utilising Git Hooks for Automation
    • Advanced Git Features - Practice Scenarios
      • Creating Custom Git Aliases
      • Working with Tags and Releases
      • Rewriting Commit History with Interactive Rebase
      • Using Git Hooks for Automated Testing
  • 😎Git in Real-World
    • Managing a Project with Multiple Contributors
    • Integrating Git with Continuous Integration, Continuous Deployment (CI, CD)
    • Versioning Assets with Git LFS (Large File Storage)
    • Deploying a Web Application using Git
    • Git In Real World - Practice Scenarios
      • Managing a Project with Multiple Contributors
      • Integrating Git with CICD Pipelines
      • Versioning Assets with Git LFS
      • Deploying a Web Application using Git
  • Git Troubleshooting
    • Common Mistakes and Pitfalls When Using Git
    • Undoing Changes with Git - Reverting and Resetting
    • Recovering Lost Commits or Branches
    • Dealing with Repository Corruption or Other Issues
  • Git Best Practices and Tips
    • Creating efficient git workflows: writing clean code for faster reviews
    • The importance of clean code in collaborative development
    • Significance of consistent naming conventions & coding Standards
    • Good code documentation for better git workflows
    • Writing meaningful git commit messages
    • Atomic commits in git & it's benefits for software teams
    • Structuring code & managing dependencies for better git workflows
    • Git branching strategies for software teams
  • Conclusion & Next Steps
    • Recap of Key Concepts and Commands
    • Further Resources for Expanding Git Knowledge
    • Encouragement and Tips for Continued Learning and Practice
  • License Considerations
Powered by GitBook
On this page

Was this helpful?

  1. Working With Git
  2. Working with Git - Practice Scenarios

Merging Branches and Resolving Conflicts

Let's illustrate a scenario where two branches have conflicting changes

Scenario

Conflicting Changes Let's consider a scenario where you are working on a project with a team, and you have two branches: feature-A and feature-B. Both branches have undergone significant changes, including modifications to the same files.

Step 1: Checking out the Target Branch

To begin the merge process, we need to switch to the branch where we want to merge the changes. In this scenario, let's assume we want to merge feature-B into feature-A. To switch to feature-A, execute the following command in your terminal:

git checkout feature-A

Step 2: Initiating the Merge

Now that we are on the target branch (feature-A), we can initiate the merge with the following command:

git merge feature-B

Step 3: Resolving Conflicts Manually

During the merge process, Git might identify conflicting changes in the files modified on both branches. Conflicts occur when Git is unable to determine which version of the file to keep. Git will mark these conflicts in the affected files.

To resolve conflicts manually, open the conflicted files in your preferred text editor. Within the file, Git will mark the conflicting sections using special markers. Here's an example:

<<<<<<< HEAD
# This is the version of the code from feature-A branch

def some_function():
    # Code implementation
    pass
=======
# This is the version of the code from feature-B branch

def some_function():
    # Updated code implementation
    pass
>>>>>>> feature-B

In the above example, Git has marked the conflicting changes between the HEAD (current branch) and feature-B. Manually review the code and select the desired changes or modify the code to combine the changes from both branches.

After resolving the conflicts, save the file, and proceed to the next step.

Step 4: Completing the Merge

Once you have resolved all conflicts in the affected files, you need to inform Git that the conflicts have been resolved. Use the following command:

git add <file1> <file2> ...

Replace <file1>, <file2>, and so on with the names of the files you have resolved.

Step 5: Committing the Merge

After resolving conflicts, create a new commit to finalise the merge:

git commit -m "Merge feature-B into feature-A"

Congratulations! You have successfully merged the changes from the feature-B branch into the feature-A branch, resolving conflicts along the way.

Using Merge Tools

Git also provides merge tools that assist in resolving conflicts visually. These tools offer a more user-friendly approach to conflict resolution. Popular merge tools include KDiff3, P4Merge, and Beyond Compare.

To configure and use a merge tool, you can update your Git configuration file with the desired tool's settings. Consult the documentation of your chosen merge tool for specific instructions.

Remember to carefully review the conflicting changes, make informed decisions, and create clear commit messages to maintain a clean and organised project history.

PreviousCreating and Switching Between BranchesNextBranching Strategies in a Team Project

Last updated 1 year ago

Was this helpful?

🦕