Checkout all files from branch, without changing HEAD nor existing files

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

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

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

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

Does the job.

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like