More recent git on Google Cloud Shell

29Sep23

Ever since I started signing GitHub commits with SSH keys I’ve made sure to use git v2.35 or later. Unfortunately Google Cloud Shell comes with a rather crusty old version of git (as part of the fact that it’s still based on Debian 11 ‘Bullseye’).

$ git --version
git version 2.30.2

Just copying over a more recent git binary doesn’t work. There’s also little point trying to update it using regular methods, as changes outside of the home directory get overwritten. We need to install into the home directory, so it’s necessary to build and install it locally from source[1].

First create a .local/bin directory and add it to the PATH[2]:

mkdir -p .local/bin
PATH="$HOME/.local/bin:$PATH"

Then make a place to download the source, cd into it, clone the source, and cd into it:

mkdir -p git/github.com/git
cd git/github.com/git/
git clone https://github.com/git/git.git
cd git

For HTTPS URLs to work we need libcurl4-openssl-dev for the build:

sudo apt install libcurl4-openssl-dev

If a build was done now, it would be of whatever is in the master branch, which might be unstable, so better to checkout the latest stable release, which is v2.42.0 at the time of writing:

git checkout tags/v2.42.0 -b v2.42.0

Then configure, build and install:

make configure
./configure --prefix=${HOME}/.local
make install

The new git should now be on the PATH:

$ which git
/home/chris/.local/bin/git

And it should be much newer than 2.30.2:

$ git --version
git version 2.42.0

Notes

[1] Well… you could cheat and grab a precompiled tarball from the v2.42.0 release on my fork.
[2] There should be a section like this in ~/.profile, but that might be missing from really old instances of Cloud Shell:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi


No Responses Yet to “More recent git on Google Cloud Shell”

  1. Leave a Comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.