8

What is the best way to checkout/merge all files from a branch into the working directory, that currently do not exist there, but without changing the repository state, HEAD, or existing files?

Is this possible with a one line command?

4 Answers 4

15

git checkout can take paths as an argument, which, if given, leaves HEAD alone, and just checks out those paths into your working directory and index, so you can just use:

git checkout branchname -- .

1
  • Hi Anish, unfortunately this is not an answer to my question because git checkout would change existing files in the working directory as well.
    – BlackEye
    Nov 5, 2018 at 9:32
0

Create additional clone of used local repo and checkout/merge in it. It will be really The Easiest Way (tm)

0
git diff --name-status HEAD <otherBranch> | grep -e "^A" | cut -f 2 | xargs git checkout <otherBranch> --

Does the job.

0

Have a look at git worktree.

With git worktree add <checkout_path> <otherBranch> you can checkout the given branch to the given path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.