Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active February 27, 2024 01:53
Show Gist options
  • Save wmakeev/4df153853c203d80a41f58f862635e60 to your computer and use it in GitHub Desktop.
Save wmakeev/4df153853c203d80a41f58f862635e60 to your computer and use it in GitHub Desktop.
Git via SSH #git #ssh #bitbucket #codecommit

Git via SSH

Step 1.

Generate the SSH keys

cd ~/.ssh
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/user/.ssh/id_rsa): bitbucket_rsa
Enter passphrase (empty for no passphrase): [type passphrase]
Enter same passphrase again: [retype passphrase]
Your identification has been saved in bitbucket_rsa.
Your public key has been saved in bitbucket_rsa.pub.
The key fingerprint is:
SHA256:9GgssyHwEA0z3Stwmqc56RGsBujvSIDkcdO8OWYXlsa [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|..=+1.  .        |
|oo+*+o.+         |
|*.=*o =.o        |
|++=+oG.+ o       |
|o..==.* S .      |
| .=. . *         |
|..o.  .          |
| . .             |
|                 |
+----[SHA256]-----+

List the contents of ~/.ssh to view the key files.

ls

output

id_rsa id_rsa.pub

Step 2. Add the key to the ssh-agent

  1. To start the agent, run the following:

    eval `ssh-agent`

    output

    Agent pid 9700
    
  2. Add keys

    MacOS

    ssh-add -K ~/.ssh/bitbucket_rsa

    output

    Enter passphrase for /Users/user/.ssh/bitbucket_rsa:
    Identity added: /Users/user/.ssh/bitbucket_rsa ([email protected])
    

    Linux

    ssh-add ~/.ssh/<private_key_file>
  3. So that your computer remembers your password each time it restarts, open (or create) the ~/.ssh/config file and add these lines to the file:

    ...
    
    Host *
      AddKeysToAgent yes
      UseKeychain yes #macOS only
    

Step 3. Update ssh config

Add to ~/.ssh/config

Host git-codecommit.*.amazonaws.com
  User [SSH key ID]
  IdentityFile ~/.ssh/codecommit_rsa

Host bitbucket.org
  User [bitbucket-user]
  IdentityFile ~/.ssh/bitbucket_rsa

Host github.com
  User wmakeev
  IdentityFile ~/.ssh/github_rsa

Access CodeCommit repositories in multiple AWS accounts with SSH credentials

Host codecommit-1
  Hostname git-codecommit.us-east-1.amazonaws.com
  User SSH-KEY-ID-1 # SSH Key ID you copied from IAM (for example, APKAEIBAERJR2EXAMPLE1)
  IdentityFile ~/.ssh/codecommit_rsa

Host codecommit-2
  Hostname git-codecommit.us-east-1.amazonaws.com
  User SSH-KEY-ID-2
  IdentityFile ~/.ssh/codecommit_2_rsa

Step 4. Add the public key to your Git server

  1. Copy the contents of your public key file

    cat ~/.ssh/bitbucket_rsa.pub | pbcopy
  2. Add key

  3. Verify your configuration and username

    CodeCommit

    ssh git-codecommit.us-east-2.amazonaws.com

    or with multiple credentials

    ssh codecommit-2

    Bitbucket

    SSH keys

    Github

    Sample output (type "yes" when prompt)

    The authenticity of host 'bitbucket.org (18.234.32.156)' can't be established.
    RSA key fingerprint is SHA256:zzCQOXSRBEiUtuE6AikHYKwbHaxvSc0ojez9YXaRp1A.
    
    Are you sure you want to continue connecting (yes/no)? yes
    
    Warning: Permanently added 'bitbucket.org,18.234.32.156' (RSA) to the list of known hosts.
    Enter passphrase for key '/home/pi/.ssh/bitbucket_rsa':
    logged in as [username].
    
    You can use git or hg to connect to Bitbucket. Shell access is disabled.
    

Step 5. Cloning a Git repository

Url format:

[email protected]:<account_name>/<repo_name>.git

or

ssh://[email protected]/<account_name>/<repo_name>.git

Clone

GitHub

git clone [email protected]:teamsinspace/documentation-tests.git

CodeCommit

git clone ssh://git-codecommit.eu-west-1.amazonaws.com/v1/repos/MyDemoRepo my-demo-repo

With multiple credentials, you will be able to replace git-codecommit.eu-west-1.amazonaws.com with codecommit-2.

git clone ssh://codecommit-2/v1/repos/YourRepositoryName

To set up a remote for your repository

git remote add origin ssh://codecommit-2/v1/repos/YourRepositoryName

Bitbucket

clone Bitbucket repository

Update the URL for Git repositories

  1. From a terminal, navigate to the repository.

    cd ~/<path_to_repo>
  2. Run git remote -v to see the current remote URL.

    git remote -v

    output

    origin https://[email protected]/tutorials/tutorials.git.bitbucket.org.git (fetch)
    origin https://[email protected]/tutorials/tutorials.git.bitbucket.org.git (push)
    
  3. Update the remote URL with git remote set-url using the current and new remote URLs.

    git remote set-url origin [email protected]:tutorials/tutorials.git.bitbucket.org.git

    If you update your URL from HTTPS to SSH, next time you push or pull from your repository, the terminal responds that it is adding the Bitbucket host to the list of known hosts. You also won't have to enter a password.

Get your local Git repository on Bitbucket

  1. Switch to your repository's directory

    cd /path/to/your/repo
  2. Connect your existing repository to Bitbucket

    git remote add origin [email protected]:[username]/[repository].git
    git push -u origin master

Setup latest git version on Linux Mint

sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git
git --version
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment