

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 基本 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)
+ [分支](#how-to-basic-git-branches)
+ [Tags (標籤)](#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`  | 
| 對尚未擁有預設分支的儲存庫進行初始遞交時，將所有本機儲存庫的預設分支名稱設定為主要  | 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`  | 
|  可用於使用本機儲存庫為 CodeCommit 儲存庫指定的別名和 CodeCommit 儲存庫的指定 URL，在本機儲存庫與遠端儲存庫 （例如 CodeCommit CodeCommit 儲存庫） 之間設定連線。  |  `git remote add remote-name remote-url`  | 
|  在本機電腦上目前資料夾的指定子資料夾中，在指定的 URL 上複製 CodeCommit 儲存庫，以建立本機儲存庫。此命令也會為複製的 CodeCommit 儲存庫中的每個分支建立遠端追蹤分支，並建立和檢查從複製的 CodeCommit 儲存庫中目前預設分支中提取的初始分支。  |  `git clone remote-url local-subfolder-name`  | 
|  顯示本機儲存庫用於 CodeCommit 儲存庫的別名。  |  `git remote`  | 
|  顯示本機儲存庫用於擷取和推送至 CodeCommit 儲存庫的別名和 URL。  |  `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 文件中的[了解歷史記錄：遞交](https://git-scm.com/docs/user-manual#understanding-commits)。)  |  `git log --graph --pretty=format:"%H (%h) : %cn : %ar : %s"`  | 

## 分支
<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`  | 

## Tags (標籤)
<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`  | 