🐙
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
  • Process of Merging Branches in Git:
  • Different Merge Strategies

Was this helpful?

  1. Working With Git

Merging Branches and Resolving Conflicts

Merging branches is a fundamental concept in Git that allows developers to combine changes from different branches into a single branch.

This process ensures that the work done in separate branches is incorporated seamlessly. However, conflicts may arise when merging branches if changes have been made to the same lines of code.


Process of Merging Branches in Git:

Merging branches in Git involves the following steps:

Step 1: Checkout the Target Branch

First, ensure that you are in the branch where you want to merge the changes. Use the following command to checkout the target branch:

git checkout target_branch

Step 2: Merge the Source Branch

Next, merge the changes from the source branch into the target branch using the git merge command:

git merge source_branch

Step 3: Resolve Conflicts (if any)

If Git encounters conflicts during the merge, it will pause the process and mark the conflicting files. You need to resolve these conflicts manually.


Different Merge Strategies

Git provides different merge strategies to handle the merging process. Let's discuss two commonly used strategies:

Fast-forward Merge

The fast-forward merge strategy is used when the target branch's commit history does not diverge from the source branch. In this case, Git simply moves the target branch pointer forward to the latest commit of the source branch. To perform a fast-forward merge, use the following command:

git merge --ff-only source_branch

Recursive Merge

The recursive merge strategy is used when the commit histories of the branches diverge. It creates a new merge commit that combines the changes from both branches. This strategy is the default one used by Git. To perform a recursive merge, use the following command:

git merge source_branch

Resolving Merge Conflicts

Merge conflicts occur when Git cannot automatically determine how to combine conflicting changes from different branches. Here are the steps to resolve merge conflicts:

Step 1: Identify Conflicting Files

When conflicts occur, Git marks the conflicting files with conflict markers (<<<<<<<, =======, and >>>>>>>). Identify the files that have conflicts using the git status command.

Step 2: Open the Conflicting Files

Open the conflicting files in a text editor. You will see the conflicting changes marked with the conflict markers.

Step 3: Resolve Conflicts Manually

Review the conflicting changes and modify the code to resolve the conflicts. Remove the conflict markers and keep the desired changes.

Step 4: Add the Resolved Files

After resolving the conflicts, stage the resolved files using the git add command:

git add resolved_file1 resolved_file2

Step 5: Commit the Merge

Finally, commit the merge using the git commit command:

git commit -m "Merge source_branch into target_branch"

Note: It's a good practice to include a meaningful commit message describing the merge.

Merging branches in Git is essential for integrating changes from different branches. By understanding the process of merging, different merge strategies, and how to resolve merge conflicts, you can ensure a smooth collaboration and maintain a coherent codebase

PreviousCreating and Switching Between BranchesNextBest Practices for Branch Management

Last updated 1 year ago

Was this helpful?

🦕