# in case branchA is not our current branchgit checkout branchA
# make merge commit but without conflicts!!# the contents of 'ours' will be discarded latergit merge -s ours branchB
# make temporary branch to merged commitgit branch branchTEMP
# get contents of working tree and index to the one of branchBgit reset --hard branchB
# reset to our merged commit but# keep contents of working tree and indexgit reset --soft branchTEMP
# change the contents of the merged commit# with the contents of branchBgit commit --amend
# get rid off our temporary branchgit branch -D branchTEMP
# verify that the merge commit contains only contents of branchBgit diff HEAD branchB
git checkout <baseBranch> // this will checkout baseBranchgit merge -s ours <newBranch> // this will simple merge newBranch in baseBranchgit rm -rf . // this will remove all non references files from baseBranch (deleted in newBranch)git checkout newBranch -- . //this will replace all conflicted files in baseBranch
# Get the content you want to keep.# If you want to keep branchB at the current commit, you can add --detached,# else it will be advanced to the merge commit in the next step.git checkout branchB
# Do the merge an keep current (our) content from branchB we just checked out.git merge -s ours branchA
# Set branchA to current commit and check it out.git checkout -B branchA
git checkout branchA
# Do a merge commit. The content of this commit does not matter,# so use a strategy that never fails.# Note: This advances branchA.git merge -s ours branchB
# Change working tree and index to desired content.# --detach ensures branchB will not move when doing the reset in the next step.git checkout --detach branchB
# Move HEAD to branchA without changing contents of working tree and index.git reset --soft branchA
# 'attach' HEAD to branchA.# This ensures branchA will move when doing 'commit --amend'.git checkout branchA
# Change content of merge commit to current index (i.e. content of branchB).git commit --amend -C HEAD
# Start from the branch that is going to receive the merge.git switch our_branch
# Create the merge commit, albeit with the wrong tree.git merge -s ours their_branch
# Replace our working tree and our index with their tree.git restore --source=their_branch --worktree --staged :/
# Put their tree in the merge commit.git commit --amend
警告:git restore是一个相当新的命令,在git中引入2.23.git help restore警告