Skip to content

Instantly share code, notes, and snippets.

@Vac1911
Last active May 16, 2022 14:52
Show Gist options
  • Save Vac1911/36571699986bbc440857a2602e14331c to your computer and use it in GitHub Desktop.
Save Vac1911/36571699986bbc440857a2602e14331c to your computer and use it in GitHub Desktop.
Sets up git authentication (for github) automatically
# Install GitHub CLI
GH=$HOME/.local/bin/gh
command -v gh
if [ "$(command -v gh)" ]; then
# GitHub CLI is globally installed
GH="$(which gh)"
elif [ -f "$GH" ]; then
# GitHub CLI is locally installed
echo "already installed"
else
# GitHub CLI needs to be installed
VERSION=`curl "https://api.github.com/repos/cli/cli/releases/latest" | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/' | cut -c2-`
curl -sSL https://github.com/cli/cli/releases/download/v${VERSION}/gh_${VERSION}_linux_amd64.tar.gz -o gh_${VERSION}_linux_amd64.tar.gz
tar xvf gh_${VERSION}_linux_amd64.tar.gz
mkdir $HOME/.local/bin
mv gh_${VERSION}_linux_amd64/bin/gh $HOME/.local/bin/
rm -rf gh_${VERSION}_linux_amd64
rm gh_${VERSION}_linux_amd64.tar.gz
echo "alias gh='${GH}'" >> $HOME/.bashrc
source ~/.bashrc
fi
# Authenticate through GitHub CLI and get username
`$GH auth login`
GITUSERNAME=`$GH api user | grep -oE '"login":"[^,]+"' | sed -E 's/.*"([^"]+)".*/\1/'`
# Setup git credentials
`$GH auth setup-git`
git config --global user.name $GITUSERNAME
git config --global user.email ${GITUSERNAME}@github.com
@Vac1911
Copy link
Author

Vac1911 commented Feb 8, 2022

Usage:

wget https://gist.githubusercontent.com/Vac1911/36571699986bbc440857a2602e14331c/raw/a1f6afa8ae203e902e9978f6da7585c5779c5770/auth.sh
chmod +x auth.sh
./auth.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment