How to clone and synchronise a GitHub repository on Android

Introduction

This project describes how I synchronise my LogSeq folder on GitHub on Android devices. I wanted to be able to work on topics in LoqSeq on my Android devices besides my PCs. Most of the screen shots and references relate to LogSeq, but you can adjust them to your needs.

The solution described here is one in which the synchonization with the repository will be manual. In this scenario, one would synchronise with the repo before opening the tool (LogSeq in my case) and update the repo again when you're done.

The Steps (skip if you want to jump directly to the actions)

This tutorial describes how to install Termux on an Android device. Termux is an open-source Android terminal emulator and Linux environment application. After Termux is updated and running, a folder on the Android device is mapped to Termux. GitHub is installed, and the repo is cloned to this folder.

The Termux folder used for the repo has to be reflected from the Android end so that any app can read and update this folder.

Using the companion Termux:Widget app and bash scripts, we synchronise the Android folder that is mapped to Termux with the repository on Github.

This document is organised so as to explain each step and highlight the tools and actions associated with them. It has "checkpoints" aimed at helping the reader gauge their progress.

Install F-Droid

Termux can be found on the Google Play Store, but the version here is not the latest. Postings on Termux forums suggest installing the version from the F-Droid alternative store. F-Droid is an app catalogue of FOSS (Free and Open Source Software) applications for the Android platform.

You need to sideload it.

Exiting Termux

To close a termux session, you should type exit.

Update Termux

apt update
apt upgrade

When asked whether to overwrite files during the upgrade process, press 'Y'.

Install and configure git

pkg install git 
git config --global user.name "<name>" 
git config --global user.email "<email>" 

Allow Termux to access folders on your Android device

termux-setup-storage

The folders on your device are shown under ~/storage/shared/. Use the ls command to decide where you want to locate the repository you will be cloning. You can use the mkdir command to create a folder, although the cloning process will automatically create a folder for your repo.

For example, if I wanted a subfolder called termux_mapped under the Documents folder, I'd use the command mkdir ~/storage/shared/Documents/termux_mapped.

If your device has external storage and it is rooted, you can R/W access it via ~/storage/external-1/.

If your Android version is 11 and you are not longer able to access your external storage, refer to Termux Wiki: Android 11.

Install and configure an SSH key to authenticate with GitHub

pgk install openssh


 

Create the SSH keys that we’ll be using as our authentication method to access our Git repository. You can keep the default file name. Enter a passphrase. Unless you enter a new location, the file path is ~/.ssh/id_ed25519.pub.

ssh-keygen -t ed25519 -C "<email>"

Load the public key to GitHub

Copy the public key to the folder accessible from your Android's file manager tool. This will allow you to upload it to GitHub.

cp ~/.ssh/id_ed25519.pub ~/storage/shared/Documents/ # will place the file in the Documents folder.

You will need to open the public key in a text editor in order to copy its contents to GitHub. If you don't have a text editor on your Android device, you can:

  • Transfer the file to a desktop computer, open it in Notepad, and process it from there.
  • Install a free text editor (search F-Droid for a FOSS one).

Go to the GitHub Settings page, click on the New SSH key button, or click the link add a new public key that comes up when you click the <> Code button of the repo you want to clone and choose the SSH tab.

Label the SSH key. You should enter the device identifier, as this allows you to control each SSH key. If you decommission a device, its key can be easily deleted. Open the file id_ed25519.pub in a text editor and paste the contents into Key.

Press the Add SSH key button.

In the end, you should see the SSH key you set.

Clone the GitHub repo

Change to the shared folder and type in the git command to clone the repo. The repo address is the one shown in the SSH tab. You can copy and paste the address.

You will be prompted to accept the fingerprint, and you will need to provide the password associated with your SSH key. You can leave the password blank if you wish, but this will make the process less secure.

cd ~/storage/shared/Documents
git clone git@github.com:**account/repo.git** # change to your account and repo (my repo was logseq.git).
git config global -add safe.directory ~/storage/shared/Documents/logseq

The folder created on your Android device should be populated with the files downloaded from the GitHub repository.

Syncing the GitHub repo

Install Termux Widget from F-Droid. This app allows one to execute scripts stored in Termux from the home screen.

Follow the instructions at Termux:Widget - Scripts Directories (Mandatory) to setup the shortcut directories.

Download the two scripts that will synchronise your folder with GitHub

From Logseq-Git-Sync-101 download the scripts pull-graph.sh and push-graph.sh.

These need to end up in the folder ~/.shortcuts in termux

Assuming you placed the scripts in the folder Documents on your Android device, you would type the command below to move them to shortcuts. This is the opposite of what was done to export the public key.

 mv ~/storage/shared/Documents/*.sh . ~/.shortcuts

On my Android device, I also edited the line source bin/source-ssh-agent because the utility was in the path (and there wasn't a ./bin folder).

pull-graph.sh
#!/usr/bin/bash
# source bin/source-ssh-agent
# source-ssh-agent resides in a folder pointed to in $PATH
source source-ssh-agent
cd ~/storage/shared/Documents/logseq
git pull
push-graph.sh
#!/usr/bin/bash
# source bin/source-ssh-agent
# source-ssh-agent resides in a folder pointed to in $PATH
source source-ssh-agent
cd ~/storage/shared/Documents/logseq
git add -A
git commit -m "Sync from SM-xxx @ $(date + '%F %T')" # Source computer and date
git push

Add the Termux Widget shortcut to the screen

You can close Termux.

When you are finished, tap on the push-graph script to synchronise changes with your GitHub repo. The first time, you may be prompted to grant additional Android or GitHub permissions and to set the directory as a safe one. Just follow the instructions.

From here to the future: Daily action

The setup is complete. What needs to happen now is the following:

  • Before you open your tool (LogSeq in this example), run the pull-graph script to get any updates from other sources.
  • Use your tool.
  • After you're finished, run the push-graph script to get any updates from other sources.
Follow This, That and (Maybe), the Other:

Comments

  1. Thanks for this tutorial, it's really helpful.

    I found a typo: $(date + '%F %T') must be $(date '+%F %T')

    I also had issues that shell scripts can't be executed in Termux when they were written in Windows and copied to Termux (probably due to encoding issues). Either write your scripts on a Linux system or directly in Termux.

    ReplyDelete

Post a Comment

Popular posts from this blog

20150628 Giarratana Circular

HOWTO setup OpenVPN server and client configuration files using EasyRSA