

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 基本 Git 命令
<a name="how-to-basic-git"></a>

您可以使用 Git 来处理本地 CodeCommit 存储库和已连接本地存储库的存储库。

以下是常用 Git 命令的一些基本示例。

有关更多选项，请参阅 Git 文档。

**Topics**
+ [配置变量](#how-to-basic-git-configuration-variables)
+ [远程存储库](#how-to-basic-git-remotes)
+ [提交](#how-to-basic-git-commits)
+ [Branches](#how-to-basic-git-branches)
+ [标签](#how-to-basic-git-tags)

## 配置变量
<a name="how-to-basic-git-configuration-variables"></a>


|  |  | 
| --- |--- |
|  列出所有配置变量。  |  `git config --list`  | 
|  只列出本地配置变量。  |  `git config --local -l`  | 
|  只列出系统配置变量。  |  `git config --system -l`  | 
|  只列出全局配置变量。  |  `git config --global -l`  | 
|  在指定的配置文件中设置配置变量。  |  `git config [--local \| --global \| --system] variable-name variable-value`  | 
| 在对还没有默认分支的存储库进行初始提交时，将所有本地存储库的默认分支名称设置为 main | git config --global init.defaultBranch main | 
|  直接编辑配置文件。也可用来发现特定配置文件的位置。要退出编辑模式，通常可键入 `:q` (不保存更改并退出) 或 `:wq` (保存更改并退出)，然后按 Enter。  |  `git config [--local \| --global \| --system] --edit`  | 

## 远程存储库
<a name="how-to-basic-git-remotes"></a>


|  |  | 
| --- |--- |
|  初始化本地存储库，为将其连接到存储库做准备。 CodeCommit   |  `git init`  | 
|  可以使用本地存储库为存储库设置的指定昵称和指向存储库的指定 URL，在本地 CodeCommit 存储库和远程存储库（例如 CodeCommit 存储库）之间建立连接。 CodeCommit   |  `git remote add remote-name remote-url`  | 
|  通过在本地计算机上当前文件夹的指定子文件夹中以指定 URL 复制 CodeCommit 存储库来创建本地存储库。此命令还会为克隆 CodeCommit 存储库中的每个分支创建一个远程跟踪分支，并创建和检出从克隆存储库中当前默认分支派生的初始分支。 CodeCommit   |  `git clone remote-url local-subfolder-name`  | 
|  显示本地存储库为 CodeCommit 存储库使用的昵称。  |  `git remote`  | 
|  显示本地存储库用于提取和推送到存储库的昵称和网址。 CodeCommit   |  `git remote -v`  | 
|  使用本地存储库为 CodeCommit 存储库和指定分支设置的指定昵称，将已完成的提交从本地存储库推送到 CodeCommit 存储库。推送期间还会为本地存储库设置上游跟踪信息。  |  `git push -u remote-name branch-name`  | 
| 设置上游跟踪信息后，将已完成的提交从本地 CodeCommit 存储库推送到存储库。 | `git push` | 
|  使用本地存储库为存储库和指定分支设置的指定昵称，从 CodeCommit 存储库中提取到本地 CodeCommit 存储库的最终提交  |  `git pull remote-name branch-name`  | 
| 设置上游跟踪信息后，将已完成的提交从 CodeCommit 存储库中提取到本地存储库。 | `git pull` | 
|  使用本地存储库为 CodeCommit 存储库指定的昵称，断开本地存储库与存储库的连接。 CodeCommit   |  `git remote rm remote-name`  | 

## 提交
<a name="how-to-basic-git-commits"></a>


|  |  | 
| --- |--- |
|  显示已添加或尚未添加到本地存储库中待处理提交的内容。  |  `git status`  | 
|  以简明格式显示已添加或尚未添加到本地存储库中的待处理提交的内容。 (`M` = 已修改，`A` = 已添加，`D` = 已删除，等)  |  `git status -sb`  | 
|  显示本地存储库中待处理提交和最新提交之间的更改。  |  `git diff HEAD`  | 
|  将特定文件添加到本地存储库中的待处理提交。  |  `git add [file-name-1 file-name-2 file-name-N \| file-pattern]`  | 
|  将所有新建的、修改的和删除的文件添加到本地存储库中的待处理提交。  |  `git add `  | 
|  开始最终确定本地存储库中的待处理提交，这会显示一个编辑器供您输入提交消息。输入消息后，待处理提交变成最终确定状态。  |  `git commit`  | 
|  最终确定本地存储库中的待处理提交，包括指定提交消息。  |  `git commit -m "Some meaningful commit comment"`  | 
|  列出本地存储库中的最新提交。  |  `git log`   | 
|  以图形格式列出本地存储库中的最新提交。  |  `git log --graph`  | 
|  以预定义的紧缩格式列出本地存储库中的最新提交。  |  `git log --pretty=oneline`  | 
|  以预定义的紧缩格式列出本地存储库中的最新提交，同时附上图形。  |  `git log --graph --pretty=oneline`  | 
|  以自定义格式列出本地存储库中的最新提交，同时附上图形。 （有关更多选项，请参阅 Git 文档中的 [Understanding History: Commits](https://git-scm.com/docs/user-manual#understanding-commits)。）  |  `git log --graph --pretty=format:"%H (%h) : %cn : %ar : %s"`  | 

## Branches
<a name="how-to-basic-git-branches"></a>


|  |  | 
| --- |--- |
|  列出本地存储库中的所有分支，并在当前分支旁边显示一个星号 (`*`)。  |  `git branch`  | 
|  将有关存储库中所有现有分支的信息提取到本地 CodeCommit 存储库。  |  `git fetch`  | 
|  列出本地存储库中的所有分支和本地存储库中的远程跟踪分支。  |  `git branch -a`  | 
|  只列出本地存储库中的远程跟踪分支。  |  `git branch -r`  | 
|  使用指定的分支名称在本地存储库中创建一个新的分支。  |  `git branch new-branch-name`  | 
|  使用指定的分支名称切换到本地存储库中的另一个分支。  |  `git checkout other-branch-name`  | 
|  使用指定的分支名称在本地存储库中创建一个新的分支，然后切换到该分支。  |  `git checkout -b new-branch-name`  | 
|  使用本地存储库为 CodeCommit 仓库设置的指定昵称和指定的分支名称，将新分支从本地 CodeCommit 存储库推送到存储库。推送期间会为本地存储库中的分支设置上游跟踪信息。  |  `git push -u remote-name new-branch-name`  | 
|  使用指定的分支名称在本地存储库中创建一个新的分支。然后，使用本地存储库为存储库设置的指定昵称和指定的分支名称，将本地 CodeCommit 存储库中的新分支连接到 CodeCommit 存储库中的现有分支。  |  `git branch --track new-branch-name remote-name/remote-branch-name`  | 
|  将本地存储库中另一个分支的更改合并到本地存储库中的当前分支。  |  `git merge from-other-branch-name`  | 
|  删除本地存储库中的某个分支，除非其包含尚未合并的作业。  |  `git branch -d branch-name`  | 
|  使用本地 CodeCommit 存储库为仓库设置的指定昵称和指定的分支名称删除 CodeCommit 存储库中的分支。（注意冒号（`:`）的用法。）  |  `git push remote-name :branch-name`  | 

## 标签
<a name="how-to-basic-git-tags"></a>


|  |  | 
| --- |--- |
|  列出本地存储库中的所有标签。  |  `git tag`  | 
|  将所有标签从 CodeCommit 存储库提取到本地存储库。  |  `git fetch --tags`  | 
|  显示本地存储库中有关特定标签的信息。  |  `git show tag-name`  | 
|  在本地存储库中创建“轻型”标签。  |  `git tag tag-name commit-id-to-point-tag-at`  | 
|  使用本地存储库为 CodeCommit 存储库设置的指定昵称和指定的标签名称，将特定标签从本地存储库推送到 CodeCommit 存储库。  |  `git push remote-name tag-name`  | 
|  使用本地存储库为 CodeCommit 仓库设置的指定昵称，将所有标签从本地存储库推送到存储库。 CodeCommit   |  `git push remote-name --tags`  | 
|  删除本地存储库中的某个标签。  |  `git tag -d tag-name`  | 
|  使用本地 CodeCommit 存储库为存储库设置的指定昵称和指定的标签名称，删除 CodeCommit 存储库中的标签。（注意冒号（`:`）的用法。）  |  `git push remote-name :tag-name`  | 