12

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

24

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 -- .

3
  • 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
  • @BlackEye why not git stash, run Anish's suggested command, then git stash pop?
    – Daniel
    Jul 18, 2022 at 20:06
  • @Daniel git stash would only store uncommited changes compared to HEAD
    – BlackEye
    Jul 19, 2022 at 21:27
1

Have a look at git worktree.

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

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.

1
  • This fails for files whose names include spaces.
    – Théophile
    Sep 26, 2022 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .