Blog

Working with Git in CodeCommit

Published on: January 12, 2022 by Vinayan C V

Working with Git in CodeCommit

Scenario:

In this article we are creating a local copy of the CodeCommit repository which we are creating in AWS, and making some changes to it. Then we push the changes to the AWS CodeCommit repository.

Step-by-step guide

Setup for HTTPS users using Git credentials.

1. Create an AWS CodeCommit Repository.
On the Create repository page, in Repository name, enter a name for your repository. Other fields are optional.

2. Creating Local repository in our system from AWS CodeCommit repo.
Choose Clone URL, and then choose the protocol you want to use when cloning or connecting to the repository.

There are three protocols:
-Copy the HTTPS URL if you are using either Git credentials with your IAM user or the credential helper included with the AWS CLI.
-Copy the HTTPS (GRC) URL if you are using the git-remote-codecommit command on your local computer.
-Copy the SSH URL if you are using an SSH public/private key pair with your IAM user.Here we are using HTTPS URL,
- Create an IAM user with CodeCommit access and generate HTTPS Git credentials, since git https credentials will be asked when running git clone.

2.1. Sign in to the AWS Management Console and open the IAM console to create CodeCommit user.
2.2. In the IAM console, in the navigation pane, choose Users, and then choose Add user or select existing user.

2.3. In Grant permissions, choose Attach existing policies directly.


2.4. After you have selected the policy you want to attach, choose Next: Review to review the list of policies to attach to the IAM user.


- Generate HTTPS Git credentials for CodeCommit user.

git clone https://git-codecommit.us-east-2.amazonaws.com/v1/repos/test

Now you have the local repo with exact copy of CodeCommit repo.
In Grant permissions, choose Attach existing policies directly.

  1. Add files to see the changes happen in CodeCommit after next push.
  2. Run git config to add your username and email address. This makes it easier to identify the commits you make and others.
    git config --local user.name "your-user-name" 
    git config --local user.email your-email-address
  3. Run git status to view the changes made,
    git status

  4. Run git add to stage the change,
    git add .
  5. The git add command adds changes in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.
  6. Run git commit to commit the change,
    git commit -m "Message to commit"

  7. Run git push to push your initial commit.
    git push -u origin master

    In this step, you push the commit from your local repo to your CodeCommit repository. you can view the changes in AWS. CodeCommit user authentication is needed here.

  8. Now you can view the changes/added files in the AWS CodeCommit repo.

Setup steps for SSH connections to AWS CodeCommit repositories

  1. To set up the public and private keys for Git and CodeCommit from the terminal on your local machine, run the ssh-keygen command, and follow the directions to save the file to the .ssh directory for your profile.
  2. Run the following command to display the value of the public key file (Codecommit_rsa.pub) :
    cat ~/.ssh/codecommit_rsa.pub

    Copy this value to the clipboard.

  3. In AWS IAM console, in the navigation pane, choose Users, and from the list of users, choose your IAM user you created for this CodeCommit. On the user details page, choose the Security Credentials tab, and then choose Upload SSH public key. Paste the contents of your SSH public key into the field, and then choose Upload SSH public key.
  4. Copy or save the information in SSH Key ID (for example, APKA4RVIFCYLNQJMQFFA).
  5. On your local machine, use a text editor to create a config file in the ~/.ssh directory, and then add the following lines to the file, where the value for User is the SSH key ID you copied earlier:
    Host git-codecommit.*.amazonaws.com 
    User APKA4RVIFCYLNQJMQFFA 
    IdentityFile ~/.ssh/codecommit_rsa
  6. Change the permissions for the config file:
    chmod 600 config

  7. Setup is completed and you can test your SSH configuration using the following command :
    ssh git-codecommit.us-east-2.amazonaws.com

LET US TAKE CARE OF YOUR SERVERS 24/7/365.

Get rid of all the hassles

CONTACT OUR DEVOPS TEAM WITH YOUR REQUIREMENTS!

Category : Linux

Vinayan C V

Vinayan C V

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend