Applying the changes from branch A to B, without merging or adding commits

GitMergeBranch

Git Problem Overview


My scenario is that I have one branch in which I've made big improvements to the build process (branch A) and in another I'm working on a unrelated feature (branch B). So now when I'm hacking away at branch B, I want to pull in the stuff I wrote in branch A because I want faster and easier builds. However, I don't want to "pollute" my branch B, just add changes from branchA to unstaged changes.

What I've tried (when standing on branchB):

git merge --no-commit branchA

Doesn't work because it puts you inside a merge. If it didn't, it would be perfect.

git checkout branchA -- .

Doesn't work because it applies changes between branchA..branchB and not the changes master..branchA.

Anything else?

Edit: Yes, changes on branch A are committed. In this example there is only one branch with build improvements, but there may be up to N branches with build improvements that I want to apply while working on a feature branch.

Git Solutions


Solution 1 - Git

I just had to do something similar and was able to fix it by adding --squash to the merge command

git merge --no-commit --squash branchA
git reset HEAD # to unstage the changes

Solution 2 - Git

cherry-pick -n should do what you want, but I'm not sure why you want the build improvements as unstaged changes - that just makes several things harder (e.g. merging other changes to the modified files, or rebasing anything).

> In this example there is only one branch with build improvements, but there may be up to N branches with build improvements that I want to apply while working on a feature branch.

In that case I would create a new branch, C, which you merge from both A and B (and any other branches with build improvements). Commit changes on the feature branch, B, then merge them to the C branch, which now contains the build improvements and the feature branch changes, so you can test them together. If you need to make more changes do it in the appropriate branch, not C, then merge to C. So never change anything in the C branch, just use it to integrate changes from other branches.

That means you can use all the features of Git in branch C, instead of juggling uncommitted changes in a dirty tree.

Solution 3 - Git

I'm not 100% sure I understood it clearly, but in my case, I've just created diff patch between branches and then applied this patch on the B branch.

Inside branch A:

 git diff branchA..branchB > patch.diff
 git apply patch.diff

Solution 4 - Git

You should be able to cherry-pick the commits (with -n to avoid committing right away).

Solution 5 - Git

I'm not sure I understand your requirements.

You can run a merge, then call git reset HEAD~1.


The following sequence should replay every commit between master and branchA on top of branchB. Commits which were already applied on branchB will be skipped.

# start from branchA
git checkout branchA
# create a temporary branch wip
git checkout -b wip
# use rebase to replay each commit between master and wip on branchB
git rebase --onto branchB master wip

# if you want to remove all the commit history and only keep the resulting diffs,
# use git reset
git reset branchB

# change the active branch
git checkout branchB
# remove temp branch
git branch -d wip

Solution 6 - Git

Since git version 2.23, you can use git restore to achieve the desired result precisely.

git restore provides options to specify which files will be "restored" via --staged and --worktree. It also provides an option to specify to which status the files will be restored via --source, which is working copy by default and can accept any git object.

Thus the question can be phrased as "restore all files in worktree to status of all files in branchA" and the desired outcome can be achieved with

git restore --source branchA --worktree .

Instead of all files (.), specific files can be given as arguments.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBjörn LindqvistView Question on Stackoverflow
Solution 1 - GitguilfferView Answer on Stackoverflow
Solution 2 - GitJonathan WakelyView Answer on Stackoverflow
Solution 3 - Gitsobi3chView Answer on Stackoverflow
Solution 4 - GitLuceroView Answer on Stackoverflow
Solution 5 - GitLeGECView Answer on Stackoverflow
Solution 6 - Gitlearning2codeView Answer on Stackoverflow