68

I want to copy my c directory with all subdirectories excluding ./git subdirectory. I do it using rsync :

echo "copy c and sh files "
rsync -a --include='*.c' --include='*.sh' --include='*/' --exclude='*' ~/c/ ~/Dropbox/Public/c
# remove .git directory = do not send it to dropbox. Thx to Tomasz Sowa
rm -rf ~/Dropbox/Public/c/.git

Can I do it better?

Anthon
  • 78,313
  • 42
  • 165
  • 222
Adam
  • 969
  • 1
  • 7
  • 15

2 Answers2

79

Just add an explicit exclude for .git:

rsync -a --exclude='.git/' --include='*.c' --include='*.sh' --include='*/' --exclude='*' ~/c/ ~/Dropbox/Public/c

Another option is to create ~/.cvsignore containing the following line along with any other directories you'd like to exclude:

.git/

Quetza
  • 996
  • 7
  • 2
  • 2
    It is specified how you should give the path for `--exclude`. If you have problems with it, [check out this answer](https://unix.stackexchange.com/a/83403/136348). – totymedli Jan 23 '18 at 01:34
50

You can just use rsync --cvs-exclude. It also ignores .git directories.

But take care, that this also ignores directories called core as occurring in Magento source files.

sebastianwagner
  • 734
  • 6
  • 7
  • 16
    This works for the top level `.git` directory but not for `.git` directories in submodules. It also does not ignore Git files like `.gitmodules`. – maxschlepzig Jul 17 '14 at 17:26
  • Note: this does not work with default rsync version on latest MacOS, which is 2.6.9 as of March 2023. To get a modern rsync where --cvs-exclude option has been expanded to also exclude .git you need a newer version of rsync. "brew install rsync" will install 3.2.7 as of March 2023! – Peter H. Boling Mar 29 '23 at 16:32