如何使用 rsync 备份没有 git 子目录的目录

Ada*_*dam 57 git rsync dropbox

我想复制我的c目录以及不包括./git子目录的所有子目录。我使用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
Run Code Online (Sandbox Code Playgroud)

我能做得更好吗?

小智 70

只需为 .git 添加一个明确的排除:

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

另一种选择是创建~/.cvsignore包含以下行以及您要排除的任何其他目录:

.git/

  • 它指定了如何给出“--exclude”的路径。如果您遇到问题,请[查看此答案](https://unix.stackexchange.com/a/83403/136348)。 (2认同)

seb*_*ner 39

你可以只使用rsync --cvs-exclude. 它还忽略.git目录。

但请注意,这也会忽略core在 Magento 源文件中调用的目录。

  • 这适用于顶级`.git` 目录,但不适用于子模块中的`.git` 目录。它也不会忽略像 `.gitmodules` 这样的 Git 文件。 (10认同)