How can I get all the files from one git branch, and put them into the current branch without merging?

This seems to do the trick:

git merge otherbranch --no-commit --no-ff -X theirs
git reset currentbranch

"-X theirs" is needed since "--strategy=theirs" isn't a valid strategy.

"--no-commit --no-ff" prevents the merge from going through just leaving the files in the working tree

And the reset cancels the merge state, but leaves the files.


The end result would be the same as checking out a different branch, copying the files, then going back to original branch, and just pasting on top of them.

Option 1

This is exactly the case for checkout. We tend to think that checkout is only used to get a branch to work on, but it can also be used to replace files in a current branch. Assuming that you want to replace the contents of currentbranch with the contents of otherbranch (e.g. copying all files of otherbranch and pasting them to currentbrach), then

git checkout currentbranch
git checkout otherbranch .

The command says checkout all files (.) from otherbranch and put them on top of currentbranch. Note, that unlike the copy/paste analogy, this will also remove any files in currentbranch that are not present in otherbranch. The downside is that you will lose all of the commit histories from otherbranch.

Option 2

The below sequence will:

  • List item
  • make the contents of currentbranch identical to otherbranch
    • e.g. git diff currentbranch otherbranch will show no changes
  • preserve the commit history of (otherbranch commits will be added to currentbranch)
  • show the change as a merge (so that it can be reverted)
  • leave otherbranch unchanged (skip making a temporary branch if this isn't needed)
   git checkout otherbranch
   git checkout -b temp-merge-other
   git merge currentbranch -s ours
   git checkout currentbranch
   git merge temp-merge-other --no-ff
   git diff otherbranch      # no difference
   git log           # commit history from dev is included
   git branch -D temp-merge-other

Tags:

Git