kan
kan

Reputation: 28981

Copy directory from another branch

There is a question "Git: copy all files in a directory from another branch" which shows how. But it doesn't delete files that are in the current branch, but that are deleted in the other branch.

There are few solutions I usually use:

  1. Delete the directory locally rm -r dir, then do git checkout otherBranch -- dir. It works, but is slow for large directories.

  2. git checkout dir and then git rm $(git diff --name-only otherBranch -- dir). It works, but I think there should be a better solution.

Is there an easier way to do it?

Upvotes: 9

Views: 4382

Answers (1)

andi5
andi5

Reputation: 1616

git reset otherBranch -- dir
git clean -df
git checkout .

This should update the contents of dir with those of the same directory on the other branch.

Upvotes: 3

Related Questions