Cloning My Site's Git Repo on Android to Write on the Go
I’m writing this entirely from my Android phone! I spent quite a few hours trying to figure out how to get a clone of my git repository on here and hit a lot of dead ends. I tried multiple sets of instructions for AIDE and mGit with no success. Finally I found the Termux app which actually supports everything I need! For some reason Termux was not coming up in any of my searches for git clients on Android. Please improve your SEO!
With Termux I was able to get my repository cloned from GitHub just like I would on desktop. Now I should be able to write directly to my site on the go. In the future I’m going to try to get Hugo itself running on here. There’s some indication that this is possible. But for now I will just be copying the frontmatter from previous articles to create new ones. I’ll check in the articles with git and then run Hugo and update my website the next time I’m in front of a computer.
Even with Termux, setup was not entirely straight forward.
I used an archived copy of the guide from pedronveloao.com as a reference. It appears to be down but a cached copy is available at archive.org. Note that this guide was missing steps for setting up the ssh agent.
Specifically I needed to run
to start the ssh auth client and
to add the keys that I had moved to my ~/.ssh folder.
In addition I found the instructions for moving keys to the proper place to be confusing. They might make sense if you are using the same set of keys for multiple devices, but I would recommend using a separate set for your phone. That way you can disallow them later if it gets stolen or lost. Luckily OpenSSH creates some keys on install that I was able to use.
To copy those keys to the default location for the ash agent I ran the following:
cd /data/data/com.termux/files/usr/etc/ssh cp ssh_host_ed25519_key ~/.ssh/ cp ssh_host_ed25519_key.pub ~/.ssh/
So to sum it all up:
- Install the Termux app with F-Droid
apt update && apt upgrade
pkg install termux-api
apt install git
apt install openssh
cp ssh_host_ed25519_key ~/.ssh/
cp ssh_host_ed25519_key.pub ~/.ssh/
termux-setup-storage(to give Termux access to app shared storage. Note that a permissions pop-up will appear)
- If your ssh pub key is not setup on GitHub yet, then you should add it now. You can copy it to your clipboard with
cat ~/.ssh/ssh_host_ed25519_key.pub | termux-clipboard-set. Just make sure it is the public key and not the private one!
- Confirm the addition with
cd ~/storage/shared/\<desired git project parent folder\>
- git clone as you would on any normal computer
- Do some work - I’m using hugo more info here
- Attempt git commit as normal
- On screen instructions will have you setup your user.name and user.email
- Run git commit again.
- Use vim to write a commit message. At this point you need to know how to get the escape key for use with vim. It’s “Volume Up + e”. There are more useful keyboard shortcuts listed on Termux’s website. If you want to forget all that, use the -m flag for your message.
- git push
After that there are a number of editors available that will let you update the files. I’m using Epsilon right now because I’m only going to be doing Markdown editing for Hugo. To be honest I’m not terribly impressed, but it does seem to do an ok job.
Because of the way OpenSSH works, I believe you probably need to rerun the eval $(ssh-agent) and ssh-add commands on every restart. I bet Termux has a good way of dealing with this (maybe a bash profile or something?) But for now I’m going to do it manually until it hurts. (me from the future: I’ve since found that this is not true. The openssh setting persist across sessions just fine!)
Obviously these instructions were focused on GitHub because that’s the git host I’m using, but I would bet that all of this will work well regardless. I believe this should support https clone too. However I would definitely recommend ssh.
It has now been 4 and a half years since I started writing blog posts on my phone and a few things have changed:
- GitHub no longer supports rsa sha-1 and has added Ed25519. Rather than sprt through rsa versions, I updated the commands to use Ed25519.
- I’ve added a command to copy the pub key to the phone’s clipboard.
- I fixed an issue where the ssh-add step didn’t note that the key itself must be added rather than running ssh-add by itself.
- I’ve inlined the key steps to make following along easier.
- Updated the termux touch keyboard link.
- Finally, I added a step to confirm that the ssh-add went smoothly.