Skip to content

Latest commit

 

History

History
161 lines (115 loc) · 3.9 KB

2. Merging Changes.md

File metadata and controls

161 lines (115 loc) · 3.9 KB

Merging Changes

Merging in Git is the process of integrating changes from one branch into another. It’s a critical operation in collaborative workflows to combine the work of different contributors or merge feature branches into the main codebase.


1. What is Merging?

Merging takes the changes made in one branch and applies them to another. The most common scenario is merging a feature branch into the main branch after development is complete.

Types of Merges

  • Fast-Forward Merge: Applies changes directly when there’s a linear path between the branches.
  • Three-Way Merge: Creates a merge commit when the branches have diverged.

2. How to Merge Changes

Fast-Forward Merge

  1. Switch to the branch you want to merge into (e.g., main):

    git checkout main
  2. Merge the feature branch:

    git merge feature_branch

    If there are no conflicts, Git will fast-forward the branch pointer to include the changes.


Three-Way Merge

A three-way merge is needed when the branches have diverged.

  1. Switch to the branch you want to merge into:

    git checkout main
  2. Merge the feature branch:

    git merge feature_branch
  3. If there are conflicts, resolve them manually (see below).


3. Resolving Merge Conflicts

A merge conflict occurs when changes in two branches affect the same part of a file.

Steps to Resolve Conflicts

  1. Identify the conflicting files:

    git status
  2. Open the conflicting files and look for conflict markers:

    <<<<<<< HEAD
    Your changes
    =======
    Changes from the other branch
    >>>>>>> feature_branch
    
  3. Edit the file to resolve the conflict and remove the markers.

  4. Mark the conflict as resolved:

    git add file_with_conflict
  5. Complete the merge:

    git commit

4. Aborting a Merge

If you decide not to proceed with a merge:

git merge --abort

This will return the branch to its original state before the merge began.


5. Viewing Merge History

To see merges in the commit history:

git log --oneline --graph

Example Workflow: Feature Branch Merge

Step 1: Create a Feature Branch

git checkout -b feature/new-feature

Step 2: Make Changes and Commit

echo "New feature" > feature.txt
git add feature.txt
git commit -m "Add new feature"

Step 3: Switch to main

git checkout main

Step 4: Merge the Feature Branch

git merge feature/new-feature

Step 5: Delete the Merged Branch

git branch -d feature/new-feature

6. Best Practices for Merging

  • Pull Latest Changes: Before merging, ensure your branch is up to date with the target branch:
    git pull origin main
  • Test After Merging: Verify that the code works as expected after a merge.
  • Communicate Conflicts: If conflicts arise, collaborate with the team to resolve them effectively.

Common Commands for Merging

Command Description
git merge branch_name Merge the specified branch into the current branch.
git merge --abort Abort a merge in progress.
git log --oneline --graph View a graphical history of commits and merges.
git status Identify conflicting files during a merge.

Conclusion

Merging changes is a fundamental Git operation for integrating code from different branches. By understanding merge strategies and conflict resolution, you can ensure a smooth and efficient workflow.


Next Steps: Resolving Merge Conflicts