68

I have folder name utils I want to copy this folder from master branch to work branch. How do I do that ?

3

1 Answer 1

129

To copy the folder over:

$ git checkout work
Switched to branch 'work'
$ git checkout master -- utils
$ git add utils
$ git commit -m "Adding 'utils' directory from 'master' branch."
[work 9fcd968] Adding 'utils' directory from 'master' branch.
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 utils/file

If you want to delete it on master after that:

$ git checkout master
Switched to branch 'master'
$ git rm -r utils
rm 'utils/file'
$ git commit -m "Removing 'utils' directory."
[master c786f95] Removing 'utils' directory.
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 utils/file

Then you can just git push as necessary. Git's output in your project may be different; I just made a simple test repo here with only one file in the utils directory.

7
  • 4
    Just for consideration, when work and master get merged again there will be a conflict because of the different operations that happened on each side. Aug 2, 2013 at 19:43
  • Interestingly I tried this. And when I merged my branch back to master, I had no conflict. Maybe git has gotten smarter?
    – clinux
    May 23, 2016 at 23:30
  • I had an issue but I fixed it adding origin: git checkout origin/master -- PUBLISH Mar 31, 2020 at 16:51
  • @Carl Norum does the copy example also applies to changing the ALREADY EXISTING IN WORK BRANCH utils folder onto the master version?
    – aerijman
    Jan 28, 2021 at 18:43
  • I'm not sure I follow - if you want to get changes from your work branch back onto maaster, just rebase or merge.
    – Carl Norum
    Jan 28, 2021 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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