Rsync includes a nifty option --cvs-exclude
to “ignore files in the same way CVS does”, but CVS has been obsolete for years. Is there any way to make it also ex
rsync -ah --delete
--include .git --exclude-from="$(git -C SRC ls-files \
--exclude-standard -oi --directory >.git/ignores.tmp && \
echo .git/ignores.tmp')" \
SRC DST
Details: --exclude-from
is mandatory instead of --exclude because likely case that exclude list would not be parsed as an argument. Exclude from requires a file and cannot work with pipes.
Current solution saves the exclude file inside the .git folder in order to assure it will not affect git status
while keeping it self contained. If you want you are welcome to use /tmp.
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 to 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.
Try this:
rsync -azP --delete --filter=":- .gitignore" <SRC> <DEST>
It can copy all files to remote directory excluding files in '.gitignore', and delete files not in your current directory.
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>
I had a number of very large .gitignore
files and none of the "pure rsync" solutions worked for me. I wrote this rsync wrapper script, it fully respects .gitignore
rules (include !
-style exceptions and .gitignore
files in subdirectories) and has worked like a charm for me.
Short answer
rsync -r --info=progress2 --filter=':- .gitignore' SOURCE DEST/
Parameters meaning:
-r
: recursive
--info=...
: show progress
--filter=...
: exclude by the rules listed on the .gitignore file