Git
Git

Automate GitHub Repository Setup: How to Set PAT, Username, and Email for multiple GitHub accounts

Question 1: Are you tired of entering the same old commands every time while setting up your GitHub repo?

Question 2: Do you hate plugins to manage your multiple GitHub accounts?

Question 3: Are you a fan of the automation of mundane tasks?

If your answer to all of the above questions is YES then this blog is for you 🙂

What led me to automate GitHub repository setup?

Well, I also had the answer YES to the above three questions and I faced a lot of challenges while setting up my GitHub repos for different accounts. For every new session, I had to fill in the username and password(PAT) for running git commands.

So it made me wonder how I can get rid of these repetitive tasks. Firstly, I knew that I could configure my local repository for a specific account using the git config command so which could solve my problem of not entering the username and password every time. Now the second challenge arrives is I have to run the same git config commands every time whenever I set up a new repository in my machine. This made me realize that I can automate the tasks and why not use a shell script to do it 🙂

With the help of some GitHub documentation, I created one script which can automate this mundane task and help you to set up your GitHub repo faster without having to worry about downloading any plugins for your IDE to manage multiple GitHub accounts.

Steps to create the SHELL script file:

  1. Open a text editor of your choice
  2. Create a NEW file
  3. Paste the below code in that file:
#!/bin/bash

# Set the PAT, username, and email for the repository
read -p "Enter Your PAT(Personal Access Token): " PAT
export PAT=$PAT

read -p "Enter Your github username: " USERNAME
export USERNAME=$USERNAME

read -p "Enter Your github account email: " EMAIL
export EMAIL=$EMAIL

# Set the repository URL
read -p "Enter github URL: " REPO_URL
export REPO_URL=$REPO_URL

REPO_URL_WITHOUT_HTTPS=$(echo $REPO_URL | sed 's/https:\/\///')

CLONE_URL="https://$USERNAME:$PAT@$REPO_URL_WITHOUT_HTTPS" 

# Extract the directory name
DIR_NAME=$(basename $REPO_URL .git)

# clone the repository
git clone $CLONE_URL

# Change into the repository directory
cd $(pwd)/$DIR_NAME

# Set the Git configuration values
git config user.email $EMAIL
git config user.name $USERNAME

# Set the authentication token for the repository
git config credential.helper store
git config user.password $PAT

# Set the remote URL for the repository
git remote set-url origin $REPO_URL

4. Save the file as .sh extension e.g. github-setup.sh

5. Execute the github-setup.sh file in your terminal. Make sure you execute the script file where you want to save the repo.

After executing the script file you will get prompts to provide your PAT, username, email, and the Github Repo link in the same order.

Here is the screenshot for your reference:

Provide the appropriate values and press enter you will be all set with your repo in no time 🙂

In the last post we had discussed about The Best Python Books to read in 2023. You can read that post here.

Happy Coding and Happy Automating 🙂

Read this post here

Disclaimer: This code has been tested in MacOS and Linux

Subscribe to the BitsToGigs Newsletter

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

You may also like

More in:Git

Leave a reply

Your email address will not be published. Required fields are marked *