Skip to content
Merging & Stashing

Summary - Merging & Stashing

Introduction to Merges

Merging combines changes from different branches into a single branch.
Useful for bringing together work from multiple contributors.

Importance of Merges

Enhances collaboration by integrating efforts from multiple contributors.
Keeps the project consistent and up-to-date.

When to Use Merges

After completing a feature or bug fix on a separate branch.
Before deploying new changes to include all updates.

Merging in Action

Steps for Merging

Checkout the Main Branch:
Command: git checkout main
Merge the Branch:
Command: git merge feature-branch

Example

Merging feature-branch into main branch:
Command: git checkout main followed by git merge feature-branch

What is a Merge Conflict?

Occurs when Git can't automatically combine changes from different branches.
Requires manual intervention to resolve.

Common Causes

Simultaneous modifications to the same line of code.
Deletions in one branch and modifications in another.

Identification

Resolving a Merge Conflict

Steps to Resolve

Identify the Conflict:
Look for conflict markers: <<<<<<<, =======, >>>>>>>
Edit the File:
Manually reconcile differences.
Mark as Resolved:
Command: git add <file>
Complete the Merge:
Command: git commit -m "<commit message>"

Different Types of Merges

Fast-Forward Merge

Moves the pointer forward without creating a new commit.
Occurs when the target branch is an ancestor of the current branch.
Command: git merge or git merge --ff-only

Regular Merge

A three-way merge creating a new merge commit.
Used when branches have diverged.
Command: git merge

No Fast-Forward Merge

Creates a new merge commit even if a fast-forward merge is possible.
Preserves the history of merges.
Command: git merge --no-ff

Trying Out Different Merges

Squash Merge

Combines all commits in a feature branch into a single commit before merging.
Keeps commit history clean.
Command: git merge --squash

Steps for Squash Merge

Prepare for Squash:
Ensure branches are up-to-date.
Execute Squash:
Command: git merge --squash
Commit the Squash:
Command: git commit

What is Stashing?

Temporarily shelves changes in the working directory without committing them.
Useful for saving work in progress when switching tasks.

Basic Commands

Save Stash:
Command: git stash
Apply Stash:
Command: git stash apply
List Stashes:
Command: git stash list

Stashing in Action

Steps to Stash Changes

Stash Current Changes:
Command: git stash
Switch Branches:
Command: git checkout branch-name
Apply Stash:
Command: git stash pop or git stash apply

Managing Stashes

View Stashes:
Command: git stash list
Apply Specific Stash:
Command: git stash apply stash@{index}
Drop Stash:
Command: git stash drop stash@{index}

Stash Untracked Files

Command: git stash -u or git stash --include-untracked
These concepts of merging and stashing will help you master Git. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.