

# Working with branches in AWS CodeCommit repositories
<a name="branches"></a>

What is a branch? In Git, branches are pointers or references to a commit. In development, they're a convenient way to organize your work. You can use branches to separate work on a new or different version of files without affecting work in other branches. You can use branches to develop new features, store a specific version of your project from a particular commit, and more. When you create your first commit, a *default branch* is created for you. This default branch is the one used as the base or default branch in local repositories (repos) when users clone the repository. The name of that default branch varies depending on how you create your first commit. If you add the first file to your repository by using the CodeCommit console, the AWS CLI, or one of the SDKs, the name of that default branch is *main*. This is the default branch name used in the examples in this guide. If you push your first commit using a Git client, the name of the default branch is what the Git client specifies as its default. Consider configuring your Git client to use *main* as the name for the initial branch.

In CodeCommit, you can change the default branch for your repository. You can also create and delete branches and view details about a branch. You can quickly compare differences between a branch and the default branch (or any two branches). To view the history of branches and merges in your repository, you can use the [Commit visualizer](how-to-view-commit-details.md#how-to-view-commit-details-console-visualizer), which is shown in the following graphic.

![\[A view of branches in a repository\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-cv-complex1.png)


For information about working with other aspects of your repository in CodeCommit, see [Working with repositories](repositories.md), [Working with files](files.md), [Working with pull requests](pull-requests.md), [Working with commits](commits.md), and [Working with user preferences](user-preferences.md). 

**Topics**
+ [

# Create a branch in AWS CodeCommit
](how-to-create-branch.md)
+ [

# Limit pushes and merges to branches in AWS CodeCommit
](how-to-conditional-branch.md)
+ [

# View branch details in AWS CodeCommit
](how-to-view-branch-details.md)
+ [

# Compare and merge branches in AWS CodeCommit
](how-to-compare-branches.md)
+ [

# Change branch settings in AWS CodeCommit
](how-to-change-branch.md)
+ [

# Delete a branch in AWS CodeCommit
](how-to-delete-branch.md)

# Create a branch in AWS CodeCommit
<a name="how-to-create-branch"></a>

You can use the CodeCommit console or the AWS CLI to create branches for your repository. This is a quick way to separate work on a new or different version of files without impacting work in the default branch. After you create a branch in the CodeCommit console, you must pull that change to your local repo. Alternatively, you can create a branch locally and then use Git from a local repo connected to the CodeCommit repository to push that change. 

**Topics**
+ [

## Create a branch (console)
](#how-to-create-branch-console)
+ [

## Create a branch (Git)
](#how-to-create-branch-git)
+ [

## Create a branch (AWS CLI)
](#how-to-create-branch-cli)

## Create a branch (console)
<a name="how-to-create-branch-console"></a>

You can use the CodeCommit console to create a branch in a CodeCommit repository. The next time users pull changes from the repository, they see the new branch.

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 where you want to create a branch. 

1. In the navigation pane, choose **Branches**.

1. Choose **Create branch**.   
![\[Creating a branch in the CodeCommit console.\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-branches-create.png)

   In **Branch name**, enter a name for the branch. In **Branch from**, choose a branch or tag from the list, or paste a commit ID. Choose **Create branch**.

## Create a branch (Git)
<a name="how-to-create-branch-git"></a>

Follow these steps to use Git from a local repo to create a branch in a local repo and then push that branch to the CodeCommit repository.

These steps are written with the assumption that you have already connected the local repo to the CodeCommit repository. For instructions, see [Connect to a repository](how-to-connect.md).

1. Create a branch in your local repo by running the **git checkout -b *new-branch-name*** command, where *new-branch-name* is the name of the new branch.

   For example, the following command creates a branch named `MyNewBranch` in the local repo:

   ```
   git checkout -b MyNewBranch
   ```

1. To push the new branch from the local repo to the CodeCommit repository, run the **git push** command, specifying both the ***remote-name*** and the ***new-branch-name***. 

   For example, to push a new branch in the local repo named `MyNewBranch` to the CodeCommit repository with the nickname `origin`:

   ```
   git push origin MyNewBranch
   ```

**Note**  
If you add the `-u` option to **git push** (for example, **git push -u origin main**), then in the future you can run **git push** without *remote-name* *branch-name*. Upstream tracking information is set. To get upstream tracking information, run **git remote show *remote-name*** (for example, **git remote show origin**).  
To see a list of all of your local and remote tracking branches, run **git branch --all**.  
To set up a branch in the local repo that is connected to a branch in the CodeCommit repository, run **git checkout *remote-branch-name***.

For more options, see your Git documentation.

## Create a branch (AWS CLI)
<a name="how-to-create-branch-cli"></a>

To use AWS CLI commands with CodeCommit, install the AWS CLI. For more information, see [Command line reference](cmd-ref.md). 

Follow these steps to use the AWS CLI to create a branch in a CodeCommit repository and then push that branch to the CodeCommit repository. For steps to create an initial commit and specify the name of the default branch for an empty repository, see [Create the first commit for a repository using the AWS CLI](how-to-create-commit.md#create-first-commit).

1. Run the **create-branch** command, specifying:
   + The name of the CodeCommit repository where the branch is created (with the **--repository-name** option).
**Note**  
To get the name of the CodeCommit repository, run the [list-repositories](how-to-view-repository-details.md#how-to-view-repository-details-no-name-cli) command.
   + The name of the new branch (with the **--branch-name** option).
   + The ID of the commit to which the new branch points (with the **--commit-id** option).

   For example, to create a branch named `MyNewBranch` that points to commit ID `317f8570EXAMPLE` in a CodeCommit repository named `MyDemoRepo`:

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

   This command produces output only if there are errors.

1. To update the list of available CodeCommit repository branches in your local repo with the new remote branch name, run **git remote update *remote-name***.

   For example, to update the list of available branches for the CodeCommit repository with the nickname `origin`:

   ```
   git remote update origin 
   ```
**Note**  
Alternatively, you can run the **git fetch** command. You can also view all remote branches by running **git branch --all**, but until you update the list of your local repo, the remote branch you created does not appear in the list.   
For more options, see your Git documentation.

1. To set up a branch in the local repo that is connected to the new branch in the CodeCommit repository, run **git checkout *remote-branch-name***.

**Note**  
 To get a list of CodeCommit repository names and their URLs, run the **git remote -v** command.

# Limit pushes and merges to branches in AWS CodeCommit
<a name="how-to-conditional-branch"></a>

By default, any CodeCommit repository user who has sufficient permissions to push code to the repository can contribute to any branch in that repository. This is true no matter how you add a branch to the repository: by using the console, the command line, or Git. However, you might want to configure a branch so that only some repository users can push or merge code to that branch. For example, you might want to configure a branch used for production code so that only a subset of senior developers can push or merge changes to that branch. Other developers can still pull from the branch, make their own branches, and create pull requests, but they cannot push or merge changes to that branch. You can configure this access by creating a conditional policy that uses a context key for one or more branches in IAM. 

**Note**  
To complete some of the procedures in this topic, you must sign in with an adminstrative user that has sufficient permissions to configure and apply IAM policies. For more information, see [Creating an IAM Admin User and Group](https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started_create-admin-group.html). 

**Topics**
+ [

## Configure an IAM policy to limit pushes and merges to a branch
](#how-to-conditional-branch-create-policy)
+ [

## Apply the IAM policy to an IAM group or role
](#how-to-conditional-branch-apply-policy)
+ [

## Test the policy
](#how-to-conditional-branch-test)

## Configure an IAM policy to limit pushes and merges to a branch
<a name="how-to-conditional-branch-create-policy"></a>

You can create a policy in IAM that prevents users from updating a branch, including pushing commits to a branch and merging pull requests to a branch. To do this, your policy uses a conditional statement, so that the effect of the `Deny` statement applies only if the condition is met. The APIs you include in the `Deny` statement determine which actions are not allowed. You can configure this policy to apply to only one branch in a repository, a number of branches in a repository, or to all branches that match the criteria across all repositories in an Amazon Web Services account. <a name="how-to-conditional-branch-create-policy-procedure"></a>

**To create a conditional policy for branches**

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 navigation pane, choose **Policies**. 

1. Choose **Create policy**.

1. Choose **JSON**, and then paste the following example policy. Replace the value of `Resource` with the ARN of the repository that contains the branch for which you want to restrict access. Replace the value of `codecommit:References` with a reference to the branch or branches to which you want to restrict access. For example, this policy denies pushing commits, merging branches, deleting branches, deleting files, merging pull requests, and adding files to a branch named *`main`* and a branch named `prod` in a repository named `MyDemoRepo`:

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Deny",
               "Action": [
                   "codecommit:GitPush",
                   "codecommit:DeleteBranch",
                   "codecommit:DeleteFile",
                   "codecommit:PutFile",
                   "codecommit:MergeBranchesByFastForward",
                   "codecommit:MergeBranchesBySquash",
                   "codecommit:MergeBranchesByThreeWay",
                   "codecommit:MergePullRequestByFastForward",
                   "codecommit:MergePullRequestBySquash",
                   "codecommit:MergePullRequestByThreeWay"
               ],
               "Resource": "arn:aws:codecommit:us-east-2:111111111111:MyDemoRepo",
               "Condition": {
                   "StringEqualsIfExists": {
                       "codecommit:References": [
                           "refs/heads/main", 
                           "refs/heads/prod"
                        ]
                   },
                   "Null": {
                       "codecommit:References": "false"
                   }
               }
           }
       ]
   }
   ```

------

   Branches in Git are simply pointers (references) to the SHA-1 value of the head commit, which is why the condition uses `References`. The `Null` statement is required in any policy whose effect is `Deny` and where `GitPush` is one of the actions. This is required because of the way Git and `git-receive-pack` work when pushing changes from a local repo to CodeCommit.
**Tip**  
To create a policy that applies to all branches named main in all repositories in an Amazon Web Services account, change the value of `Resource` from a repository ARN to an asterisk (`*`). 

1. Choose **Review policy**. Correct any errors in your policy statement, and then continue to **Create policy**.

1. When the JSON is validated, the **Create policy** page is displayed. A warning appears in the **Summary** section, advising you that this policy does not grant permissions. This is expected. 
   + In **Name**, enter a name for this policy, such as **DenyChangesToMain**.
   + In **Description**, enter a description of the policy's purpose. This is optional, but recommended.
   + Choose **Create policy**.

## Apply the IAM policy to an IAM group or role
<a name="how-to-conditional-branch-apply-policy"></a>

You've created a policy that limits pushes and merges to a branch, but the policy has no effect until you apply it to an IAM user, group, or role. As a best practice, consider applying the policy to an IAM group or role. Applying policies to individual IAM users does not scale well.<a name="how-to-conditional-branch-apply-policy-procedure"></a>

**To apply the conditional policy to a group or role**

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 navigation pane, if you want to apply the policy to an IAM group, choose **Groups**.If you want to apply the policy to a role that users assume, choose **Role**. Choose the name of the group or role.

1. On the **Permissions** tab, choose **Attach Policy**.

1. Select the conditional policy you created from the list of policies, and then choose **Attach policy**.

For more information, see [Attaching and Detatching IAM Policies](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html).

## Test the policy
<a name="how-to-conditional-branch-test"></a>

You should test the effects of the policy you've applied on the group or role to ensure that it acts as expected. There are many ways you can do this. For example, to test a policy similar to the one shown above, you can:
+ Sign in to the CodeCommit console with an IAM user who is either a member of an IAM group that has the policy applied, or assumes a role that has the policy applied. In the console, add a file on the branch where the restrictions apply. You should see an error message when you attempt to save or upload a file to that branch. Add a file to a different branch. The operation should succeed.
+ Sign in to the CodeCommit console with an IAM user who is either a member of an IAM group that has the policy applied, or assumes a role that has the policy applied. Create a pull request that merges to the branch where the restrictions apply. You should be able to create the pull request, but get an error if you try to merge it. 
+ From the terminal or command line, create a commit on the branch where the restrictions apply, and then push that commit to the CodeCommit repository. You should see an error message. Commits and pushes made from other branches should work as usual.

# View branch details in AWS CodeCommit
<a name="how-to-view-branch-details"></a>

You can use the CodeCommit console to view details about the branches in a CodeCommit repository. You can view the date of the last commit to a branch, the commit message, and more. You can also use the AWS CLI or Git from a local repo connected to the CodeCommit repository.

**Topics**
+ [

## View branch details (console)
](#how-to-view-branch-details-console)
+ [

## View branch details (Git)
](#how-to-view-branch-details-git)
+ [

## View branch details (AWS CLI)
](#how-to-view-branch-details-cli)

## View branch details (console)
<a name="how-to-view-branch-details-console"></a>

Use the CodeCommit console to quickly view a list of branches for your repository and details about the branches.

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 where you want to view branch details. 

1. In the navigation pane, choose **Branches**.  
![\[A view of branches in a repository.\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-branches.png)

1. The name of the branch used as the default for the repository is displayed next to **Default branch**. To view details about the most recent commit to a branch, choose the branch, and then choose **View last commit**. To view the files and code in a branch, choose the branch name. 

## View branch details (Git)
<a name="how-to-view-branch-details-git"></a>

To use Git from a local repo to view details about both the local and remote tracking branches for a CodeCommit repository, run the **git branch** command.

The following steps are written with the assumption that you have already connected the local repo to the CodeCommit repository. For instructions, see [Connect to a repository](how-to-connect.md).

1. Run the **git branch** command, specifying the **--all** option:

   ```
   git branch --all
   ```

1. If successful, this command returns output similar to the following:

   ```
     MyNewBranch
   * main
     remotes/origin/MyNewBranch
     remotes/origin/main
   ```

   The asterisk (`*`) appears next to the currently open branch. The entries after that are remote tracking references.
**Tip**  
**git branch** shows local branches.  
**git branch -r** shows remote branches.  
**git checkout *existing-branch-name*** switches to the specified branch name and, if **git branch** is run immediately afterward, displays it with an asterisk (`*`).  
**git remote update *remote-name*** updates your local repo with the list of available CodeCommit repository branches. (To get a list of CodeCommit repository names and their URLs, run the **git remote -v** command.)

For more options, see your Git documentation.

## View branch details (AWS CLI)
<a name="how-to-view-branch-details-cli"></a>

To use AWS CLI commands with CodeCommit, install the AWS CLI. For more information, see [Command line reference](cmd-ref.md). 

To use the AWS CLI to view details about the branches in a CodeCommit repository, run one or more of the following commands:
+ To view a list of branch names, run [list-branches](#how-to-view-branch-details-cli).
+ To view information about a specific branch, run [get-branch](#how-to-view-branch-details-cli-details).

### To view a list of branch names
<a name="how-to-view-branch-details-cli-list"></a>

1. Run the **list-branches** command, specifying the name of the CodeCommit repository (with the `--repository-name` option).
**Tip**  
To get the name of the CodeCommit repository, run the [list-repositories](how-to-view-repository-details.md#how-to-view-repository-details-no-name-cli) command.

   For example, to view details about the branches in a CodeCommit repository named `MyDemoRepo`:

   ```
   aws codecommit list-branches --repository-name MyDemoRepo
   ```

1. If successful, this command outputs a `branchNameList` object, with an entry for each branch.

   Here is some example output based on the preceding example command:

   ```
   {
       "branches": [
           "MyNewBranch",
           "main"
       ]
   }
   ```

### To view information about a branch
<a name="how-to-view-branch-details-cli-details"></a>

1. Run the **get-branch** command, specifying:
   + The repository name (with the **--repository-name** option).
   + The branch name (with the **--branch-name** option).

   For example, to view information about a branch named `MyNewBranch` in a CodeCommit repository named `MyDemoRepo`:

   ```
   aws codecommit get-branch --repository-name MyDemoRepo --branch-name MyNewBranch
   ```

1. If successful, this command outputs the name of the branch and the ID of the last commit made to the branch.

   Here is some example output based on the preceding example command:

   ```
   {
       "branch": {
             "branchName": "MyNewBranch",
             "commitID": "317f8570EXAMPLE"
       }
   }
   ```

# Compare and merge branches in AWS CodeCommit
<a name="how-to-compare-branches"></a>

You can use the CodeCommit console to compare branches in a CodeCommit repository. Comparing branches helps you quickly view the differences between a branch and the default branch, or view the differences between any two branches.

**Topics**
+ [

## Compare a branch to the default branch
](#how-to-compare-branches-default)
+ [

## Compare two specific branches
](#how-to-compare-branches-two)
+ [

## Merge two branches (AWS CLI)
](#how-to-merge-branches-cli)

## Compare a branch to the default branch
<a name="how-to-compare-branches-default"></a>

Use the CodeCommit console to quickly view the differences between a branch and the default branch for your 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 where you want to compare branches. 

1. In the navigation pane, choose **Commits**, and then choose the **Compare commits** tab.

1. In **Destination**, choose the name of the default branch. In **Source**, choose the branch you want to compare to the default branch. Choose **Compare**.

## Compare two specific branches
<a name="how-to-compare-branches-two"></a>

Use the CodeCommit console to view the differences between two branches that you want to compare.

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 where you want to compare branches. 

1. In the navigation pane, choose **Commits**, and then choose the **Compare commits** tab.

1. In **Destination** and **Source**, choose the two branches to compare, and then choose **Compare**. To view the list of changed files, expand the changed files list. You can view changes in files side by side (Split view) or inline (Unified view).
**Note**  
If you are signed in as an IAM user, you can configure and save your preferences for viewing code and other console settings. For more information, see [Working with user preferences](user-preferences.md).  
![\[An abbreviated view of the differences between two branches.\]](http://docs.aws.amazon.com/codecommit/latest/userguide/images/codecommit-compare-branches.png)

## Merge two branches (AWS CLI)
<a name="how-to-merge-branches-cli"></a>

You can merge two branches in a CodeCommit repository using the AWS CLI using one of the available merge strategies by running one of the following commands:
+ To merge two branches using the fast-forward merge strategy, run the [**merge-branches-by-fast-forward**](#merge-branches-by-fast-forward) command.
+ To merge two branches using the squash merge strategy, run the [**merge-branches-by-squash**](#merge-branches-by-squash) command.
+ To merge two branches using the three-way merge strategy, run the [**merge-branches-by-three-way**](#merge-branches-by-three-way) command.

You can also test merges by running the **create-unreferenced-merge-commit** command. For more information, see [Resolve Conflicts in a Pull Request](how-to-resolve-conflict-pull-request.md#create-unreferenced-merge-commit).

**Note**  
To use AWS CLI commands with CodeCommit, install the AWS CLI. For more information, see [Command line reference](cmd-ref.md). 

**To use the AWS CLI to merge two branches in a CodeCommit repository**

1. <a name="merge-branches-by-fast-forward"></a>To merge two branches using the fast-forward merge strategy, run the **merge-branches-by-fast-forward** command, specifying: 
   + The name of the source branch that contains the changes you want to merge (with the **--source-commit-specifier** option). 
   + The name of the destination branch where you want to merge your changes (with the **--destination-commit-specifier** option). 
   + The name of the repository (with the **--repository-name** option).

    For example, to merge a source branch named *bugfix-1234* into a destination branch named *preprod* in a repository named *MyDemoRepo*:

   ```
   aws codecommit merge-branches-by-fast-forward --source-commit-specifier bugfix-bug1234 --destination-commit-specifier preprod --repository-name MyDemoRepo
   ```

   If successful, this command produces output similar to the following:

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

1. <a name="merge-branches-by-squash"></a>To merge two branches using the squash merge strategy, run the **merge-branches-by-squash** command, specifying:
   + The name of the source branch that contains the changes you want to merge (with the **--source-commit-specifier** option). 
   + The name of the destination branch where you want to merge your changes (with the **--destination-commit-specifier** option). 
   + The name of the repository (with the **--repository-name** option).
   + The commit message to include (with the **--commit-message** option).
   + The name to use for the commit (with the **--name** option).
   + The email address to use for the commit (with the **--email** option).

   For example, to merge a source branch named *bugfix-bug1234* with a destination branch named *bugfix-quarterly* in a repository named *MyDemoRepo*:

   ```
   aws codecommit merge-branches-by-squash --source-commit-specifier bugfix-bug1234 --destination-commit-specifier bugfix-quarterly --author-name "Maria Garcia" --email "maria_garcia@example.com" --commit-message "Merging in fix branches to prepare for a general patch." --repository-name MyDemoRepo
   ```

   If successful, this command produces output similar to the following:

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

1. <a name="merge-branches-by-three-way"></a>To merge two branches using the three-way merge strategy, run the **merge-branches-by-three-way** command, specifying:
   + The name of the source branch that contains the changes you want to merge (with the **--source-commit-specifier** option). 
   + The name of the destination branch where you want to merge your changes (with the **--destination-commit-specifier** option). 
   + The name of the repository (with the **--repository-name** option).
   + The commit message to include (with the **--commit-message** option).
   + The name to use for the commit (with the **--name** option).
   + The email address to use for the commit (with the **--email** option).

   For example, to merge a source branch named *main* with a destination branch named *bugfix-1234* in a repository named *MyDemoRepo*:

   ```
   aws codecommit merge-branches-by-three-way --source-commit-specifier main --destination-commit-specifier bugfix-bug1234 --author-name "Jorge Souza" --email "jorge_souza@example.com" --commit-message "Merging changes from main to bugfix branch before additional testing."  --repository-name MyDemoRepo
   ```

   If successful, this command produces output similar to the following:

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

# Change branch settings in AWS CodeCommit
<a name="how-to-change-branch"></a>

You can change which branch to use as the default branch in the AWS CodeCommit console or with the AWS CLI. For example, if you created your first commit using a Git client that set the default branch to *master*, you could create a branch named *main*, and then change the branch settings so that the new branch is set as the default branch for the repository. To change other branch settings, you can use Git from a local repo connected to the CodeCommit repository. 

**Topics**
+ [

## Change the default branch (console)
](#how-to-change-branch-console)
+ [

## Change the default branch (AWS CLI)
](#how-to-change-branch-cli)

## Change the default branch (console)
<a name="how-to-change-branch-console"></a>

You can specify which branch is the default branch in a CodeCommit repository in the AWS CodeCommit console. 

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 where you want to change settings. 

1. In the navigation pane, choose **Settings**.

1. In **Default branch**, choose the branch drop-down list and choose a different branch. Choose **Save**.
**Tip**  
If you do not see another branch in the drop-down list, you have not created any additional branches. You cannot change the default branch of a repository if the repository has only one branch. For more information, see [Create a branch in AWS CodeCommit](how-to-create-branch.md).
If you do not see the **Default branch** section but instead see items for notification rules and connections, you are in the general settings menu for the console. The settings menu for repositories is listed under **Repositories** at the same level as **Code** and **Pull requests**.

## Change the default branch (AWS CLI)
<a name="how-to-change-branch-cli"></a>

To use AWS CLI commands with CodeCommit, install the AWS CLI. For more information, see [Command line reference](cmd-ref.md). 

To use the AWS CLI to change a repository's branch settings in a CodeCommit repository, run the following command:
+ [update-default-branch](#how-to-change-branch-cli-default) to change the default branch.

### To change the default branch
<a name="how-to-change-branch-cli-default"></a>

1. Run the **update-default-branch** command, specifying:
   + The name of the CodeCommit repository where the default branch is updated (with the **--repository-name** option).
**Tip**  
To get the name of the CodeCommit repository, run the [list-repositories](how-to-view-repository-details.md#how-to-view-repository-details-no-name-cli) command.
   + The name of the new default branch (with the **--default-branch-name** option).
**Tip**  
To get the name of the branch, run the [list-branches](how-to-view-branch-details.md#how-to-view-branch-details-cli) command.

1. For example, to change the default branch to `MyNewBranch` in a CodeCommit repository named `MyDemoRepo`:

   ```
   aws codecommit update-default-branch --repository-name MyDemoRepo --default-branch-name MyNewBranch
   ```

   This command produces output only if there are errors.

For more options, see your Git documentation.

# Delete a branch in AWS CodeCommit
<a name="how-to-delete-branch"></a>

You can use the CodeCommit console to delete a branch in a repository. Deleting a branch in CodeCommit does not delete that branch in a local repo, so users might continue to have copies of that branch until the next time they pull changes. To delete a branch locally and push that change to the CodeCommit repository, use Git from a local repo connected to the CodeCommit repository. 

Deleting a branch does not delete any commits, but it does delete all references to the commits in that branch. If you delete a branch that contains commits that have not been merged into another branch in the repository, you cannot retrieve those commits unless you have their full commit IDs. 

**Note**  
You cannot use the instructions in this topic to delete a repository's default branch. If you want to delete the default branch, you must create a branch, make the new branch the default branch, and then delete the old branch. For more information, see [Create a branch](how-to-create-branch.md) and [Change branch settings](how-to-change-branch.md).

**Topics**
+ [

## Delete a branch (console)
](#how-to-delete-branch-console)
+ [

## Delete a branch (AWS CLI)
](#how-to-delete-branch-cli)
+ [

## Delete a branch (Git)
](#how-to-delete-branch-git)

## Delete a branch (console)
<a name="how-to-delete-branch-console"></a>

You can use the CodeCommit console to delete a branch in a CodeCommit 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 where you want to delete a branch. 

1. In the navigation pane, choose **Branches**.

1. Find the name of the branch that you want to delete, choose **Delete branch**, and confirm your choice.

## Delete a branch (AWS CLI)
<a name="how-to-delete-branch-cli"></a>

You can use the AWS CLI to delete a branch in a CodeCommit repository, if that branch is not the default branch for the repository. For more information about installing and using the AWS CLI, see [Command line reference](cmd-ref.md). 

1. At the terminal or command line, run the **delete-branch** command, specifying:
   + The name of the CodeCommit repository where the branch is to deleted (with the **--repository-name** option).
**Tip**  
To get the name of the CodeCommit repository, run the [list-repositories](how-to-view-repository-details.md#how-to-view-repository-details-no-name-cli) command.
   + The name of the branch to delete (with the **branch-name** option).
**Tip**  
To get the name of the branch, run the [list-branches](how-to-view-branch-details.md#how-to-view-branch-details-cli) command.

1. For example, to delete a branch named `MyNewBranch` in an CodeCommit repository named `MyDemoRepo`:

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

   This command returns information about the deleted branch, including the name of the deleted branch and the full commit ID of the commit that was the head of the branch. For example:

   ```
   "deletedBranch": {
       "branchName": "MyNewBranch",
       "commitId": "317f8570EXAMPLE"
   }
   ```

## Delete a branch (Git)
<a name="how-to-delete-branch-git"></a>

Follow these steps to use Git from a local repo to delete a branch in a CodeCommit repository.

These steps are written with the assumption that you have already connected the local repo to the CodeCommit repository. For instructions, see [Connect to a repository](how-to-connect.md).

1. To delete the branch from the local repo, run the **git branch -D *branch-name*** command where *branch-name* is the name of the branch you want to delete.
**Tip**  
To get a list of branch names, run **git branch --all**.

   For example, to delete a branch in the local repo named `MyNewBranch`:

   ```
   git branch -D MyNewBranch
   ```

1. To delete the branch from the CodeCommit repository, run the **git push *remote-name* --delete *branch-name*** command where *remote-name* is the nickname the local repo uses for the CodeCommit repository and *branch-name* is the name of the branch you want to delete from the CodeCommit repository. 
**Tip**  
To get a list of CodeCommit repository names and their URLs, run the **git remote -v** command.

   For example, to delete a branch named `MyNewBranch` in the CodeCommit repository named `origin`:

   ```
   git push origin --delete MyNewBranch
   ```
**Tip**  
This command does not delete a branch if it is the default branch.

For more options, see your Git documentation.