

# Migrate to AWS CodeCommit
<a name="how-to-migrate-repository"></a>

You can migrate a Git repository to a CodeCommit repository in a number of ways: by cloning it, mirroring it, migrating all or just some of the branches, and so on. You can also migrate local, unversioned content on your computer to CodeCommit.

The following topics show you some of the ways you can migrate a repository. Your steps might vary, depending on the type, style, or complexity of your repository and the decisions you make about what and how you want to migrate. For very large repositories, you might want to consider [migrating incrementally](how-to-push-large-repositories.md).

**Note**  
You can migrate to CodeCommit from other version control systems, such as Perforce, Subversion, or TFS, but you must first migrate to Git.   
For more options, see your Git documentation.  
Alternatively, you can review the information about [migrating to Git](http://git-scm.com/book/en/v2/Git-and-Other-Systems-Migrating-to-Git) in the *Pro Git* book by Scott Chacon and Ben Straub.

**Topics**
+ [Migrate a Git repository to AWS CodeCommit](how-to-migrate-repository-existing.md)
+ [Migrate content to CodeCommit](how-to-migrate-repository-local.md)
+ [Migrate a repository in increments](how-to-push-large-repositories.md)

# Migrate a Git repository to AWS CodeCommit
<a name="how-to-migrate-repository-existing"></a>

You can migrate an existing Git repository to a CodeCommit repository. The procedures in this topic show you how to migrate a project hosted on another Git repository to CodeCommit. As part of this process, you:
+ Complete the initial setup required for CodeCommit.
+ Create a CodeCommit repository.
+ Clone the repository and push it to CodeCommit.
+ View files in the CodeCommit repository.
+ Share the CodeCommit repository with your team.

![\[Migrating a Git repository to CodeCommit\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-migrate-existing.png)


**Topics**
+ [Step 0: Setup required for access to CodeCommit](#how-to-migrate-existing-setup)
+ [Step 1: Create a CodeCommit repository](#how-to-migrate-existing-create)
+ [Step 2: Clone the repository and push to the CodeCommit repository](#how-to-migrate-existing-clone)
+ [Step 3: View files in CodeCommit](#how-to-migrate-existing-view)
+ [Step 4: Share the CodeCommit repository](#how-to-migrate-existing-share)

## Step 0: Setup required for access to CodeCommit
<a name="how-to-migrate-existing-setup"></a>

Before you can migrate a repository to CodeCommit, you must create and configure an IAM user for CodeCommit and configure your local computer for access. You should also install the AWS CLI to manage CodeCommit. Although you can perform most CodeCommit tasks without it, the AWS CLI offers flexibility when working with Git at the command line or terminal. 

If you are already set up for CodeCommit, you can skip ahead to [Step 1: Create a CodeCommit repository](#how-to-migrate-existing-create).

**To create and configure an IAM user for accessing CodeCommit**

1. Create an Amazon Web Services account by going to [http://aws.amazon.com](http://aws.amazon.com) and choosing **Sign Up**.

1. Create an IAM user, or use an existing one, in your Amazon Web Services account. Make sure you have an access key ID and a secret access key associated with that IAM user. For more information, see [Creating an IAM User in Your Amazon Web Services account](https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html).
**Note**  
CodeCommit requires AWS Key Management Service. If you are using an existing IAM user, make sure there are no policies attached to the user that expressly deny the AWS KMS actions required by CodeCommit. For more information, see [AWS KMS and encryption](encryption.md).

1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the IAM console, in the navigation pane, choose **Users**, and then choose the IAM user you want to configure for CodeCommit access.

1. On the **Permissions** tab, choose **Add Permissions**. 

1. In **Grant permissions**, choose **Attach existing policies directly**.

1. From the list of policies, select **AWSCodeCommitPowerUser** or another managed policy for CodeCommit access. For more information, see [AWS managed policies for CodeCommit](security-iam-awsmanpol.md).

   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. If the list is correct, choose **Add permissions**.

    For more information about CodeCommit managed policies and sharing access to repositories with other groups and users, see [Share a repository](how-to-share-repository.md) and [Authentication and access control for AWS CodeCommit](auth-and-access-control.md).

**To install and configure the AWS CLI**

1. On your local machine, download and install the AWS CLI. This is a prerequisite for interacting with CodeCommit from the command line. We recommend that you install AWS CLI version 2. It is the most recent major version of the AWS CLI and supports all of the latest features. It is the only version of the AWS CLI that supports using a root account, federated access, or temporary credentials with **git-remote-codecommit**.

   For more information, see [Getting Set Up with the AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html).
**Note**  
CodeCommit works only with AWS CLI versions 1.7.38 and later. As a best practice, install or upgrade the AWS CLI to the latest version available. To determine which version of the AWS CLI you have installed, run the **aws --version** command.  
To upgrade an older version of the AWS CLI to the latest version, see [Installing the AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/installing.html).

1. Run this command to verify that the CodeCommit commands for the AWS CLI are installed.

   ```
   aws codecommit help
   ```

   This command returns a list of CodeCommit commands.

1. Configure the AWS CLI with a profile by using the **configure** command, as follows:.

   ```
   aws configure
   ```

   When prompted, specify the AWS access key and AWS secret access key of the IAM user to use with CodeCommit. Also, be sure to specify the AWS Region where the repository exists, such as `us-east-2`. When prompted for the default output format, specify `json`. For example, if you are configuring a profile for an IAM user:

   ```
   AWS Access Key ID [None]: Type your IAM user AWS access key ID here, and then press Enter
   AWS Secret Access Key [None]: Type your IAM user AWS secret access key here, and then press Enter
   Default region name [None]: Type a supported region for CodeCommit here, and then press Enter
   Default output format [None]: Type json here, and then press Enter
   ```

   For more information about creating and configuring profiles to use with the AWS CLI, see the following:
   + [Named Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
   + [Using an IAM Role in the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html)
   + [Set command](https://docs.aws.amazon.com/cli/latest/reference/set.html)
   + [Connecting to AWS CodeCommit repositories with rotating credentials](temporary-access.md)

   To connect to a repository or a resource in another AWS Region, you must reconfigure the AWS CLI with the default Region name. Supported default Region names for CodeCommit include:
   + us-east-2
   + us-east-1
   + eu-west-1
   + us-west-2
   + ap-northeast-1
   + ap-southeast-1
   + ap-southeast-2
   + ap-southeast-3
   + me-central-1
   + eu-central-1
   + ap-northeast-2
   + sa-east-1
   + us-west-1
   + eu-west-2
   + ap-south-1
   + ap-south-1
   + ca-central-1
   + us-gov-west-1
   + us-gov-east-1
   + eu-north-1
   + ap-east-1
   + me-south-1
   + cn-north-1
   + cn-northwest-1
   + eu-south-1
   + ap-northeast-3
   + af-south-1
   + il-central-1

   For more information about CodeCommit and AWS Region, see [Regions and Git connection endpoints](regions.md). For more information about IAM, access keys, and secret keys, see [How Do I Get Credentials?](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAM_Introduction.html#IAM_SecurityCredentials) and [Managing Access Keys for IAM Users](https://docs.aws.amazon.com/IAM/latest/UserGuide/ManagingCredentials.html). For more information about the AWS CLI and profiles, see [Named Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).

Next, you must install Git. 
+ **For Linux, macOS, or Unix**:

  To work with files, commits, and other information in CodeCommit repositories, you must install Git on your local machine. CodeCommit supports Git versions 1.7.9 and later. Git version 2.28 supports configuring the branch name for initial commits. We recommend using a recent version of Git.

  To install Git, we recommend websites such as [Git Downloads](http://git-scm.com/downloads).
**Note**  
Git is an evolving, regularly updated platform. Occasionally, a feature change might affect the way it works with CodeCommit. If you encounter issues with a specific version of Git and CodeCommit, review the information in [Troubleshooting](troubleshooting.md).
+ **For Windows:** 

  To work with files, commits, and other information in CodeCommit repositories, you must install Git on your local machine. CodeCommit supports Git versions 1.7.9 and later. Git version 2.28 supports configuring the branch name for initial commits. We recommend using a recent version of Git.

  To install Git, we recommend websites such as [Git for Windows](https://gitforwindows.org/). If you use this link to install Git, you can accept all of the installation default settings except for the following: 
  + When prompted during the **Adjusting your PATH environment** step, choose the option to use Git from the command line.
  + (Optional) If you intend to use HTTPS with the credential helper that is included in the AWS CLI instead of configuring Git credentials for CodeCommit, on the **Configuring extra options** page, make sure the **Enable Git Credential Manager** option is cleared. The Git Credential Manager is only compatible with CodeCommit if IAM users configure Git credentials. For more information, see [For HTTPS users using Git credentials](setting-up-gc.md) and [Git for Windows: I installed Git for Windows, but I am denied access to my repository (403)](troubleshooting-ch.md#troubleshooting-windowshttps).
**Note**  
Git is an evolving, regularly updated platform. Occasionally, a feature change might affect the way it works with CodeCommit. If you encounter issues with a specific version of Git and CodeCommit, review the information in [Troubleshooting](troubleshooting.md).

CodeCommit supports both HTTPS and SSH authentication. To complete setup, you must configure Git credentials for CodeCommit (HTTPS, recommended for most users), an SSH key pair to use when accessing CodeCommit (SSH), **git-remote-codecommit** (recommended for users who use federated access), or the credential helper included in the AWS CLI (HTTPS). 
+ For Git credentials on all supported operating systems, see [Step 3: Create Git credentials for HTTPS connections to CodeCommit](setting-up-gc.md#setting-up-gc-iam).
+ For SSH on Linux, macOS, or Unix, see [SSH and Linux, macOS, or Unix: Set up the public and private keys for Git and CodeCommit](setting-up-ssh-unixes.md#setting-up-ssh-unixes-keys-unixes).
+  For SSH on Windows, see [Step 3:  Set up the public and private keys for Git and CodeCommit ](setting-up-ssh-windows.md#setting-up-ssh-windows-keys-windows).
+ For **git-remote-codecommit**, see [Setup steps for HTTPS connections to AWS CodeCommit with git-remote-codecommit](setting-up-git-remote-codecommit.md).
+ For the credential helper on Linux, macOS, or Unix, see [Set Up the Credential Helper (Linux, macOS, or Unix)](setting-up-https-unixes.md#setting-up-https-unixes-ch-config).
+ For the credential helper on Windows, see [Set Up the Credential Helper (Windows)](setting-up-https-windows.md#setting-up-https-windows-ch-config).

## Step 1: Create a CodeCommit repository
<a name="how-to-migrate-existing-create"></a>

In this section, you use the CodeCommit console to create the CodeCommit repository you use for the rest of this tutorial. To use the AWS CLI to create the repository, see [Create a repository (AWS CLI)](how-to-create-repository.md#how-to-create-repository-cli).

1. Open the CodeCommit console at [https://console.aws.amazon.com/codesuite/codecommit/home](https://console.aws.amazon.com/codesuite/codecommit/home).

1. In the region selector, choose the AWS Region where you want to create the repository. For more information, see [Regions and Git connection endpoints](regions.md).

1. On the **Repositories** page, choose **Create repository**. 

1. On the **Create repository** page, in **Repository name**, enter a name for the repository.
**Note**  
Repository names are case sensitive. The name must be unique in the AWS Region for your Amazon Web Services account.

1. (Optional) In **Description**, enter a description for the repository. This can help you and other users identify the purpose of the repository. 
**Note**  
The description field displays Markdown in the console and accepts all HTML characters and valid Unicode characters. If you are an application developer who is using the `GetRepository` or `BatchGetRepositories` APIs and you plan to display the repository description field in a web browser, see the [CodeCommit API Reference](https://docs.aws.amazon.com/codecommit/latest/APIReference/).

1. (Optional) Choose **Add tag** to add one or more repository tags (a custom attribute label that helps you organize and manage your AWS resources) to your repository. For more information, see [Tagging repositories in AWS CodeCommit](how-to-tag-repository.md).

1. (Optional) Expand **Additional configuration** to specify whether to use the default AWS managed key or your own customer managed key for encrypting and decrypting data in this repository. If you choose to use your own customer managed key, you must ensure that it is available in the AWS Region where you are creating the repository, and that the key is active. For more information, see [AWS Key Management Service and encryption for AWS CodeCommit repositories](encryption.md).

1. (Optional) Select **Enable Amazon CodeGuru Reviewer for Java and Python** if this repository contains Java or Python code, and you want CodeGuru Reviewer to analyze it. CodeGuru Reviewer uses multiple machine learning models to find code defects and to suggest improvements and fixes in pull requests. For more information, see the [https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/Welcome.html](https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/Welcome.html).

1. Choose **Create**. 

![\[Creating a repository for migrating a Git repository to CodeCommit\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-create-repo-migrate-existing.png)


After it is created, the repository appears in the **Repositories** list. In the URL column, choose the copy icon, and then choose the protocol (SSH or HTTPS) to be used to connect to CodeCommit. Copy the URL.

For example, if you named your repository *MyClonedRepository* and you are using Git credentials with HTTPS in the US East (Ohio) Region, the URL looks like the following:

```
https://git-codecommit.us-east-2.amazonaws.com/MyClonedRepository
```

You need this URL later in [Step 2: Clone the repository and push to the CodeCommit repository](#how-to-migrate-existing-clone).

## Step 2: Clone the repository and push to the CodeCommit repository
<a name="how-to-migrate-existing-clone"></a>

In this section, you clone a Git repository to your local computer, creating what is called a local repo. You then push the contents of the local repo to the CodeCommit repository you created earlier.

1. From the terminal or command prompt on your local computer, run the **git clone** command with the `--mirror` option to clone a bare copy of the remote repository into a new folder named *aws-codecommit-demo*. This is a bare repo meant only for migration. It is not the local repo for interacting with the migrated repository in CodeCommit. You can create that later, after the migration to CodeCommit is complete.

   The following example clones a demo application hosted on GitHub (*https://github.com/awslabs/aws-demo-php-simple-app.git*) to a local repo in a directory named *aws-codecommit-demo*. 

   ```
   git clone --mirror https://github.com/awslabs/aws-demo-php-simple-app.git aws-codecommit-demo
   ```

1. Change directories to the directory where you made the clone.

   ```
   cd aws-codecommit-demo
   ```

1. Run the **git push** command, specifying the URL and name of the destination CodeCommit repository and the **--all** option. (This is the URL you copied in [Step 1: Create a CodeCommit repository](#how-to-migrate-existing-create)).

   For example, if you named your repository *MyClonedRepository* and you are set up to use HTTPS, you would run the following command:

   ```
   git push https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyClonedRepository --all
   ```
**Note**  
The **--all** option only pushes all branches for the repository. It does not push other references, such as tags. If you want to push tags, wait until the initial push is complete, and then push again, this time using the **--tags** option:  

   ```
   git push ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyClonedRepository --tags
   ```
For more information, see [Git push](https://git-scm.com/docs/git-push) on the Git website. For information about pushing large repositories, especially when pushing all references at once (for example, with the **--mirror** option), see [Migrate a repository in increments](how-to-push-large-repositories.md).

You can delete the *aws-codecommit-demo* folder and its contents after you have migrated the repository to CodeCommit. To create a local repo with all the correct references for working with the repository in CodeCommit, run the `git clone` command without the `--mirror` option:

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

## Step 3: View files in CodeCommit
<a name="how-to-migrate-existing-view"></a>

After you have pushed the contents of your directory, you can use the CodeCommit console to quickly view all of the files in that repository.

1. Open the CodeCommit console at [https://console.aws.amazon.com/codesuite/codecommit/home](https://console.aws.amazon.com/codesuite/codecommit/home).

1. In **Repositories**, choose the name of the repository (for example, *MyClonedRepository*). 

1. View the files in the repository for the branches, the clone URLs, the settings, and more.  
![\[View of a cloned repository in CodeCommit\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-cloned-repo-url.png)

## Step 4: Share the CodeCommit repository
<a name="how-to-migrate-existing-share"></a>

When you create a repository in CodeCommit, two endpoints are generated: one for HTTPS connections and one for SSH connections. Both provide secure connections over a network. Your users can use either protocol. Both endpoints remain active no matter which protocol you recommend to your users. Before you can share your repository with others, you must create IAM policies that allow other users access to your repository. Provide those access instructions to your users. 

**Create a customer managed policy for your repository**

1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the **Dashboard** navigation area, choose **Policies**, and then choose **Create Policy**. 

1. On the **Create Policy** page,, choose **Import managed policy**.

1. On the **Import managed policies** page, in **Filter policies**, enter **AWSCodeCommitPowerUser**. Choose the button next to the policy name and then choose **Import**.

1. On the **Create policy** page, choose **JSON**. Replace the "\$1" portion of the `Resource` line for CodeCommit actions with the Amazon Resource Name (ARN) of the CodeCommit repository, as shown here:

   ```
   "Resource": [
    "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo"
    ]
   ```
**Tip**  
To find the ARN for the CodeCommit repository, go to the CodeCommit console, choose the repository name from the list, and then choose **Settings**. For more information, see [View repository details](how-to-view-repository-details.md).

   If you want this policy to apply to more than one repository, add each repository as a resource by specifying its ARN. Include a comma between each resource statement, as shown here:

   ```
   "Resource": [
    "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo",
    "arn:aws:codecommit:us-east-2:111111111111:MyOtherDemoRepo"
    ]
   ```

   When you are finished editing, choose **Review policy**.

1. On the **Review Policy** page, in **Name**, enter a new name for the policy (for example, *AWSCodeCommitPowerUser-MyDemoRepo*). Optionally provide a description for this policy.

1. Choose **Create Policy**.

To manage access to your repository, create an IAM group for its users, add IAM users to that group, and then attach the customer managed policy you created in the previous step. Attach any other policies required for access, such as IAMUserSSHKeys or IAMSelfManageServiceSpecificCredentials. 

1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the **Dashboard** navigation area, choose **Groups**, and then choose **Create New Group**. 

1. On the **Set Group Name** page, in **Group Name**, enter a name for the group (for example, *MyDemoRepoGroup*), and then choose **Next Step**. Consider including the repository name as part of the group name.
**Note**  
This name must be unique across an Amazon Web Services account.

1. Select the box next to the customer managed policy you created in the previous section (for example, **AWSCodeCommitPowerUser-MyDemoRepo**). 

1. On the **Review** page, choose **Create Group**. IAM creates this group with the specified policies already attached. The group appears in the list of groups associated with your Amazon Web Services account.

1. Choose your group from the list. 

1. On the group summary page, choose the **Users** tab, and then choose **Add Users to Group**. On the list that shows all users associated with your Amazon Web Services account, select the boxes next to the users to whom you want to allow access to the CodeCommit repository, and then choose **Add Users**.
**Tip**  
You can use the Search box to quickly find users by name.

1. When you have added your users, close the IAM console.

After you have created an IAM user to access CodeCommit using the policy group and policies you configured, send that user the information required to connect to the repository.

1. Open the CodeCommit console at [https://console.aws.amazon.com/codesuite/codecommit/home](https://console.aws.amazon.com/codesuite/codecommit/home).

1. In the region selector, choose the AWS Region where the repository was created. Repositories are specific to an AWS Region. For more information, see [Regions and Git connection endpoints](regions.md).

1. On the **Repositories** page, choose the repository you want to share. 

1. In **Clone URL**, choose the protocol that you want your users to use. This copies the clone URL for the connection protocol. 

1. Send your users the clone URL along with any other instructions, such as installing the AWS CLI, configuring a profile, or installing Git. Make sure to include the configuration information for the connection protocol (for example, HTTPS). 

# Migrate local or unversioned content to AWS CodeCommit
<a name="how-to-migrate-repository-local"></a>

The procedures in this topic show you how to migrate an existing project or local content on your computer to a CodeCommit repository. As part of this process, you:
+ Complete the initial setup required for CodeCommit.
+ Create a CodeCommit repository.
+ Place a local folder under Git version control and push the contents of that folder to the CodeCommit repository.
+ View files in the CodeCommit repository.
+ Share the CodeCommit repository with your team.

![\[Migrating a local project to CodeCommit\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-migrate-local.png)


**Topics**
+ [Step 0: Setup required for access to CodeCommit](#how-to-migrate-local-setup)
+ [Step 1: Create a CodeCommit repository](#how-to-migrate-local-create)
+ [Step 2: Migrate local content to the CodeCommit repository](#how-to-migrate-local-version)
+ [Step 3: View files in CodeCommit](#how-to-migrate-local-view)
+ [Step 4: Share the CodeCommit repository](#how-to-migrate-local-share)

## Step 0: Setup required for access to CodeCommit
<a name="how-to-migrate-local-setup"></a>

Before you can migrate local content to CodeCommit, you must create and configure an IAM user for CodeCommit and configure your local computer for access. You should also install the AWS CLI to manage CodeCommit. Although you can perform most CodeCommit tasks without it, the AWS CLI offers flexibility when working with Git. 

If you are already set up for CodeCommit, you can skip ahead to [Step 1: Create a CodeCommit repository](#how-to-migrate-local-create).

**To create and configure an IAM user for accessing CodeCommit**

1. Create an Amazon Web Services account by going to [http://aws.amazon.com](http://aws.amazon.com) and choosing **Sign Up**.

1. Create an IAM user, or use an existing one, in your Amazon Web Services account. Make sure you have an access key ID and a secret access key associated with that IAM user. For more information, see [Creating an IAM User in Your Amazon Web Services account](https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html).
**Note**  
CodeCommit requires AWS Key Management Service. If you are using an existing IAM user, make sure there are no policies attached to the user that expressly deny the AWS KMS actions required by CodeCommit. For more information, see [AWS KMS and encryption](encryption.md).

1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the IAM console, in the navigation pane, choose **Users**, and then choose the IAM user you want to configure for CodeCommit access.

1. On the **Permissions** tab, choose **Add Permissions**. 

1. In **Grant permissions**, choose **Attach existing policies directly**.

1. From the list of policies, select **AWSCodeCommitPowerUser** or another managed policy for CodeCommit access. For more information, see [AWS managed policies for CodeCommit](security-iam-awsmanpol.md).

   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. If the list is correct, choose **Add permissions**.

    For more information about CodeCommit managed policies and sharing access to repositories with other groups and users, see [Share a repository](how-to-share-repository.md) and [Authentication and access control for AWS CodeCommit](auth-and-access-control.md).

**To install and configure the AWS CLI**

1. On your local machine, download and install the AWS CLI. This is a prerequisite for interacting with CodeCommit from the command line. We recommend that you install AWS CLI version 2. It is the most recent major version of the AWS CLI and supports all of the latest features. It is the only version of the AWS CLI that supports using a root account, federated access, or temporary credentials with **git-remote-codecommit**.

   For more information, see [Getting Set Up with the AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html).
**Note**  
CodeCommit works only with AWS CLI versions 1.7.38 and later. As a best practice, install or upgrade the AWS CLI to the latest version available. To determine which version of the AWS CLI you have installed, run the **aws --version** command.  
To upgrade an older version of the AWS CLI to the latest version, see [Installing the AWS Command Line Interface](https://docs.aws.amazon.com/cli/latest/userguide/installing.html).

1. Run this command to verify that the CodeCommit commands for the AWS CLI are installed.

   ```
   aws codecommit help
   ```

   This command returns a list of CodeCommit commands.

1. Configure the AWS CLI with a profile by using the **configure** command, as follows:.

   ```
   aws configure
   ```

   When prompted, specify the AWS access key and AWS secret access key of the IAM user to use with CodeCommit. Also, be sure to specify the AWS Region where the repository exists, such as `us-east-2`. When prompted for the default output format, specify `json`. For example, if you are configuring a profile for an IAM user:

   ```
   AWS Access Key ID [None]: Type your IAM user AWS access key ID here, and then press Enter
   AWS Secret Access Key [None]: Type your IAM user AWS secret access key here, and then press Enter
   Default region name [None]: Type a supported region for CodeCommit here, and then press Enter
   Default output format [None]: Type json here, and then press Enter
   ```

   For more information about creating and configuring profiles to use with the AWS CLI, see the following:
   + [Named Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)
   + [Using an IAM Role in the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-role.html)
   + [Set command](https://docs.aws.amazon.com/cli/latest/reference/set.html)
   + [Connecting to AWS CodeCommit repositories with rotating credentials](temporary-access.md)

   To connect to a repository or a resource in another AWS Region, you must reconfigure the AWS CLI with the default Region name. Supported default Region names for CodeCommit include:
   + us-east-2
   + us-east-1
   + eu-west-1
   + us-west-2
   + ap-northeast-1
   + ap-southeast-1
   + ap-southeast-2
   + ap-southeast-3
   + me-central-1
   + eu-central-1
   + ap-northeast-2
   + sa-east-1
   + us-west-1
   + eu-west-2
   + ap-south-1
   + ap-south-1
   + ca-central-1
   + us-gov-west-1
   + us-gov-east-1
   + eu-north-1
   + ap-east-1
   + me-south-1
   + cn-north-1
   + cn-northwest-1
   + eu-south-1
   + ap-northeast-3
   + af-south-1
   + il-central-1

   For more information about CodeCommit and AWS Region, see [Regions and Git connection endpoints](regions.md). For more information about IAM, access keys, and secret keys, see [How Do I Get Credentials?](https://docs.aws.amazon.com/IAM/latest/UserGuide/IAM_Introduction.html#IAM_SecurityCredentials) and [Managing Access Keys for IAM Users](https://docs.aws.amazon.com/IAM/latest/UserGuide/ManagingCredentials.html). For more information about the AWS CLI and profiles, see [Named Profiles](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html).

Next, you must install Git. 
+ **For Linux, macOS, or Unix**:

  To work with files, commits, and other information in CodeCommit repositories, you must install Git on your local machine. CodeCommit supports Git versions 1.7.9 and later. Git version 2.28 supports configuring the branch name for initial commits. We recommend using a recent version of Git.

  To install Git, we recommend websites such as [Git Downloads](http://git-scm.com/downloads).
**Note**  
Git is an evolving, regularly updated platform. Occasionally, a feature change might affect the way it works with CodeCommit. If you encounter issues with a specific version of Git and CodeCommit, review the information in [Troubleshooting](troubleshooting.md).
+ **For Windows:** 

  To work with files, commits, and other information in CodeCommit repositories, you must install Git on your local machine. CodeCommit supports Git versions 1.7.9 and later. Git version 2.28 supports configuring the branch name for initial commits. We recommend using a recent version of Git.

  To install Git, we recommend websites such as [Git for Windows](https://gitforwindows.org/). If you use this link to install Git, you can accept all of the installation default settings except for the following: 
  + When prompted during the **Adjusting your PATH environment** step, choose the option to use Git from the command line.
  + (Optional) If you intend to use HTTPS with the credential helper that is included in the AWS CLI instead of configuring Git credentials for CodeCommit, on the **Configuring extra options** page, make sure the **Enable Git Credential Manager** option is cleared. The Git Credential Manager is only compatible with CodeCommit if IAM users configure Git credentials. For more information, see [For HTTPS users using Git credentials](setting-up-gc.md) and [Git for Windows: I installed Git for Windows, but I am denied access to my repository (403)](troubleshooting-ch.md#troubleshooting-windowshttps).
**Note**  
Git is an evolving, regularly updated platform. Occasionally, a feature change might affect the way it works with CodeCommit. If you encounter issues with a specific version of Git and CodeCommit, review the information in [Troubleshooting](troubleshooting.md).

CodeCommit supports both HTTPS and SSH authentication. To complete setup, you must configure Git credentials for CodeCommit (HTTPS, recommended for most users), an SSH key pair (SSH) to use when accessing CodeCommit, **git-remote-codecommit** (recommended for users who use federated access), or the credential helper included in the AWS CLI. 
+ For Git credentials on all supported operating systems, see [Step 3: Create Git credentials for HTTPS connections to CodeCommit](setting-up-gc.md#setting-up-gc-iam).
+ For SSH on Linux, macOS, or Unix, see [SSH and Linux, macOS, or Unix: Set up the public and private keys for Git and CodeCommit](setting-up-ssh-unixes.md#setting-up-ssh-unixes-keys-unixes).
+  For SSH on Windows, see [Step 3:  Set up the public and private keys for Git and CodeCommit ](setting-up-ssh-windows.md#setting-up-ssh-windows-keys-windows).
+ For **git-remote-codecommit**, see [Setup steps for HTTPS connections to AWS CodeCommit with git-remote-codecommit](setting-up-git-remote-codecommit.md).
+ For the credential helper on Linux, macOS, or Unix, see [Set Up the Credential Helper (Linux, macOS, or Unix)](setting-up-https-unixes.md#setting-up-https-unixes-ch-config).
+ For the credential helper on Windows, see [Set Up the Credential Helper (Windows)](setting-up-https-windows.md#setting-up-https-windows-ch-config).

## Step 1: Create a CodeCommit repository
<a name="how-to-migrate-local-create"></a>

In this section, you use the CodeCommit console to create the CodeCommit repository you use for the rest of this tutorial. To use the AWS CLI to create the repository, see [Create a repository (AWS CLI)](how-to-create-repository.md#how-to-create-repository-cli).

1. Open the CodeCommit console at [https://console.aws.amazon.com/codesuite/codecommit/home](https://console.aws.amazon.com/codesuite/codecommit/home).

1. In the region selector, choose the AWS Region where you want to create the repository. For more information, see [Regions and Git connection endpoints](regions.md).

1. On the **Repositories** page, choose **Create repository**. 

1. On the **Create repository** page, in **Repository name**, enter a name for the repository.
**Note**  
Repository names are case sensitive. The name must be unique in the AWS Region for your Amazon Web Services account.

1. (Optional) In **Description**, enter a description for the repository. This can help you and other users identify the purpose of the repository. 
**Note**  
The description field displays Markdown in the console and accepts all HTML characters and valid Unicode characters. If you are an application developer who is using the `GetRepository` or `BatchGetRepositories` APIs and you plan to display the repository description field in a web browser, see the [CodeCommit API Reference](https://docs.aws.amazon.com/codecommit/latest/APIReference/).

1. (Optional) Choose **Add tag** to add one or more repository tags (a custom attribute label that helps you organize and manage your AWS resources) to your repository. For more information, see [Tagging repositories in AWS CodeCommit](how-to-tag-repository.md).

1. (Optional) Expand **Additional configuration** to specify whether to use the default AWS managed key or your own customer managed key for encrypting and decrypting data in this repository. If you choose to use your own customer managed key, you must ensure that it is available in the AWS Region where you are creating the repository, and that the key is active. For more information, see [AWS Key Management Service and encryption for AWS CodeCommit repositories](encryption.md).

1. (Optional) Select **Enable Amazon CodeGuru Reviewer for Java and Python** if this repository contains Java or Python code, and you want CodeGuru Reviewer to analyze it. CodeGuru Reviewer uses multiple machine learning models to find code defects and to suggest improvements and fixes in pull requests. For more information, see the [https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/Welcome.html](https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/Welcome.html).

1. Choose **Create**. 

After it is created, the repository appears in the **Repositories** list. In the URL column, choose the copy icon, and then choose the protocol (HTTPS or SSH) to be used to connect to CodeCommit. Copy the URL.

For example, if you named your repository *MyFirstRepo* and you are using HTTPS, the URL would look like the following:

```
https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyFirstRepo
```

You need this URL later in [Step 2: Migrate local content to the CodeCommit repository](#how-to-migrate-local-version).

## Step 2: Migrate local content to the CodeCommit repository
<a name="how-to-migrate-local-version"></a>

Now that you have a CodeCommit repository, you can choose a directory on your local computer to convert into a local Git repository. The **git init** command can be used to either convert existing, unversioned content to a Git repository or, if you do not yet have files or content, to initialize a new, empty repository.

1. From the terminal or command line on your local computer, change directories to the directory you want to use as the source for your repository.

1. Run the following command to configure Git to use a default branch named **main**:

   ```
   git config --local init.defaultBranch main
   ```

   You can also run this command to set your default branch name to **main** for all newly-created repositories:

   ```
   git config --global init.defaultBranch main
   ```

1. Run the **git init** command to initialize Git version control in the directory. This creates a .git subdirectory in the root of the directory that enables version control tracking. The .git folder also contains all of the required metadata for the repository. 

   ```
   git init
   ```

1. Check the status of the initialized directory by running the following command:

   ```
   git status
   ```

   Add the files you want to add to version control. In this tutorial, you run the `git add` command with the `.` specifier to add all of the files in this directory. For other options, consult your Git documentation. 

   ```
   git add .
   ```

1. Create a commit for the added files with a commit message. 

   ```
   git commit -m "Initial commit"
   ```

1. Run the **git push** command, specifying the URL and name of the destination CodeCommit repository and the `--all` option. (This is the URL you copied in [Step 1: Create a CodeCommit repository](#how-to-migrate-local-create).)

   For example, if you named your repository *MyFirstRepo* and you are set up to use HTTPS, you would run the following command:

   ```
   git push https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyFirstRepo --all
   ```

## Step 3: View files in CodeCommit
<a name="how-to-migrate-local-view"></a>

After you have pushed the contents of your directory, you can use the CodeCommit console to quickly view all of the files in the repository.

1. Open the CodeCommit console at [https://console.aws.amazon.com/codesuite/codecommit/home](https://console.aws.amazon.com/codesuite/codecommit/home).

1. In **Repositories**, choose the name of the repository (for example, *MyFirstRepository*) from the list. 

1. View the files in the repository for the branches, clone URLs, settings, and more.

## Step 4: Share the CodeCommit repository
<a name="how-to-migrate-local-share"></a>

When you create a repository in CodeCommit, two endpoints are generated: one for HTTPS connections and one for SSH connections. Both provide secure connections over a network. Your users can use either protocol. Both endpoints remain active no matter which protocol you recommend to your users. Before you can share your repository with others, you must create IAM policies that allow other users access to your repository. Provide those access instructions to your users. 

**Create a customer managed policy for your repository**

1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the **Dashboard** navigation area, choose **Policies**, and then choose **Create Policy**. 

1. On the **Create Policy** page,, choose **Import managed policy**.

1. On the **Import managed policies** page, in **Filter policies**, enter **AWSCodeCommitPowerUser**. Choose the button next to the policy name and then choose **Import**.

1. On the **Create policy** page, choose **JSON**. Replace the "\$1" portion of the `Resource` line for CodeCommit actions with the Amazon Resource Name (ARN) of the CodeCommit repository, as shown here:

   ```
   "Resource": [
    "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo"
    ]
   ```
**Tip**  
To find the ARN for the CodeCommit repository, go to the CodeCommit console, choose the repository name from the list, and then choose **Settings**. For more information, see [View repository details](how-to-view-repository-details.md).

   If you want this policy to apply to more than one repository, add each repository as a resource by specifying its ARN. Include a comma between each resource statement, as shown here:

   ```
   "Resource": [
    "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo",
    "arn:aws:codecommit:us-east-2:111111111111:MyOtherDemoRepo"
    ]
   ```

   When you are finished editing, choose **Review policy**.

1. On the **Review Policy** page, in **Name**, enter a new name for the policy (for example, *AWSCodeCommitPowerUser-MyDemoRepo*). Optionally provide a description for this policy.

1. Choose **Create Policy**.

To manage access to your repository, create an IAM group for its users, add IAM users to that group, and then attach the customer managed policy you created in the previous step. Attach any other policies required for access, such as `IAMSelfManageServiceSpecificCredentials` or `IAMUserSSHKeys`. 

1. Sign in to the AWS Management Console and open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the **Dashboard** navigation area, choose **Groups**, and then choose **Create New Group**. 

1. On the **Set Group Name** page, in **Group Name**, enter a name for the group (for example, *MyDemoRepoGroup*), and then choose **Next Step**. Consider including the repository name as part of the group name.
**Note**  
This name must be unique across an Amazon Web Services account.

1. Select the box next to the customer managed policy you created in the previous section (for example, **AWSCodeCommitPowerUser-MyDemoRepo**). 

1. On the **Review** page, choose **Create Group**. IAM creates this group with the specified policies already attached. The group appears in the list of groups associated with your Amazon Web Services account.

1. Choose your group from the list. 

1. On the group summary page, choose the **Users** tab, and then choose **Add Users to Group**. On the list that shows all users associated with your Amazon Web Services account, select the boxes next to the users to whom you want to allow access to the CodeCommit repository, and then choose **Add Users**.
**Tip**  
You can use the Search box to quickly find users by name.

1. When you have added your users, close the IAM console.

After you have created an IAM user to be used to access CodeCommit using the policy group and policies you configured, send that user the information required to connect to the repository.

1. Open the CodeCommit console at [https://console.aws.amazon.com/codesuite/codecommit/home](https://console.aws.amazon.com/codesuite/codecommit/home).

1. In the region selector, choose the AWS Region where the repository was created. Repositories are specific to an AWS Region. For more information, see [Regions and Git connection endpoints](regions.md).

1. On the **Repositories** page, choose the repository you want to share. 

1. In **Clone URL**, choose the protocol that you want your users to use. This copies the clone URL for the connection protocol. 

1. Send your users the clone URL along with any other instructions, such as installing the AWS CLI, configuring a profile, or installing Git. Make sure to include the configuration information for the connection protocol (for example, HTTPS). 

# Migrate a repository incrementally
<a name="how-to-push-large-repositories"></a>

When migrating to AWS CodeCommit, consider pushing your repository in increments or chunks to reduce the chances an intermittent network issue or degraded network performance causes the entire push to fail. By using incremental pushes with a script like the one included here, you can restart the migration and push only those commits that did not succeed on the earlier attempt.

The procedures in this topic show you how to create and run a script that migrates your repository in increments and repushes only those increments that did not succeed until the migration is complete.

These instructions are written with the assumption that you have already completed the steps in [Setting up ](setting-up.md) and [Create a repository](how-to-create-repository.md). 

**Topics**
+ [Step 0: Determine whether to migrate incrementally](#how-to-push-large-repositories-determine)
+ [Step 1: Install prerequisites and add the CodeCommit repository as a remote](#how-to-push-large-repositories-prereq)
+ [Step 2: Create the script to use for migrating incrementally](#how-to-push-large-repositories-createscript)
+ [Step 3: Run the script and migrate incrementally to CodeCommit](#how-to-push-large-repositories-runscript)
+ [Appendix: Sample script `incremental-repo-migration.py`](#how-to-push-large-repositories-sample)

## Step 0: Determine whether to migrate incrementally
<a name="how-to-push-large-repositories-determine"></a>

There are several factors to consider to determine the overall size of your repository and whether to migrate incrementally. The most obvious is the overall size of the artifacts in the repository. Factors such as the accumulated history of the repository can also contribute to size. A repository with years of history and branches can be very large, even though the individual assets are not. There are a number of strategies you can pursue to make migrating these repositories simpler and more efficient. For example, you can use a shallow clone strategy when cloning a repository with a long history of development, or you can turn off delta compression for large binary files. You can research options by consulting your Git documentation, or you can choose to set up and configure incremental pushes for migrating your repository using the sample script included in this topic, `incremental-repo-migration.py`. 

You might want to configure incremental pushes if one or more of the following conditions is true:
+ The repository you want to migrate has more than five years of history.
+ Your internet connection is subject to intermittent outages, dropped packets, slow response, or other interruptions in service.
+ The overall size of the repository is larger than 2 GB and you intend to migrate the entire repository.
+ The repository contains large artifacts or binaries that do not compress well, such as large image files with more than five tracked versions.
+ You have previously attempted a migration to CodeCommit and received an "Internal Service Error" message. 

Even if none of the above conditions are true, you can still choose to push incrementally.

## Step 1: Install prerequisites and add the CodeCommit repository as a remote
<a name="how-to-push-large-repositories-prereq"></a>

You can create your own custom script, which has its own prerequisites. If you use the sample included in this topic, you must:
+ Install its prerequisites.
+ Clone the repository to your local computer.
+ Add the CodeCommit repository as a remote for the repository you want to migrate.

**Set up to run incremental-repo-migration.py**

1.  On your local computer, install Python 2.6 or later. For more information and the latest versions, see [the Python website](https://www.python.org/downloads/).

1. On the same computer, install GitPython, which is a Python library used to interact with Git repositories. For more information, see [the GitPython documentation](http://gitpython.readthedocs.org/en/stable/).

1.  Use the **git clone --mirror** command to clone the repository you want to migrate to your local computer. From the terminal (Linux, macOS, or Unix) or the command prompt (Windows), use the **git clone --mirror** command to create a local repo for the repository, including the directory where you want to create the local repo. For example, to clone a Git repository named *MyMigrationRepo* with a URL of *https://example.com/my-repo/* to a directory named *my-repo*:

   ```
   git clone --mirror https://example.com/my-repo/MyMigrationRepo.git my-repo
   ```

   You should see output similar to the following, which indicates the repository has been cloned into a bare local repo named my-repo:

   ```
   Cloning into bare repository 'my-repo'...
   remote: Counting objects: 20, done.
   remote: Compressing objects: 100% (17/17), done.
   remote: Total 20 (delta 5), reused 15 (delta 3)
   Unpacking objects: 100% (20/20), done.
   Checking connectivity... done.
   ```

1. Change directories to the local repo for the repository you just cloned (for example, *my-repo*). From that directory, use the **git remote add *DefaultRemoteName* *RemoteRepositoryURL*** command to add the CodeCommit repository as a remote repository for the local repo.
**Note**  
When pushing large repositories, consider using SSH instead of HTTPS. When you push a large change, a large number of changes, or a large repository, long-running HTTPS connections are often terminated prematurely due to networking issues or firewall settings. For more information about setting up CodeCommit for SSH, see [For SSH connections on Linux, macOS, or Unix](setting-up-ssh-unixes.md) or [For SSH connections on Windows](setting-up-ssh-windows.md).

    For example, use the following command to add the SSH endpoint for a CodeCommit repository named MyDestinationRepo as a remote repository for the remote named `codecommit`: 

   ```
   git remote add codecommit ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDestinationRepo
   ```
**Tip**  
Because this is a clone, the default remote name (`origin`) is already in use. You must use another remote name. Although the example uses `codecommit`, you can use any name you want. Use the **git remote show** command to review the list of remotes set for your local repo.

1. Use the **git remote -v** command to display the fetch and push settings for your local repo and confirm they are set correctly. For example:

   ```
   codecommit  ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDestinationRepo (fetch)
   codecommit  ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDestinationRepo (push)
   ```
**Tip**  
If you still see fetch and push entries for a different remote repository (for example, entries for origin), use the **git remote set-url --delete** command to remove them.

## Step 2: Create the script to use for migrating incrementally
<a name="how-to-push-large-repositories-createscript"></a>

These steps are written with the assumption that you are using the `incremental-repo-migration.py` sample script. 

1. Open a text editor and paste the contents of [the sample script](#how-to-push-large-repositories-sample) into an empty document.

1. Save the document in a documents directory (not the working directory of your local repo) and name it `incremental-repo-migration.py`. Make sure the directory you choose is one configured in your local environment or path variables, so you can run the Python script from a command line or terminal.

## Step 3: Run the script and migrate incrementally to CodeCommit
<a name="how-to-push-large-repositories-runscript"></a>

 Now that you have created your `incremental-repo-migration.py` script, you can use it to incrementally migrate a local repo to a CodeCommit repository. By default, the script pushes commits in batches of 1,000 commits and attempts to use the Git settings for the directory from which it is run as the settings for the local repo and remote repository. You can use the options included in `incremental-repo-migration.py` to configure other settings, if necessary.

1. From the terminal or command prompt, change directories to the local repo you want to migrate.

1. From that directory, run the following command:

   ```
   python incremental-repo-migration.py
   ```

1. The script runs and shows progress at the terminal or command prompt. Some large repositories are slow to show progress. The script stops if a single push fails three times. You can then rerun the script, and it starts from the batch that failed. You can rerun the script until all pushes succeed and the migration is complete.

**Tip**  
You can run `incremental-repo-migration.py` from any directory as long as you use the `-l` and `-r` options to specify the local and remote settings to use. For example, to use the script from any directory to migrate a local repo located at /tmp/*my-repo* to a remote nicknamed *codecommit*:  

```
python incremental-repo-migration.py -l "/tmp/my-repo" -r "codecommit" 
```
 You might also want to use the `-b` option to change the default batch size used when pushing incrementally. For example, if you are regularly pushing a repository with very large binary files that change often and are working from a location that has restricted network bandwidth, you might want to use the `-b` option to change the batch size to 500 instead of 1,000. For example:  

```
python incremental-repo-migration.py -b 500
```
This pushes the local repo incrementally in batches of 500 commits. If you decide to change the batch size again when you migrate the repository (for example, if you decide to decrease the batch size after an unsuccessful attempt), remember to use the `-c` option to remove the batch tags before resetting the batch size with `-b`:  

```
python incremental-repo-migration.py -c
python incremental-repo-migration.py -b 250
```

**Important**  
Do not use the `-c` option if you want to rerun the script after a failure. The `-c` option removes the tags used to batch the commits. Use the `-c` option only if you want to change the batch size and start again, or if you decide you no longer want to use the script.

## Appendix: Sample script `incremental-repo-migration.py`
<a name="how-to-push-large-repositories-sample"></a>

For your convenience, we have developed a sample Python script, `incremental-repo-migration.py`, for pushing a repository incrementally. This script is an open source code sample and provided as-is.

```
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Amazon Software License (the "License").
# You may not use this file except in compliance with the License. A copy of the License is located at
#    http://aws.amazon.com/asl/
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for
# the specific language governing permissions and limitations under the License.

#!/usr/bin/env python

import os
import sys
from optparse import OptionParser
from git import Repo, TagReference, RemoteProgress, GitCommandError


class PushProgressPrinter(RemoteProgress):
    def update(self, op_code, cur_count, max_count=None, message=""):
        op_id = op_code & self.OP_MASK
        stage_id = op_code & self.STAGE_MASK
        if op_id == self.WRITING and stage_id == self.BEGIN:
            print("\tObjects: %d" % max_count)


class RepositoryMigration:
    MAX_COMMITS_TOLERANCE_PERCENT = 0.05
    PUSH_RETRY_LIMIT = 3
    MIGRATION_TAG_PREFIX = "codecommit_migration_"

    def migrate_repository_in_parts(
        self, repo_dir, remote_name, commit_batch_size, clean
    ):
        self.next_tag_number = 0
        self.migration_tags = []
        self.walked_commits = set()
        self.local_repo = Repo(repo_dir)
        self.remote_name = remote_name
        self.max_commits_per_push = commit_batch_size
        self.max_commits_tolerance = (
            self.max_commits_per_push * self.MAX_COMMITS_TOLERANCE_PERCENT
        )

        try:
            self.remote_repo = self.local_repo.remote(remote_name)
            self.get_remote_migration_tags()
        except (ValueError, GitCommandError):
            print(
                "Could not contact the remote repository. The most common reasons for this error are that the name of the remote repository is incorrect, or that you do not have permissions to interact with that remote repository."
            )
            sys.exit(1)

        if clean:
            self.clean_up(clean_up_remote=True)
            return

        self.clean_up()

        print("Analyzing repository")
        head_commit = self.local_repo.head.commit
        sys.setrecursionlimit(max(sys.getrecursionlimit(), head_commit.count()))

        # tag commits on default branch
        leftover_commits = self.migrate_commit(head_commit)
        self.tag_commits([commit for (commit, commit_count) in leftover_commits])

        # tag commits on each branch
        for branch in self.local_repo.heads:
            leftover_commits = self.migrate_commit(branch.commit)
            self.tag_commits([commit for (commit, commit_count) in leftover_commits])

        # push the tags
        self.push_migration_tags()

        # push all branch references
        for branch in self.local_repo.heads:
            print("Pushing branch %s" % branch.name)
            self.do_push_with_retries(ref=branch.name)

        # push all tags
        print("Pushing tags")
        self.do_push_with_retries(push_tags=True)

        self.get_remote_migration_tags()
        self.clean_up(clean_up_remote=True)

        print("Migration to CodeCommit was successful")

    def migrate_commit(self, commit):
        if commit in self.walked_commits:
            return []

        pending_ancestor_pushes = []
        commit_count = 1

        if len(commit.parents) > 1:
            # This is a merge commit
            # Ensure that all parents are pushed first
            for parent_commit in commit.parents:
                pending_ancestor_pushes.extend(self.migrate_commit(parent_commit))
        elif len(commit.parents) == 1:
            # Split linear history into individual pushes
            next_ancestor, commits_to_next_ancestor = self.find_next_ancestor_for_push(
                commit.parents[0]
            )
            commit_count += commits_to_next_ancestor
            pending_ancestor_pushes.extend(self.migrate_commit(next_ancestor))

        self.walked_commits.add(commit)

        return self.stage_push(commit, commit_count, pending_ancestor_pushes)

    def find_next_ancestor_for_push(self, commit):
        commit_count = 0

        # Traverse linear history until we reach our commit limit, a merge commit, or an initial commit
        while (
            len(commit.parents) == 1
            and commit_count < self.max_commits_per_push
            and commit not in self.walked_commits
        ):
            commit_count += 1
            self.walked_commits.add(commit)
            commit = commit.parents[0]

        return commit, commit_count

    def stage_push(self, commit, commit_count, pending_ancestor_pushes):
        # Determine whether we can roll up pending ancestor pushes into this push
        combined_commit_count = commit_count + sum(
            ancestor_commit_count
            for (ancestor, ancestor_commit_count) in pending_ancestor_pushes
        )

        if combined_commit_count < self.max_commits_per_push:
            # don't push anything, roll up all pending ancestor pushes into this pending push
            return [(commit, combined_commit_count)]

        if combined_commit_count <= (
            self.max_commits_per_push + self.max_commits_tolerance
        ):
            # roll up everything into this commit and push
            self.tag_commits([commit])
            return []

        if commit_count >= self.max_commits_per_push:
            # need to push each pending ancestor and this commit
            self.tag_commits(
                [
                    ancestor
                    for (ancestor, ancestor_commit_count) in pending_ancestor_pushes
                ]
            )
            self.tag_commits([commit])
            return []

        # push each pending ancestor, but roll up this commit
        self.tag_commits(
            [ancestor for (ancestor, ancestor_commit_count) in pending_ancestor_pushes]
        )
        return [(commit, commit_count)]

    def tag_commits(self, commits):
        for commit in commits:
            self.next_tag_number += 1
            tag_name = self.MIGRATION_TAG_PREFIX + str(self.next_tag_number)

            if tag_name not in self.remote_migration_tags:
                tag = self.local_repo.create_tag(tag_name, ref=commit)
                self.migration_tags.append(tag)
            elif self.remote_migration_tags[tag_name] != str(commit):
                print(
                    "Migration tags on the remote do not match the local tags. Most likely your batch size has changed since the last time you ran this script. Please run this script with the --clean option, and try again."
                )
                sys.exit(1)

    def push_migration_tags(self):
        print("Will attempt to push %d tags" % len(self.migration_tags))
        self.migration_tags.sort(
            key=lambda tag: int(tag.name.replace(self.MIGRATION_TAG_PREFIX, ""))
        )
        for tag in self.migration_tags:
            print(
                "Pushing tag %s (out of %d tags), commit %s"
                % (tag.name, self.next_tag_number, str(tag.commit))
            )
            self.do_push_with_retries(ref=tag.name)

    def do_push_with_retries(self, ref=None, push_tags=False):
        for i in range(0, self.PUSH_RETRY_LIMIT):
            if i == 0:
                progress_printer = PushProgressPrinter()
            else:
                progress_printer = None

            try:
                if push_tags:
                    infos = self.remote_repo.push(tags=True, progress=progress_printer)
                elif ref is not None:
                    infos = self.remote_repo.push(
                        refspec=ref, progress=progress_printer
                    )
                else:
                    infos = self.remote_repo.push(progress=progress_printer)

                success = True
                if len(infos) == 0:
                    success = False
                else:
                    for info in infos:
                        if (
                            info.flags & info.UP_TO_DATE
                            or info.flags & info.NEW_TAG
                            or info.flags & info.NEW_HEAD
                        ):
                            continue
                        success = False
                        print(info.summary)

                if success:
                    return
            except GitCommandError as err:
                print(err)

        if push_tags:
            print("Pushing all tags failed after %d attempts" % (self.PUSH_RETRY_LIMIT))
        elif ref is not None:
            print("Pushing %s failed after %d attempts" % (ref, self.PUSH_RETRY_LIMIT))
            print(
                "For more information about the cause of this error, run the following command from the local repo: 'git push %s %s'"
                % (self.remote_name, ref)
            )
        else:
            print(
                "Pushing all branches failed after %d attempts"
                % (self.PUSH_RETRY_LIMIT)
            )
        sys.exit(1)

    def get_remote_migration_tags(self):
        remote_tags_output = self.local_repo.git.ls_remote(
            self.remote_name, tags=True
        ).split("\n")
        self.remote_migration_tags = dict(
            (tag.split()[1].replace("refs/tags/", ""), tag.split()[0])
            for tag in remote_tags_output
            if self.MIGRATION_TAG_PREFIX in tag
        )

    def clean_up(self, clean_up_remote=False):
        tags = [
            tag
            for tag in self.local_repo.tags
            if tag.name.startswith(self.MIGRATION_TAG_PREFIX)
        ]

        # delete the local tags
        TagReference.delete(self.local_repo, *tags)

        # delete the remote tags
        if clean_up_remote:
            tags_to_delete = [":" + tag_name for tag_name in self.remote_migration_tags]
            self.remote_repo.push(refspec=tags_to_delete)


parser = OptionParser()
parser.add_option(
    "-l",
    "--local",
    action="store",
    dest="localrepo",
    default=os.getcwd(),
    help="The path to the local repo. If this option is not specified, the script will attempt to use current directory by default. If it is not a local git repo, the script will fail.",
)
parser.add_option(
    "-r",
    "--remote",
    action="store",
    dest="remoterepo",
    default="codecommit",
    help="The name of the remote repository to be used as the push or migration destination. The remote must already be set in the local repo ('git remote add ...'). If this option is not specified, the script will use 'codecommit' by default.",
)
parser.add_option(
    "-b",
    "--batch",
    action="store",
    dest="batchsize",
    default="1000",
    help="Specifies the commit batch size for pushes. If not explicitly set, the default is 1,000 commits.",
)
parser.add_option(
    "-c",
    "--clean",
    action="store_true",
    dest="clean",
    default=False,
    help="Remove the temporary tags created by migration from both the local repo and the remote repository. This option will not do any migration work, just cleanup. Cleanup is done automatically at the end of a successful migration, but not after a failure so that when you re-run the script, the tags from the prior run can be used to identify commit batches that were not pushed successfully.",
)

(options, args) = parser.parse_args()

migration = RepositoryMigration()
migration.migrate_repository_in_parts(
    options.localrepo, options.remoterepo, int(options.batchsize), options.clean
)
```