View a markdown version of this page

CodeCommit examples using AWS CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

CodeCommit examples using AWS CLI

The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with CodeCommit.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use associate-approval-rule-template-with-repository.

AWS CLI

To associate an approval rule template with a repository

The following associate-approval-rule-template-with-repository example associates the specified approval rule template with a repository named MyDemoRepo.

aws codecommit associate-approval-rule-template-with-repository \ --repository-name MyDemoRepo \ --approval-rule-template-name 2-approver-rule-for-main

This command produces no output.

For more information, see Associate an Approval Rule Template with a Repository in the AWS CodeCommit User Guide.

The following code example shows how to use batch-associate-approval-rule-template-with-repositories.

AWS CLI

To associate an approval rule template with multiple repositories in a single operation

The following batch-associate-approval-rule-template-with-repositories example associates the specified approval rule template with repositories named MyDemoRepo and MyOtherDemoRepo.

Note: Approval rule templates are specific to the AWS Region where they are created. They can only be associated with repositories in that AWS Region.

aws codecommit batch-associate-approval-rule-template-with-repositories \ --repository-names MyDemoRepo, MyOtherDemoRepo \ --approval-rule-template-name 2-approver-rule-for-main

Output:

{ "associatedRepositoryNames": [ "MyDemoRepo", "MyOtherDemoRepo" ], "errors": [] }

For more information, see Associate an Approval Rule Template with a Repository in the AWS CodeCommit User Guide.

The following code example shows how to use batch-describe-merge-conflicts.

AWS CLI

To get information about merge conflicts in all files or a subset of files in a merge between two commit specifiers

The following batch-describe-merge-conflicts example determines the merge conflicts for merging a source branch named feature-randomizationfeature with a destination branch named main using the THREE_WAY_MERGE strategy in a repository named MyDemoRepo.

aws codecommit batch-describe-merge-conflicts \ --source-commit-specifier feature-randomizationfeature \ --destination-commit-specifier main \ --merge-option THREE_WAY_MERGE \ --repository-name MyDemoRepo

Output:

{ "conflicts": [ { "conflictMetadata": { "filePath": "readme.md", "fileSizes": { "source": 139, "destination": 230, "base": 85 }, "fileModes": { "source": "NORMAL", "destination": "NORMAL", "base": "NORMAL" }, "objectTypes": { "source": "FILE", "destination": "FILE", "base": "FILE" }, "numberOfConflicts": 1, "isBinaryFile": { "source": false, "destination": false, "base": false }, "contentConflict": true, "fileModeConflict": false, "objectTypeConflict": false, "mergeOperations": { "source": "M", "destination": "M" } }, "mergeHunks": [ { "isConflict": true, "source": { "startLine": 0, "endLine": 3, "hunkContent": "VGhpcyBpEXAMPLE==" }, "destination": { "startLine": 0, "endLine": 1, "hunkContent": "VXNlIHRoEXAMPLE=" } } ] } ], "errors": [], "destinationCommitId": "86958e0aEXAMPLE", "sourceCommitId": "6ccd57fdEXAMPLE", "baseCommitId": "767b6958EXAMPLE" }

For more information, see Resolve Conflicts in a Pull Request in the AWS CodeCommit User Guide.

The following code example shows how to use batch-disassociate-approval-rule-template-from-repositories.

AWS CLI

To disassociate an approval rule template from multiple repositories in a single operation

The following batch-disassociate-approval-rule-template-from-repositories example disassociates the specified approval rule template from repositories named MyDemoRepo and MyOtherDemoRepo.

aws codecommit batch-disassociate-approval-rule-template-from-repositories \ --repository-names MyDemoRepo, MyOtherDemoRepo \ --approval-rule-template-name 1-approval-rule-for-all pull requests

Output:

{ "disassociatedRepositoryNames": [ "MyDemoRepo", "MyOtherDemoRepo" ], "errors": [] }

For more information, see Disassociate an Approval Rule Template in the AWS CodeCommit User Guide.

The following code example shows how to use batch-get-commits.

AWS CLI

To view information about multiple commits

The following batch-get-commits example displays details about the specified commits.

aws codecommit batch-get-commits \ --repository-name MyDemoRepo \ --commit-ids 317f8570EXAMPLE 4c925148EXAMPLE

Output:

{ "commits": [ { "additionalData": "", "committer": { "date": "1508280564 -0800", "name": "Mary Major", "email": "mary_major@example.com" }, "author": { "date": "1508280564 -0800", "name": "Mary Major", "email": "mary_major@example.com" }, "commitId": "317f8570EXAMPLE", "treeId": "1f330709EXAMPLE", "parents": [ "6e147360EXAMPLE" ], "message": "Change variable name and add new response element" }, { "additionalData": "", "committer": { "date": "1508280542 -0800", "name": "Li Juan", "email": "li_juan@example.com" }, "author": { "date": "1508280542 -0800", "name": "Li Juan", "email": "li_juan@example.com" }, "commitId": "4c925148EXAMPLE", "treeId": "1f330709EXAMPLE", "parents": [ "317f8570EXAMPLE" ], "message": "Added new class" } }

For more information, see View Commit Details in the AWS CodeCommit User Guide.

The following code example shows how to use batch-get-repositories.

AWS CLI

To view details about multiple repositories

This example shows details about multiple AWS CodeCommit repositories.

aws codecommit batch-get-repositories \ --repository-names MyDemoRepo MyOtherDemoRepo

Output:

{ "repositoriesNotFound": [], "repositories": [ { "creationDate": 1429203623.625, "defaultBranch": "main", "repositoryName": "MyDemoRepo", "cloneUrlSsh": "ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo", "lastModifiedDate": 1430783812.0869999, "repositoryDescription": "My demonstration repository", "cloneUrlHttp": "https://codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo", "repositoryId": "f7579e13-b83e-4027-aaef-650c0EXAMPLE", "Arn": "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo" "accountId": "111111111111" }, { "creationDate": 1429203623.627, "defaultBranch": "main", "repositoryName": "MyOtherDemoRepo", "cloneUrlSsh": "ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyOtherDemoRepo", "lastModifiedDate": 1430783812.0889999, "repositoryDescription": "My other demonstration repository", "cloneUrlHttp": "https://codecommit.us-east-2.amazonaws.com/v1/repos/MyOtherDemoRepo", "repositoryId": "cfc29ac4-b0cb-44dc-9990-f6f51EXAMPLE", "Arn": "arn:aws:codecommit:us-east-2:111111111111:MyOtherDemoRepo" "accountId": "111111111111" } ], "repositoriesNotFound": [] }

The following code example shows how to use create-approval-rule-template.

AWS CLI

To create an approval rule template

The following create-approval-rule-template example creates an approval rule template named 2-approver-rule-for-main ``. The template requires two users who assume the role of ``CodeCommitReview to approve any pull request before it can be merged to the main branch.

aws codecommit create-approval-rule-template \ --approval-rule-template-name 2-approver-rule-for-main \ --approval-rule-template-description "Requires two developers from the team to approve the pull request if the destination branch is main" \ --approval-rule-template-content "{\"Version\": \"2018-11-08\",\"DestinationReferences\": [\"refs/heads/main\"],\"Statements\": [{\"Type\": \"Approvers\",\"NumberOfApprovalsNeeded\": 2,\"ApprovalPoolMembers\": [\"arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*\"]}]}"

Output:

{ "approvalRuleTemplate": { "approvalRuleTemplateName": "2-approver-rule-for-main", "creationDate": 1571356106.936, "approvalRuleTemplateId": "dd8b17fe-EXAMPLE", "approvalRuleTemplateContent": "{\"Version\": \"2018-11-08\",\"DestinationReferences\": [\"refs/heads/main\"],\"Statements\": [{\"Type\": \"Approvers\",\"NumberOfApprovalsNeeded\": 2,\"ApprovalPoolMembers\": [\"arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*\"]}]}", "lastModifiedUser": "arn:aws:iam::123456789012:user/Mary_Major", "approvalRuleTemplateDescription": "Requires two developers from the team to approve the pull request if the destination branch is main", "lastModifiedDate": 1571356106.936, "ruleContentSha256": "4711b576EXAMPLE" } }

For more information, see Create an Approval Rule Template in the AWS CodeCommit User Guide.

The following code example shows how to use create-branch.

AWS CLI

To create a branch

This example creates a branch in an AWS CodeCommit repository. This command produces output only if there are errors.

Command:

aws codecommit create-branch --repository-name MyDemoRepo --branch-name MyNewBranch --commit-id 317f8570EXAMPLE

Output:

None.
  • For API details, see CreateBranch in AWS CLI Command Reference.

The following code example shows how to use create-commit.

AWS CLI

To create a commit

The following create-commit example demonstrates how to create an initial commit for a repository that adds a readme.md file to a repository named MyDemoRepo in the main branch.

aws codecommit create-commit \ --repository-name MyDemoRepo \ --branch-name main \ --put-files "filePath=readme.md,fileContent='Welcome to our team repository.'"

Output:

{ "filesAdded": [ { "blobId": "5e1c309d-EXAMPLE", "absolutePath": "readme.md", "fileMode": "NORMAL" } ], "commitId": "4df8b524-EXAMPLE", "treeId": "55b57003-EXAMPLE", "filesDeleted": [], "filesUpdated": [] }

For more information, see Create a Commit in AWS CodeCommit in the AWS CodeCommit User Guide.

  • For API details, see CreateCommit in AWS CLI Command Reference.

The following code example shows how to use create-pull-request-approval-rule.

AWS CLI

To create an approval rule for a pull request

The following create-pull-request-approval-rule example creates an approval rule named Require two approved approvers for the specified pull request. The rule specifies that two approvals are required from an approval pool. The pool includes all users who access CodeCommit by assuming the role of CodeCommitReview in the 123456789012 AWS account. It also includes either an IAM user or federated user named Nikhil_Jayashankar from the same AWS account.

aws codecommit create-pull-request-approval-rule \ --pull-request-id 42 --approval-rule-name "Require two approved approvers" \ --approval-rule-content "{\"Version\": \"2018-11-08\",\"Statements\": [{\"Type\": \"Approvers\",\"NumberOfApprovalsNeeded\": 2,\"ApprovalPoolMembers\": [\"CodeCommitApprovers:123456789012:Nikhil_Jayashankar\", \"arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*\"]}]}"

Output:

{ "approvalRule": { "approvalRuleName": "Require two approved approvers", "lastModifiedDate": 1570752871.932, "ruleContentSha256": "7c44e6ebEXAMPLE", "creationDate": 1570752871.932, "approvalRuleId": "aac33506-EXAMPLE", "approvalRuleContent": "{\"Version\": \"2018-11-08\",\"Statements\": [{\"Type\": \"Approvers\",\"NumberOfApprovalsNeeded\": 2,\"ApprovalPoolMembers\": [\"CodeCommitApprovers:123456789012:Nikhil_Jayashankar\", \"arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*\"]}]}", "lastModifiedUser": "arn:aws:iam::123456789012:user/Mary_Major" } }

For more information, see Create an Approval Rule in the AWS CodeCommit User Guide.

The following code example shows how to use create-pull-request.

AWS CLI

To create a pull request

The following create-pull-request example creates a pull request named 'Pronunciation difficulty analyzer' with a description of 'Please review these changes by Tuesday' that targets the 'jane-branch' source branch and is to be merged to the default branch 'main' in an AWS CodeCommit repository named 'MyDemoRepo'.

aws codecommit create-pull-request \ --title "My Pull Request" \ --description "Please review these changes by Tuesday" \ --client-request-token 123Example \ --targets repositoryName=MyDemoRepo,sourceReference=MyNewBranch

Output:

{ "pullRequest": { "approvalRules": [ { "approvalRuleContent": "{\"Version\": \"2018-11-08\",\"DestinationReferences\": [\"refs/heads/main\"],\"Statements\": [{\"Type\": \"Approvers\",\"NumberOfApprovalsNeeded\": 2,\"ApprovalPoolMembers\": [\"arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*\"]}]}", "approvalRuleId": "dd8b17fe-EXAMPLE", "approvalRuleName": "2-approver-rule-for-main", "creationDate": 1571356106.936, "lastModifiedDate": 571356106.936, "lastModifiedUser": "arn:aws:iam::123456789012:user/Mary_Major", "originApprovalRuleTemplate": { "approvalRuleTemplateId": "dd3d22fe-EXAMPLE", "approvalRuleTemplateName": "2-approver-rule-for-main" }, "ruleContentSha256": "4711b576EXAMPLE" } ], "authorArn": "arn:aws:iam::111111111111:user/Jane_Doe", "description": "Please review these changes by Tuesday", "title": "Pronunciation difficulty analyzer", "pullRequestTargets": [ { "destinationCommit": "5d036259EXAMPLE", "destinationReference": "refs/heads/main", "repositoryName": "MyDemoRepo", "sourceCommit": "317f8570EXAMPLE", "sourceReference": "refs/heads/jane-branch", "mergeMetadata": { "isMerged": false } } ], "lastActivityDate": 1508962823.285, "pullRequestId": "42", "clientRequestToken": "123Example", "pullRequestStatus": "OPEN", "creationDate": 1508962823.285 } }

The following code example shows how to use create-repository.

AWS CLI

To create a repository

This example creates a repository and associates it with the user's AWS account.

Command:

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

Output:

{ "repositoryMetadata": { "repositoryName": "MyDemoRepo", "cloneUrlSsh": "ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo", "lastModifiedDate": 1444766838.027, "repositoryDescription": "My demonstration repository", "cloneUrlHttp": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo", "repositoryId": "f7579e13-b83e-4027-aaef-650c0EXAMPLE", "Arn": "arn:aws:codecommit:us-east-1:111111111111EXAMPLE:MyDemoRepo", "accountId": "111111111111" } }

The following code example shows how to use create-unreferenced-merge-commit.

AWS CLI

To create an unreferenced commit that represents the result of merging two commit specifiers

The following create-unreferenced-merge-commit example creates a commit that represents the results of a merge between a source branch named bugfix-1234 with a destination branch named main using the THREE_WAY_MERGE strategy in a repository named MyDemoRepo.

aws codecommit create-unreferenced-merge-commit \ --source-commit-specifier bugfix-1234 \ --destination-commit-specifier main \ --merge-option THREE_WAY_MERGE \ --repository-name MyDemoRepo \ --name "Maria Garcia" \ --email "maria_garcia@example.com" \ --commit-message "Testing the results of this merge."

Output:

{ "commitId": "4f178133EXAMPLE", "treeId": "389765daEXAMPLE" }

For more information, see Resolve Conflicts in a Pull Request in the AWS CodeCommit User Guide.

The following code example shows how to use credential-helper.

AWS CLI

To set up the credential helper included in the AWS CLI with AWS CodeCommit

The credential-helper utility is not designed to be called directly from the AWS CLI. Instead it is intended to be used as a parameter with the git config command to set up your local computer. It enables Git to use HTTPS and a cryptographically signed version of your IAM user credentials or Amazon EC2 instance role whenever Git needs to authenticate with AWS to interact with CodeCommit repositories.

git config --global credential.helper '!aws codecommit credential-helper $@' git config --global credential.UseHttpPath true

Output:

[credential] helper = !aws codecommit credential-helper $@ UseHttpPath = true

For more information, see Setting up for AWS CodeCommit Using Other Methods in the AWS CodeCommit User Guide. Review the content carefully, and then follow the procedures in one of the following topics: For HTTPS Connections on Linux, macOS, or Unix or For HTTPS Connections on Windows in the AWS CodeCommit User Guide.

The following code example shows how to use delete-approval-rule-template.

AWS CLI

To delete an approval rule template

The following delete-approval-rule-template example deletes the specified approval rule template.

aws codecommit delete-approval-rule-template \ --approval-rule-template-name 1-approver-for-all-pull-requests

Output:

{ "approvalRuleTemplateId": "41de97b7-EXAMPLE" }

For more information, see Delete an Approval Rule Template in the AWS CodeCommit User Guide.

The following code example shows how to use delete-branch.

AWS CLI

To delete a branch

This example shows how to delete a branch in an AWS CodeCommit repository.

Command:

aws codecommit delete-branch --repository-name MyDemoRepo --branch-name MyNewBranch

Output:

{ "branch": { "commitId": "317f8570EXAMPLE", "branchName": "MyNewBranch" } }
  • For API details, see DeleteBranch in AWS CLI Command Reference.

The following code example shows how to use delete-comment-content.

AWS CLI

To delete the content of a comment

You can only delete the content of a comment if you created the comment. This example demonstrates how to delete the content of a comment with the system-generated ID of ff30b348EXAMPLEb9aa670f.

aws codecommit delete-comment-content \ --comment-id ff30b348EXAMPLEb9aa670f

Output:

{ "comment": { "creationDate": 1508369768.142, "deleted": true, "lastModifiedDate": 1508369842.278, "clientRequestToken": "123Example", "commentId": "ff30b348EXAMPLEb9aa670f", "authorArn": "arn:aws:iam::111111111111:user/Li_Juan", "callerReactions": [], "reactionCounts": { "CLAP" : 1 } } }

The following code example shows how to use delete-file.

AWS CLI

To delete a file

The following delete-file example demonstrates how to delete a file named README.md from a branch named main with a most recent commit ID of c5709475EXAMPLE in a repository named MyDemoRepo.

aws codecommit delete-file \ --repository-name MyDemoRepo \ --branch-name main \ --file-path README.md \ --parent-commit-id c5709475EXAMPLE

Output:

{ "blobId":"559b44fEXAMPLE", "commitId":"353cf655EXAMPLE", "filePath":"README.md", "treeId":"6bc824cEXAMPLE" }

For more information, see Edit or Delete a File in AWS CodeCommit in the AWS CodeCommit API Reference guide.

  • For API details, see DeleteFile in AWS CLI Command Reference.

The following code example shows how to use delete-pull-request-approval-rule.

AWS CLI

To delete an approval rule for a pull request

The following delete-pull-request-approval-rule example deletes the approval rule named My Approval Rule for the specified pull request.

aws codecommit delete-pull-request-approval-rule \ --approval-rule-name "My Approval Rule" \ --pull-request-id 15

Output:

{ "approvalRuleId": "077d8e8a8-EXAMPLE" }

For more information, see Edit or Delete an Approval Rule in the AWS CodeCommit User Guide.

The following code example shows how to use delete-repository.

AWS CLI

To delete a repository

This example shows how to delete an AWS CodeCommit repository.

Command:

aws codecommit delete-repository --repository-name MyDemoRepo

Output:

{ "repositoryId": "f7579e13-b83e-4027-aaef-650c0EXAMPLE" }

The following code example shows how to use describe-merge-conflicts.

AWS CLI

To get detailed information about merge conflicts

The following describe-merge-conflicts example determines the merge conflicts for a file named readme.md in the specified source branch and destination branch using the THREE_WAY_MERGE strategy.

aws codecommit describe-merge-conflicts \ --source-commit-specifier feature-randomizationfeature \ --destination-commit-specifier main \ --merge-option THREE_WAY_MERGE \ --file-path readme.md \ --repository-name MyDemoRepo

Output:

{ "conflictMetadata": { "filePath": "readme.md", "fileSizes": { "source": 139, "destination": 230, "base": 85 }, "fileModes": { "source": "NORMAL", "destination": "NORMAL", "base": "NORMAL" }, "objectTypes": { "source": "FILE", "destination": "FILE", "base": "FILE" }, "numberOfConflicts": 1, "isBinaryFile": { "source": false, "destination": false, "base": false }, "contentConflict": true, "fileModeConflict": false, "objectTypeConflict": false, "mergeOperations": { "source": "M", "destination": "M" } }, "mergeHunks": [ { "isConflict": true, "source": { "startLine": 0, "endLine": 3, "hunkContent": "VGhpcyBpEXAMPLE=" }, "destination": { "startLine": 0, "endLine": 1, "hunkContent": "VXNlIHRoEXAMPLE=" } } ], "destinationCommitId": "86958e0aEXAMPLE", "sourceCommitId": "6ccd57fdEXAMPLE", "baseCommitId": "767b69580EXAMPLE" }

For more information, see Resolve Conflicts in a Pull Request in the AWS CodeCommit User Guide.

The following code example shows how to use describe-pull-request-events.

AWS CLI

To view events in a pull request

The following describe-pull-request-events example retrieves the events for a pull request with the ID of '8'.

aws codecommit describe-pull-request-events --pull-request-id 8

Output:

{ "pullRequestEvents": [ { "pullRequestId": "8", "pullRequestEventType": "PULL_REQUEST_CREATED", "eventDate": 1510341779.53, "actor": "arn:aws:iam::111111111111:user/Zhang_Wei" }, { "pullRequestStatusChangedEventMetadata": { "pullRequestStatus": "CLOSED" }, "pullRequestId": "8", "pullRequestEventType": "PULL_REQUEST_STATUS_CHANGED", "eventDate": 1510341930.72, "actor": "arn:aws:iam::111111111111:user/Jane_Doe" } ] }

The following code example shows how to use disassociate-approval-rule-template-from-repository.

AWS CLI

To disassociate an approval rule template from a repository

The following disassociate-approval-rule-template-from-repository example disassociates the specified approval rule template from a repository named MyDemoRepo.

aws codecommit disassociate-approval-rule-template-from-repository \ --repository-name MyDemoRepo \ --approval-rule-template-name 1-approver-rule-for-all-pull-requests

This command produces no output.

For more information, see Disassociate an Approval Rule Template in the AWS CodeCommit User Guide.

The following code example shows how to use evaluate-pull-request-approval-rules.

AWS CLI

To evaluate whether a pull request has all of its approval rules satisfied

The following evaluate-pull-request-approval-rules example evaluates the state of approval rules on the specified pull request. In this example, an approval rule has not been satisfied for the pull request, so the output of the command shows an approved value of false.

aws codecommit evaluate-pull-request-approval-rules \ --pull-request-id 27 \ --revision-id 9f29d167EXAMPLE

Output:

{ "evaluation": { "approved": false, "approvalRulesNotSatisfied": [ "Require two approved approvers" ], "overridden": false, "approvalRulesSatisfied": [] } }

For more information, see Merge a Pull Request in the AWS CodeCommit User Guide.

The following code example shows how to use get-approval-rule-template.

AWS CLI

To get the content of an approval rule template

The following get-approval-rule-template example gets the content of an approval rule template named 1-approver-rule-for-all-pull-requests.

aws codecommit get-approval-rule-template \ --approval-rule-template-name 1-approver-rule-for-all-pull-requests

Output:

{ "approvalRuleTemplate": { "approvalRuleTemplateContent": "{\"Version\": \"2018-11-08\",\"Statements\": [{\"Type\": \"Approvers\",\"NumberOfApprovalsNeeded\": 1,\"ApprovalPoolMembers\": [\"arn:aws:sts::123456789012:assumed-role/CodeCommitReview/*\"]}]}", "ruleContentSha256": "621181bbEXAMPLE", "lastModifiedDate": 1571356106.936, "creationDate": 1571356106.936, "approvalRuleTemplateName": "1-approver-rule-for-all-pull-requests", "lastModifiedUser": "arn:aws:iam::123456789012:user/Li_Juan", "approvalRuleTemplateId": "a29abb15-EXAMPLE", "approvalRuleTemplateDescription": "All pull requests must be approved by one developer on the team." } }

For more information, see Manage Approval Rule Templates in the