How to resolve stash merge conflicts

GitGitBeginner
Practice Now

Introduction

Git stash is a powerful feature that allows developers to temporarily save uncommitted changes and switch between different tasks. This tutorial provides comprehensive insights into resolving merge conflicts when working with Git stash, helping developers effectively manage code changes and maintain a clean, organized development workflow.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git(("`Git`")) -.-> git/DataManagementGroup(["`Data Management`"]) git/BranchManagementGroup -.-> git/branch("`Handle Branches`") git/BranchManagementGroup -.-> git/checkout("`Switch Branches`") git/BranchManagementGroup -.-> git/merge("`Merge Histories`") git/BasicOperationsGroup -.-> git/status("`Check Status`") git/BasicOperationsGroup -.-> git/diff("`Compare Changes`") git/BasicOperationsGroup -.-> git/commit("`Create Commit`") git/DataManagementGroup -.-> git/reset("`Undo Changes`") git/DataManagementGroup -.-> git/stash("`Save Changes Temporarily`") subgraph Lab Skills git/branch -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/checkout -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/merge -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/status -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/diff -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/commit -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/reset -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} git/stash -.-> lab-418260{{"`How to resolve stash merge conflicts`"}} end

Git Stash Basics

What is Git Stash?

Git stash is a powerful feature that allows developers to temporarily save uncommitted changes without committing them to the repository. This is particularly useful when you need to switch branches or pull updates but have ongoing work that isn't ready to be committed.

Key Stash Commands

Command Description
git stash Saves current working directory changes
git stash list Shows all stored stashes
git stash apply Applies the most recent stash
git stash pop Applies and removes the most recent stash
git stash drop Removes the most recent stash

Basic Workflow

graph LR A[Working Directory] -->|git stash| B[Stash Area] B -->|git stash pop| A B -->|git stash apply| A

Practical Example

## Create some changes
echo "Temporary work in progress" > temp.txt

## Stash the changes
git stash

## Switch branches or perform other tasks
git checkout another-branch

## Return and apply stashed changes
git stash pop

When to Use Git Stash

  • Switching branches with uncommitted changes
  • Pausing current work to address urgent tasks
  • Cleaning up working directory temporarily

Best Practices

  1. Use descriptive messages with git stash save "description"
  2. Regularly clean up stash list
  3. Don't rely on stash for long-term code storage

LabEx recommends mastering stash techniques to improve development workflow efficiency.

Merge Conflict Overview

Understanding Merge Conflicts

Merge conflicts occur when Git cannot automatically resolve differences between two commits. This typically happens when:

  • The same part of a file is modified in different ways in two branches
  • Changes are made to the same line or nearby lines of code

Conflict Visualization

graph TD A[Branch A] -->|Modify Same Line| C{Merge Conflict} B[Branch B] -->|Modify Same Line| C C -->|Manual Resolution| D[Merged Code]

Common Conflict Scenarios

Scenario Description
Line Modification Different changes to the same line
File Deletion One branch deletes, another modifies
File Renaming Conflicts in file structure

Conflict Markers

When a conflict occurs, Git marks the file with special markers:

<<<<<<< HEAD
Current branch code
=======
Incoming branch code
>>>>>>> branch-name

Conflict Detection

## Attempt to merge branches
git merge feature-branch

## If conflict occurs
## Conflict files will be marked with conflict markers

Resolution Strategies

  1. Manual Editing
  2. Using merge tools
  3. Choosing specific changes

Practical Example

## Create conflicting changes
echo "Original content" > file.txt
git add file.txt
git commit -m "Initial commit"

## Create two branches with different modifications
git checkout -b branch-a
echo "Branch A modification" > file.txt
git commit -am "Branch A change"

git checkout main
git checkout -b branch-b
echo "Branch B modification" > file.txt
git commit -am "Branch B change"

## Attempt merge (will cause conflict)
git merge branch-a

Best Practices

  • Communicate with team members
  • Pull changes frequently
  • Use clear commit messages

LabEx recommends developing a systematic approach to handling merge conflicts efficiently.

Stash Conflict Resolution

Understanding Stash Merge Conflicts

Stash merge conflicts occur when applying a stashed change to a modified working directory creates incompatible modifications.

Conflict Resolution Workflow

graph TD A[Stash Changes] --> B{Apply Stash} B -->|Conflict Detected| C[Manual Resolution] C --> D[Resolve Conflicts] D --> E[Commit Changes]

Detailed Resolution Steps

1. Identify Conflicts

## Apply stash and detect conflicts
git stash apply

## View conflict status
git status

2. Conflict Markers

<<<<<<< Updated upstream
Current branch changes
=======
Stashed changes
>>>>>>> Stash changes

Resolution Strategies

Strategy Description Command
Manual Edit Directly edit conflicting files Manual editing
Keep Stash Use stashed changes completely git checkout --patch
Discard Stash Reject stashed changes git reset HEAD

Practical Resolution Example

## Create stash with changes
git stash save "Experimental changes"

## Apply stash with potential conflicts
git stash apply

## Manually resolve conflicts in files
nano conflicted_file.txt

## Mark conflicts as resolved
git add conflicted_file.txt

## Complete merge
git commit -m "Resolved stash merge conflicts"

Advanced Conflict Handling

Using Visual Merge Tools

## Configure merge tool
git config --global merge.tool vscode

## Resolve conflicts visually
git mergetool

Best Practices

  1. Commit or stash changes before switching branches
  2. Use descriptive stash messages
  3. Regularly clean up stash list
  4. Communicate with team about ongoing work

Common Pitfalls

  • Losing work during conflict resolution
  • Incomplete conflict resolution
  • Overwriting important changes

LabEx recommends practicing stash conflict resolution in a safe environment to build confidence and skill.

Summary

Understanding how to resolve stash merge conflicts is crucial for maintaining a smooth Git workflow. By mastering conflict resolution techniques, developers can confidently manage code changes, minimize disruptions, and ensure seamless version control across different branches and project stages.

Other Git Tutorials you may like