8

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?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
kan
  • 28,279
  • 7
  • 71
  • 101
  • I don't know any other method, but I'm curious: have you tried running a benchmark to find out how the two approaches fare on, say, the `github.com/git/git` repository? – jub0bs Aug 15 '14 at 20:33
  • @Jubobs I bet second option will be faster, but may fail if `diff` would return too large list. Also it would fail if file names have spaces or special chars. – kan Aug 18 '14 at 08:53
  • What you're describing comes up more in testing/build/QA. If the idea is to switch frequently between a small number of directory states (i.e. branches), you should have all the states on the filesystem and just symlink the one you want. The result is instantaneous. – Joe Atzberger Sep 04 '14 at 01:56
  • @JoeAtzberger No, sometimes I do it when I want revert a directory back to a different revision. – kan Sep 04 '14 at 06:39

1 Answers1

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

andi5
  • 1,606
  • 1
  • 11
  • 10