Applying the changes from branch A to B, without merging or adding commits
Categories:
Applying Git Changes Without Merging or Committing

Learn how to transfer specific changes from one Git branch to another without performing a full merge or creating new commits, using techniques like git cherry-pick
and git diff
.
In Git, developers often need to move changes between branches. While git merge
and git rebase
are common for integrating entire branches, there are scenarios where you only want to apply specific changes from one branch to another, without bringing over all commits or creating new merge commits. This article explores methods to achieve this, focusing on git cherry-pick
for individual commits and git diff
for uncommitted changes or patches.
Understanding the Challenge
The core challenge lies in isolating the desired changes. A typical git merge
integrates all changes from the source branch into the target. If the source branch contains work-in-progress, experimental features, or commits that are not yet ready for the target branch, a full merge is undesirable. Similarly, if you have uncommitted changes on one branch that you want to apply to another without first committing them, standard Git commands don't offer a direct path.
flowchart TD A[Feature Branch A] --> B{Desired Changes?} B -->|Yes| C[Apply to Branch B] B -->|No| D[Ignore] C --> E[Branch B (Updated)] style A fill:#f9f,stroke:#333,stroke-width:2px style E fill:#bbf,stroke:#333,stroke-width:2px
Conceptual flow of selectively applying changes between branches.
Method 1: Applying Specific Commits with git cherry-pick
git cherry-pick
is the most straightforward way to apply a specific commit from one branch to another. It takes the changes introduced by an existing commit and tries to reapply them as a new commit on your current branch. This means the commit ID will change, but the content of the changes will be the same.
1. Identify the Commit
First, identify the SHA-1 hash of the commit you want to cherry-pick from the source branch (e.g., branch-A
). You can use git log
to find this.
2. Switch to Target Branch
Switch to the branch where you want to apply the changes (e.g., branch-B
).
3. Cherry-Pick the Commit
Execute the git cherry-pick
command with the commit hash. If there are conflicts, resolve them and then git cherry-pick --continue
.
# 1. On branch-A, find the commit hash
git log --oneline
# Example output: 1a2b3c4 My desired change
# 2. Switch to the target branch
git checkout branch-B
# 3. Cherry-pick the commit
git cherry-pick 1a2b3c4
Using git cherry-pick
to apply a specific commit.
git cherry-pick <hash1> <hash2> <hash3>
. Git will apply them sequentially, creating a new commit for each.Method 2: Applying Uncommitted Changes or Patches with git diff
Sometimes, you have changes in your working directory or staging area on one branch that you haven't committed yet, but you want to apply them to another branch. Or, you might want to apply a set of changes that aren't neatly encapsulated in a single commit. For these scenarios, git diff
combined with git apply
is very powerful.
sequenceDiagram participant User participant BranchA as "Branch A" participant BranchB as "Branch B" User->>BranchA: Make uncommitted changes User->>BranchA: git diff > patch.diff User->>BranchB: git checkout BranchB User->>BranchB: git apply patch.diff BranchB-->>User: Changes applied to working directory
Sequence for applying uncommitted changes using git diff
and git apply
.
1. Generate a Patch File
On the source branch (e.g., branch-A
), generate a patch file containing the desired changes. This can be from uncommitted changes, or between two commits, or even between two branches.
2. Switch to Target Branch
Switch to the branch where you want to apply the changes (e.g., branch-B
).
3. Apply the Patch
Apply the generated patch file to your current branch's working directory. The changes will appear as uncommitted modifications.
# Scenario 1: Applying uncommitted changes from working directory
# On branch-A:
git diff > my_changes.patch
# Switch to branch-B:
git checkout branch-B
# Apply the patch (changes will be in working directory, not committed)
git apply my_changes.patch
# Scenario 2: Applying changes between two specific commits
# On any branch:
git diff <commit-hash-before> <commit-hash-after> > specific_range.patch
# Switch to branch-B:
git checkout branch-B
# Apply the patch
git apply specific_range.patch
# Scenario 3: Applying changes between two branches (e.g., all changes unique to branch-A compared to main)
# On any branch:
git diff main..branch-A > branch_diff.patch
# Switch to branch-B:
git checkout branch-B
# Apply the patch
git apply branch_diff.patch
Generating and applying patches with git diff
and git apply
.
git apply
, the changes are applied directly to your working directory. They are not automatically committed. You will need to git add
and git commit
them manually after reviewing.Both git cherry-pick
and git diff
/git apply
provide powerful ways to manage changes across branches without full merges. Choose the method that best suits whether your changes are already committed or still in progress, and the granularity you need.