Uploading files to an FTP server can be made easier by only uploading the changes that happened since the last upload. This can be achieved using Git-ftp which is an FTP client using Git to determine which files to upload to or delete on the remote server.

Git-ftp tracks the uploaded files by storing the commit ID in a log file in the server. Git is used to determine the changes happening on the local server. You can easily deploy another branch or go back in the Git history to upload an older version.

Git-ftp Actions

Just like ‘git’ command, ‘git-ftp’ uses various actions to make changes to the uploaded files. The actions are as discussed below:

  • init: Uploads all git-tracked non-ignored files to the remote server and creates the .git-ftp.log file containing the SHA1 of the latest commit.
  • catchup: Creates or updates the .git-ftp.log file on the remote host and assumes that you had already uploaded other files probably using another program.
  • push: Uploads files that have changed and deletes files that have been deleted since the last upload.
  • download: (EXPERIMENTAL) : Downloads changes remote host to your working tree. The feature requires lftp installed and does not use any power of Git. Note that it can delete local untracked files not listed in .git-ftp-ignore file.
  • pull: (EXPERIMENTAL) : Downloads changes from remote host to a separate commit and merges that into your current branch. If you just want to download the files without a merge, consider download. Pull also requires lftp to be installed.
  • snapshot: (EXPERIMENTAL) : Like download and pull, snapshot requires lftp to be installed. It downloads files to a new Git repository and takes an additional argument as local destination directory.
  • show: Downloads last uploaded SHA1 from log and hooks `git show`.
  • log: Downloads last uploaded SHA1 from log and hooks `git log`
  • add-scope: Creates a new scope.
  • remove-scope: Remove a scope.
  • help: Displays help screen.

Git-ftp Options

Below are some of the most commonly used options with Git-ftp.

  • -u [username], –user [username] : FTP login name. If no argument is given, local user will be taken.
  • -p [password], –passwd [password] : FTP password.
  • -P, –ask-passwd : Ask for FTP password interactively.
  • -k [[account]@[host]], –keychain [[account]@[host]] : FTP password from KeyChain (macOS only).
  • -a, –all : Uploads all files of current Git checkout.
  • -c, –commit : Sets SHA1 hash of last deployed commit by option.
  • -A, –active : Uses FTP active mode and works works only if you have either no firewall and a direct connection to the server or an FTP aware firewall.
  • -b [branch], –branch [branch] : Push a specific branch
  • -s [scope], –scope [scope] : Using a scope (e.g. dev, production, testing, foobar). See SCOPE and DEFAULTS section for more information.
  • -l, –lock : Enable remote locking.
  • -D, –dry-run : Does not upload or delete anything, but tries to get the .git-ftp.log file from remote host.
  • -f, –force : Does not ask any questions, it just does.

In this guide, we are going to be using FTP server on Ubuntu 20.04. Let us first set up our FTP server.

Install FTP server with VSFTPD on Ubuntu

Run the following commands to install FTP server on Ubuntu Linux with vsftpd.

sudo apt-get update
sudo apt-get install vsftpd

Once installed, configure vsftp as per your requirements. Let’s first back up the original configuration file:

sudo mv /etc/vsftpd.conf /etc/vsftpd.conf_orig
sudo vim /etc/vsftpd.conf

Add the below content which should work for a basic FTP server.

listen=NO 
listen_ipv6=YES 
anonymous_enable=NO 
local_enable=YES 
write_enable=YES 
local_umask=022 
dirmessage_enable=YES 
use_localtime=YES 
xferlog_enable=YES 
connect_from_port_20=YES 
chroot_local_user=YES 
secure_chroot_dir=/var/run/vsftpd/empty 
pam_service_name=vsftpd 
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem 
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key 
ssl_enable=NO 
pasv_enable=Yes 
pasv_min_port=10000 
pasv_max_port=10100 
allow_writeable_chroot=YES

Allow FTP traffic through the firewall

sudo ufw allow from any to any port 20,21,10000:10100 proto tcp

Restart vsftpd to make the changes take effect:

sudo systemctl restart vsftpd

Create FTP user

$ sudo useradd -m ftpuser 
$ sudo passwd ftpuser
New password: 
Retype new password:  
passwd: password updated successfully

Configure FTP client to use Git-ftp

We need to install Git-ftp on our ftp client. I am using Ubuntu 20.04 (192.168.50.2) and I am going to run the command below to install git-ftp

sudo apt-get -y install git-ftp

Once installed initialize your Git repo and upload files to the remote server for the first time as below: If you were not already working on git directory, you need to first run the following commands:

$ git init
$ git add
$ git commit -m “first commit”

Now upload to your remote server with the following command

$ git-ftp init -u ftpuser -P ftp://192.168.50.2:21/test 
Password:  
7 files to sync: 
[1 of 7] Buffered for upload '.bash_history'. 
[2 of 7] Buffered for upload '.bash_logout'. 
[3 of 7] Buffered for upload '.bashrc'. 
[4 of 7] Buffered for upload '.cache/motd.legal-displayed'. 
[5 of 7] Buffered for upload '.profile'. 
[6 of 7] Buffered for upload '.sudo_as_admin_successful'. 
[7 of 7] Buffered for upload 'test.txt'. 
Uploading ... 
Last deployment changed from  to eb17fe020727c7c1d3cb8b5c453bbce2fb2c0373.

Here, ftp-git is tracking the files and uploading them to the ftp server. If your server already had files and you wish to track them with with ftp-git, you can download all the files and initialize git repo. Then run the command below:

$ git-ftp -u ftpuser -P catchup ftp://192.168.50.2:21/test 
Password:  
Last deployment changed from  to eb17fe020727c7c1d3cb8b5c453bbce2fb2c0373.

Instead of constantly passing username, password and url, you can add default settings using ‘config’ command as below:

git config git-ftp.url "ftp://192.168.50.2:21/test"
git config git-ftp.user "ftpuser" 
git config git-ftp.password "secr3t"

Now you can run git ftp commands without specifying credentials as below:

$ git ftp push
1 file to sync: 
[1 of 1] Buffered for upload 'test.txt'. 
Uploading ... 
Last deployment changed from eb17fe020727c7c1d3cb8b5c453bbce2fb2c0373 to 59d3be56ca0d11bc077ed57b72ac3fde09063478.

For catchup command:

$ git ftp catchup
Last deployment changed from  to 59d3be56ca0d11bc077ed57b72ac3fde09063478.

In this guide, we have looked at how to upload and make changes to files in an ftp server using ‘Git-ftp’. The command shows how we can save a lot of time and bandwidth in making changes to the ftp server files. I hope the guide has been useful. Below are more interesting Linux guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here

six + eight =