rsync exclude according to .gitignore & .hgignore & svn:ignore like --filter=:C rsync exclude according to .gitignore & .hgignore & svn:ignore like --filter=:C git git

rsync exclude according to .gitignore & .hgignore & svn:ignore like --filter=:C


As mentioned by luksan, you can do this with the --filter switch to rsync. I achieved this with --filter=':- .gitignore' (there's a space before ".gitignore") which tells rsync to do a directory merge with .gitignore files and have them exclude per git's rules. You may also want to add your global ignore file, if you have one. To make it easier to use, I created an alias to rsync which included the filter.


You can use git ls-files to build the list of files excluded by the repository's .gitignore files.https://git-scm.com/docs/git-ls-files

Options:

  • --exclude-standard Consider all .gitignore files.
  • -o Don't ignore unstaged changes.
  • -i Only output ignored files.
  • --directory Only output the directory path if the entire directory is ignored.

The only thing I left to ignore was .git.

rsync -azP --exclude=.git --exclude=`git -C <SRC> ls-files --exclude-standard -oi --directory` <SRC> <DEST>


After the hours of research I have found exactly what I need: to sync destination folder with the source folder (also deleting files in the destination if they were deleted in the source), and not to copy to the destination the files that are ignored by .gitignore, but also not to delete this files in the destination:

rsync -vhra /source/project/ /destination/project/ --include='**.gitignore' --exclude='/.git' --filter=':- .gitignore' --delete-after

Another words, this command completely ignore files from .gitignore, both in source and in the destination.You can omit --exclude='/.git' part if want to copy the .git folder too.

You MUST copy .gitignore files from the source. If you will use LordJavac's command, the .gitignore will not be copied.And if you create a file in the destination folder, that should be ignored by .gitignore, this file will be deleted despite .gitignore.This is because you don't have .gitignore-files in the destination.But if you will have this files, the files described in the .gitignore will not be deleted, they will be ignored, just expected.