Skip to content

Instantly share code, notes, and snippets.

@thoughtful-solutions
Last active January 23, 2022 19:51
Show Gist options
  • Save thoughtful-solutions/84a0f1b5089d4f274f6aef38e08536d8 to your computer and use it in GitHub Desktop.
Save thoughtful-solutions/84a0f1b5089d4f274f6aef38e08536d8 to your computer and use it in GitHub Desktop.
git on Google Drive with rclone

Why

Early on within a startup of an organisation resources are limited and the ability to control what is used and where things are found allow more consistent working practices and less cost.

This approach is to support small working groups using git, it is not a full git server and has no embedded review. However it does function, on three platforms, Windows, Macintosh (OSX) and Linux. The examples here are from a Linux environment.

Setting up rclone

rclone provides a platform neutral manner to gain access to remote cloud based storage

It is possible to configure access to GSuite Shared (Team) drives and Google Drives

$ rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> 

at this point select n for new remote

n/s/q> n

now name the remote storage

name> gsuitehome
Type of storage to configure.
Choose a number from below, or type in your own value 

now select google drive storage type by entring drive.

storage> drive

for the client_id and client_secret leave them blank as default. You can use API keys if you wish select the scope to be 1 for full access to the remote storage

Google Application Client Id - leave blank normally.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>
Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value
 1 / Full access all files, excluding Application Data Folder.
   \ "drive"
 2 / Read-only access to file metadata and file contents.
   \ "drive.readonly"
   / Access to files created by rclone only.
 3 | These are visible in the drive website.
   | File authorization is revoked when the user deauthorizes the app.
   \ "drive.file"
   / Allows read and write access to the Application Data folder.
 4 | This is not visible in the drive website.
   \ "drive.appfolder"
   / Allows read-only access to file metadata but
 5 | does not allow any access to read or download file content.
   \ "drive.metadata.readonly"
scope> 1

Leave the remote folder id blank. Decline the offer for advance config and select Yes for autoconfig

ID of the root folder - leave blank normally.  Fill in to access "Computers" folders. (see docs).
root_folder_id>
Service Account Credentials JSON file path  - leave blank normally.
Needed only if you want use SA instead of interactive login.
service_account_file>
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n
Remote config
Use advance config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine or Y didn't work
y) Yes
n) No
y/n> y

This will cause a browser to open and give you a chance to confirm your Google Password.

Configure this as a team drive?
y) Yes
n) No

here you can choose. if you are using a GSuite shared drive (y) or your own home drive (n) Having selected appropriately you'll be presented with something like

--------------------
[gdrive]
type = drive
client_id =
client_secret =
scope = drive
root_folder_id =
service_account_file =
token = {"access_token":"123213",
"token_type":"Bearer","refresh_token":"asdas",
"expiry":"YYYY-MM-23T16:22:27.817432122+01:00"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote

y/e/d> y

and at this point say Y as it is ok

Current remotes:

Name                 Type
====                 ====
gsuitehome           drive

e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q 

Enter q and the rclone is ready to work.

the rclone config lives in ~/.config/rclone/rclone.conf

You can confirm everything is working by doing

rclone lsd gsuitehome:

Don't forget the ':' to remind it that the resource is remote like ssh.

Setting up git

First setup an rclone mount for gsuite

$ mkdir ~/gsuitehome
$ rclone mount gsuitehome: ~/gsuitehome &

This mounts your gsuite home directory to your local storage

Now create a bare git repo

$ mkdir ~/tmp
$ cd ~/tmp
$ git init --bare

This should create a bare git repo

$ ls -l
drwxrwxr-x. 2 user group    6 Mar  5 15:48 branches
-rw-rw-r--. 1 user group   66 Mar  5 15:48 config
-rw-rw-r--. 1 user group   73 Mar  5 15:48 description
-rw-rw-r--. 1 user group   23 Mar  5 15:48 HEAD
drwxrwxr-x. 2 user group 4096 Mar  5 15:48 hooks
drwxrwxr-x. 2 user group   21 Mar  5 15:48 info
drwxrwxr-x. 4 user group   30 Mar  5 15:48 objects
drwxrwxr-x. 4 user group   31 Mar  5 15:48 refs

Now create a directory heirachy for and place the repo on your gsuitehome

$ mkdir -p ~/gsuitehome/git-repos/myrepo
$ cp -r ~/tmp/* ~/gsuitehome/git-repos/myrepo

Now tidy up after yourself

$ cd ~
$ rm -fr ~/tmp

Working with git

Creating a repo

Identify yourself to the git repo and for the work you are about to do

$ git config --global user.name "Forename Initial. Lastname"
$ git config --global user.email "[email protected]"

Now take a copy of your repo

$ git clone ~/gsuitehome/git-repos/myrepo
Cloning into 'myrepo'...
warning: You appear to have cloned an empty repository.
done.

This will create a sub-directory called 'myrepo' in your current working directory This is an empty repository

$ cd myrepo
$ git status
On branch master

No commits yet
nothing to commit (create/copy files and use "git add" to track)

$ git remote -v
origin	/home/user/gsuitehome/git-repos/myrepo (fetch)
origin	/home/user/gsuitehome/git-repos/myrepo (push)

If you attempt to push back to origin you will fail.

$ git push origin
error: src refspec refs/heads/master does not match any.
error: failed to push some refs to '/home/user/gsuitehome/git-repos/myrepo'

If you attempt to branch you will fail

$ git branch mybranch
fatal: Not a valid object name: 'master'.

So now populate master with something

$ touch README.MD
$ git add -A
$ git commit -m "Initial Config"

$ git push origin
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 215 bytes | 215.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /home/user/gsuitehome/git-repos/myrepo
 * [new branch]      master -> master

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

Creating a branch

Now we create a branch

$ git branch mybranch
$ git checkout mybranch
Switched to branch 'mybranch'
$ git status
On branch mybranch
nothing to commit, working tree clean

Now we add files and commit

$ touch another.md
$ git add -A
$ git commit -m "another file added"
1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 another.md

And we look at the branches (local and remote)

git branch -a
  master
* mybranch
  remotes/origin/master

And we push the local branch and track it

$ git push -u origin  mybranch
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 250 bytes | 250.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To /home/user/gsuitehome/git-repos/myrepo
 * [new branch]      mybranch -> mybranch
Branch 'mybranch' set up to track remote branch 'mybranch' from 'origin'.

And we can confirm that the branch is present both locally and remote

$ git branch -a
  master
* mybranch
  remotes/origin/master
  remotes/origin/mybranch

So we can now check that we have different things in the branches

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ ls
README.MD

$ git checkout mybranch
Switched to branch 'mybranch'
Your branch is up to date with 'origin/mybranch'.
$ ls
another.md  README.MD

Merging with git

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

$ git merge mybranch 
Updating 8cc659a..3f665fb
Fast-forward
another.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 another.md

$ git push -u origin master
Total 0 (delta 0), reused 0 (delta 0)
To /home/edmunds/gsuite/git-repos/myrepo
   8cc659a..3f665fb  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment