Syncing sftp file system with version control

可紊 提交于 2020-01-03 17:36:28

问题


I've recently started managing a (small-ish) website for a university society, and apparently (according to the last person who managed it) the way to access the files for the website is by SFTP. I use Linux Mint/Nautilus, so this is no problem. However I also have a local copy to test my changes with Apache/localhost before uploading (ignoring for the moment that I can't really get that to work...). I use Git to keep track of my changes.

Is there any way I can diff between the SFTP version and my own version, and then only update the changed files (basically some kind of sync over SFTP), and possibly incorporate the version control into that as well?

By the way, I have very little prior experience with this, so explanations would be appreciated. Same goes for reasonably fool-proof methods to do things, rather than necessarily the most sleek but horrible to configure way.

Thanks!


回答1:


Use Rsync Over SSH

If they are running a true SSH server, rather than an SFTP-only server, then you can use rsync to push your local changes to the remote. For example:

cd /my/project/directory
rsync -PSauve ssh --exclude=.git * $LOGNAME@example.com:/var/www/

You might also want to use the --delete option, but be cautious if there are likely to be files that exist only on the server side that need to be preserved.

Use SFTP for Tree Operations

If you can't use rsync, you could copy the upstream files into a local branch, diff them, merge whatever changes you want, and then push the results back up. For example:

cd /my/project/directory
git checkout -b upstream
sftp -r user@example.com:/var/www/\* .
git add .
git commit -m 'Import current files from upstream.'
git checkout master
git diff upstream
: # merge or modify files as needed
sftp user@example.com:/var/www < <(echo 'put -r *')

Use SCP instead of SFTP

If you can't use rsync, SCP is often much nicer than SFTP for non-interactive operations. For example:

scp -r . user@example.com:/var/www/

SCP offers a cleaner interface, and supports symlinks (which SFTP doesn't), but may have other limitations for your use case. In particular, it may not print the names of empty directories that it copies; whether or not that matters is up to you.



来源:https://stackoverflow.com/questions/14714178/syncing-sftp-file-system-with-version-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!