Git: Setting Up
Step-By-Step Initialization Of Account
1. Generate Keys
2. Set Git User Profile
3. Register SSH Public Key With Github
4. Test Git Connection
Generate Keys
ssh-keygen -t rsa -b $key_rsa_length -C $email_address
Note: terms with $
are variables which you can set at a separate file during the generation.
Caveat: While many tutorials indicate you can default the path at ~/.ssh/id_rsa
, it is better to give a specific name ID to ensure you do not corrupt your other keys. SSH keys are used for many other applications other than git, hence, you want your keys to be specific.
Check fingerprint of key:
$ ssh-keygen -lf <path_to_key_file>/<file_key> -E sha256
e.g. $ ssh-keygen -lf ~/.ssh/id_ursa_rsa -E sha256
_
function generate_key_for_git {
# Prompt for the key RSA length and email address
echo "Enter the RSA key length (e.g., 4096):"
read -r key_rsa_length
echo "Enter the email address to associate with the key:"
read -r email_address
# Ensure the .ssh directory exists
mkdir -p "$HOME/.ssh"
while true; do
# Prompt for the key name
echo "Enter a name for the key to be stored in ~/.ssh/ (e.g., id_ursa_rsa):"
read -r key_name
# Check if the key file already exists
if [ -f "$HOME/.ssh/$key_name" ] || [ -f "$HOME/.ssh/$key_name.pub" ]; then
echo "Key file already exists. Please choose another name."
else
break
fi
done
# Generate the SSH key
ssh-keygen -t rsa -b "$key_rsa_length" -C "$email_address" -f "$HOME/.ssh/$key_name"
# Store the private key path
private_key_path="$HOME/.ssh/$key_name"
# Derive the public key from the private key
ssh-keygen -f "$private_key_path" -y > "$private_key_path.pub"
# Print the demarcator message
echo "======================"
echo "Add this public key to your Github account"
echo "======================"
# Output the public key
cat "$private_key_path.pub"
}
"""
% generate_key_for_git
Enter the RSA key length (e.g., 4096):
1024
Enter the email address to associate with the key:
x@x.com
Enter a name for the key to be stored in ~/.ssh/ (e.g., id_ursa_rsa):
id_x
Key file already exists. Please choose another name.
Enter a name for the key to be stored in ~/.ssh/ (e.g., id_ursa_rsa):
id_x01
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/chanfamily/.ssh/id_x01
Your public key has been saved in /Users/chanfamily/.ssh/id_x01.pub
The key fingerprint is:
SHA256:pZI435jwI...88qaXevtq5d4 x@x.com
The key's randomart image is:
+---[RSA 1024]----+
|.o o=+oo+. |
|.+=oo+oo |
|o +o .o . |
|.o. . . o |
|o+. + o S |
|*o+ .= = |
|o=.+. = . |
|. =..o |
|.=o=o E |
+----[SHA256]-----+
======================
Add this public key to your Github account
======================
ssh-rsa AA.......uQ1GKNQMxMlLcYxbsfgKDTTBX99jerH4aqlyccKDw== x@x.com
"""
Set Git User Profile
git config --global user.name $id_git
git config --global user.email $email_address
After setting the profile, the test connection attempt, should fail.
eval $(ssh-agent -s) #start ssh-agent in the backgroundssh-add $key_store$key_id # add private key, i.e. $private_authentication_key_with_path
echo "set_git_user_profile [DONE]"git config --list # list Git settings
ssh -T git@github.com # try connecting to git account
# or `ssh -T git@github.com -i <path_to_key>`
For now, the connection will not work yet. This is because the SSH public key has to be registered with the github account.
function login_with_git_key {
# List the available keys in the ~/.ssh directory
echo "Available keys in ~/.ssh/:"
ls -1 ~/.ssh/
# Prompt for the private key name to use for authentication
echo "Enter the private key name to use for Git (e.g., id_ursa_rsa):"
read -r key_name
key_path="$HOME/.ssh/$key_name"
# Check if the key file exists
if [ ! -f "$key_path" ]; then
echo "Key file does not exist. Please make sure the key name is correct."
return 1
fi
# Start the ssh-agent in the background
eval $(ssh-agent -s)
# Add the specified private key
ssh-add "$key_path"
# Confirm the key was added
if [ $? -eq 0 ]; then
echo "Key added successfully."
else
echo "Failed to add the key."
return 1
fi
# Display the current Git configuration
echo "Git user profile set."
git config --list
}
"""
% login_with_git_key
Available keys in ~/.ssh/:
id_rsa
:
Enter the private key name to use for Git (e.g., id_ursa_rsa):
id_rsa
Agent pid 41607
Identity added: /Users/...
Key added successfully.
Git user profile set.
:
"""
Caching Credentials
git config --global credential.helper cache
After executing this command, when you perform a Git operation that requires authentication (e.g., git push
), you'll be prompted to enter credentials. Git will cache these credentials in memory for 15 minutes, so subsequent operations won't prompt you again within that time frame.
Unsetting the Credential Helper
# command to remove the configuration that uses the credential cache helper. As a result, Git will revert to its default behavior of prompting for credentials every time they're needed, unless another credential helper is configured.
git config --global --unset credential.helper
Register SSH Public Key With Github
# derive the public key from the private key
ssh-keygen -f ~/.ssh/<key.private.file> -y > ~/.ssh/<key.public.file>.pubcd /Users/<user_id>/.ssh/
cat $key_store$key_id'.pub'
echo $demarcator echo ‘Add this public key to Github account’
Log on to Github and register the public key.
Caveat: Keep the private key safe, and back up the key pairs safely.
Get the generated public key from the file, and copy-paste the contents on the following:
# check the hash of the public key. This should match what is stated in the added key in github
$ ssh-keygen -lf ~/.ssh/<key_file_name>.pub # $key_store$key_id'.pub'
Test Git Connection
# test git log-in
eval $(ssh-agent -s)
ssh-add $key_store$key_id # ssh -i $key_store$key_id git@github.com
ssh -T git@github.com # try connecting to git account# check hash of key
ssh-add -l -E sha256
You should be able to connect now.
Let’s set a user menu to assist for semi-automation:
Github scripts for the tutorial: Chapter_01_Access_Setup
There is no abstract art. You must start with something. — Pablo Picasso
Git
Chapter 01: Setting up account
Chapter 02: Setting up repo, Get and Update
Chapter 03: Collaboration And Update
Chapter 04: Workdesk
Chapter 05: Starting Tooling Git