Home > Software design >  git: merge some changes from one branch to another without recording a merge
git: merge some changes from one branch to another without recording a merge

Time:08-18

Suppose I have branches A and B with a common ancestor, which have diverged. I want to apply some of the changes on branch A to branch B, but without recording that A has been merged into B, so that if I try to do a "real" merge of A into B I still get the remaining differences.

I can do this by:

  • merging without committing
  • creating a patch file from the uncommitted changes
  • aborting the merge
  • applying the patch
  • reverting the changes I don't want for now
  • committing it as a normal (non-merge) commit

but surely there is an easier way?

Context: I have a base branch (say "main") and a feature branch with many changes on it (say "feature"). I want to create a pull request with only some of the changes on the feature branch, so I create a new branch for that ("pr1"). Eventually the rest of the feature branch will probably be merged into "main", so I don't want to record in git history that all of "feature" has been merged into "pr1", as once that's merged it will look as if all of "feature" has been merged into "main" when it hasn't.

Edit: the changes I want in the PR are not neatly contained in a few commits. There have been maybe a hundred commits on the feature branch, including some merges, and I just want to take the current state of it and submit some of it for review.

CodePudding user response:

You're probably overthinking this. Why not just reconstruct your existing commits so that they consist of commits you want in pr1 followed by commits you don't want in pr1? Then you just make a branch at the last commit that does go into pr1...

A -- B -- C (main)
     \
      W -- X -- Y -- Z (feature)
         (pr1)

... and push it to form pr1 and now go right on working on feature.

To rearrange how the commits go, use git reset --mixed all the way back to the LCA. See my essay on Three Types of Regret; this is Type 2.

https://stackoverflow.com/a/59675191/341994

CodePudding user response:

Assumptions:

  • Some, but not all changes from branch A is to be merged into branch B but without in being a merge commit.
  • No untracked or modified files.

Here is how to create a merge commit without it being recorded as a merge commit:

git checkout -b merge-from-A B
git merge A                         # If conflicts, resolve those and commit.

Do whatever modifications needed to exclude what you do not want to bring in from branch A and amend or create a new commit:

# Alternative 1
git commit --amend

# Alternative 2
git add .
git commit -m "Remove/disable blah blah because blah blah"

Now you have the changes you want from branch A merged into branch merge-from-A. Here is how to bring those into branch B without recording it as a merge:

git chechout B
git diff HEAD merge-from-A | git apply -
git add .

# Alternative 1
git commit -m "Partially merge of blah blah changes from branch A"
git branch -d merge-from-A

# Alternative 2
git commit -m "Partially merge of blah blah changes from branch A" -m "Command run: git diff HEAD merge-from-A | git apply -"
# Keep merge-from-A branch for reference

By my preferences, the alternative 2 choices are the best.

  • Related