DevOps Classroom notes 07/Jul/2026

Git Merge, Rebase, Cherry-pick, and Stash

Git Merge

git merge combines changes from one branch into another. It creates a new merge commit that ties together the histories of both branches (unless a fast-forward is possible).

Types of Merge

Fast-forward merge — happens when the target branch has no new commits since the feature branch was created. Git simply moves the pointer forward.

Three-way merge — happens when both branches have diverged (have new commits). Git creates a new merge commit with two parents.

Example

# Start on main
git checkout main

# Merge feature branch into main
git merge feature-login

Before merge:

main:     A---B---C
                    \
feature:             D---E

After merge (three-way):

main:     A---B---C-------M
                    \     /
feature:             D---E

Common flags

git merge --no-ff feature-login   # force a merge commit even if fast-forward is possible
git merge --abort                 # cancel a merge in progress (e.g., during conflicts)
git merge --squash feature-login  # combine all feature commits into one, staged but not committed

Git Rebase

git rebase moves or replays commits from one branch onto another. Instead of creating a merge commit, it rewrites history so your commits appear as if they were made on top of the target branch.

Example

git checkout feature-login
git rebase main

Before rebase:

main:     A---B---C
                    \
feature:             D---E

After rebase:

main:     A---B---C
                    \
feature:             D'---E'

Note: D' and E' are new commits with new hashes — the original D and E are replaced.

Interactive Rebase

Used to edit, squash, reorder, or drop commits before they’re shared.

git rebase -i HEAD~3

This opens an editor listing the last 3 commits:

pick a1b2c3 Add login form
pick d4e5f6 Fix typo
pick g7h8i9 Add validation

Common actions:

  • pick — keep commit as is
  • squash (or s) — combine with previous commit
  • reword (or r) — edit commit message
  • drop (or d) — remove commit

Git Cherry-pick

git cherry-pick applies a specific commit from one branch onto another, without merging the entire branch.

Example

# You're on main, and want commit e5f6g7 from feature-login
git checkout main
git cherry-pick e5f6g7

Before:

main:     A---B---C
                    \
feature:             D---E---F
                          ^
                   (want only E)

After:

main:     A---B---C---E'

Common flags

git cherry-pick e5f6g7 g8h9i0     # cherry-pick multiple commits
git cherry-pick --continue        # after resolving a conflict
git cherry-pick --abort           # cancel the cherry-pick
git cherry-pick -n e5f6g7         # apply changes without committing (stage only)

Use Case

A hotfix was committed to a release branch, and you want that same fix in main without merging all of release’s other commits.

gitexercises


Quick Reference Summary

Command Purpose
git merge <branch> Combine two branches’ histories, creating a merge commit
git rebase <branch> Replay commits onto another branch for linear history
git rebase -i Edit, squash, reorder, or drop commits interactively
git cherry-pick <commit> Apply a single commit from elsewhere onto current branch

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube