Tutorial: Using CodeCommit in AWS CloudShell - AWS CloudShell

Tutorial: Using CodeCommit in AWS CloudShell

CodeCommit is a secure, highly scalable, and managed source control service that hosts private Git repositories. Using AWS CloudShell, you can work with CodeCommit on the command line using the git-remote-codecommit utility. This utility is pre-installed in the AWS CloudShell compute environment and provides a simple method for pushing and pulling code from CodeCommit repositories. This utility does this by extending Git. For more information, see the AWS CodeCommit User Guide.

This tutorial describes how to create a CodeCommit repository and clone it to your AWS CloudShell compute environment. You also learn how to stage and commit a file to your cloned repository before pushing it to the remote repository that's managed in AWS Cloud.

Prerequisites

For information about the permissions that an IAM user requires to use AWS CloudShell, see the prerequisites section in the Getting started tutorial. You also need IAM permissions to work with CodeCommit.

In addition, before starting, make sure to have the following:

  • A basic understanding of Git commands and version control concepts

  • A file in the home directory of your shell that can be committed to the local and remote repositories. In this tutorial, it's referred to as my-git-file.

Step 1: Create and clone a CodeCommit repository

  1. In the CloudShell command line interface, enter the following codecommit command to create a CodeCommit repository called MyDemoRepo.

    aws codecommit create-repository --repository-name MyDemoRepo --repository-description "My demonstration repository"

    If the repository is successfully created, the command line displays the service's response.

    { "repositoryMetadata": { "accountId": "111122223333", "repositoryId": "0dcd29a8-941a-1111-1111-11111111111a", "repositoryName": "MyDemoRepo", "repositoryDescription": "My demonstration repository", "lastModifiedDate": "2020-11-23T20:38:23.068000+00:00", "creationDate": "2020-11-23T20:38:23.068000+00:00", "cloneUrlHttp": "https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/MyDemoRepo", "cloneUrlSsh": "ssh://git-codecommit.eu-west-1.amazonaws.com/v1/repos/MyDemoRepo", "Arn": "arn:aws:codecommit:eu-west-1:111111111111:MyDemoRepo" } )
  2. Using the command line, create a new directory for your local repository and make it your working directory.

    mkdir my-shell-repo cd my-shell-repo
  3. To clone the remote repository, use the git clone command. (As you're working with git-remote-codecommit, use the HTTPS (GRC) URL style).

    git clone codecommit::eu-west-1://MyDemoRepo

    If the repository is successfully cloned, the command line displays the service's response.

    Cloning into 'MyDemoRepo'... warning: You appear to have cloned an empty repository.
  4. To navigate to the cloned repository, use the cd command.

    cd MyDemoRepo

Step 2: Stage and commit a file before pushing it to your CodeCommit repository

  1. Add a file called my-git-file to the MyDemoRepo folder using either a Vim editor or the file upload feature of AWS CloudShell. To learn how to use both, see the Getting started tutorial.

  2. To stage your file in the repository, run the git add command.

    git add my-git-file
  3. To check that the file has been staged and is ready to be committed, run the git status command.

    git status

    my-git-file is listed as a new file and displays in green text, indicating it's ready to be committed.

  4. Commit this version of the staged file to the repository.

    git commit -m "first commit to repo"
    Note

    If you're asked for configuration information to complete the commit, use the following format.

    $ git config --global user.name "Jane Doe" $ git config --global user.email janedoe@example.com
  5. To sync your remote repository with the changes made in your local one, push the changes to the upstream branch.

    git push