# 27.多存储库

到目前为止，我们一直在使用单个 git 存储库。然而，git 擅长处理多个存储库。这些额外的存储库可以本地存储，也可以通过网络连接访问。

本节我们将创建一个名为`cloned_git_learnnig`的新存储库。展示如何从一个存储库移动更改到另一个存储库，并且当两个存储库之间发生冲突时如何处理。

目前，我们将使用本地存储库（即存储在本地硬盘上的存储库）进行工作，但是，在本节中学到的大多数内容都适用于多个存储库，无论它们是在本地还是通过网络远程存储。

> 注意：我们将对两个副本的仓库进行更改，操作前请确定你所在的仓库是哪个。

## 克隆 `git_learning` 仓库

```powershell
# 列出当前目录下的文件及文件
# 我的仓库存放在我的桌面文件夹
$ ls

   Directory: C:\Users\aku\Desktop

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----            2023/5/5    20:36                git_learning
-a---           2023/4/25    13:13           1398 Visual Studio Code.lnk

# 在仓库目录的上级目录下输入
$ git clone git_learning cloned_git_learnnig
Cloning into 'cloned_git_learnnig'...
done.
```

## 进入 `cloned_git_learnnig` 查看提交日志

```powershell
# 进入文件夹
$ cd  .\cloned_git_learnnig\

# 查看日志
git hist
* 2456c71 2023-05-05 | Added teset2.txt (HEAD -> master, origin/master, origin/lgnew, origin/HEAD) [aku]
* 1c78eab 2023-05-05 | Added teset3.txt [aku]
* 31e5621 2023-05-05 | Added teset2.txt [aku]
* 5dc8b1e 2023-05-05 | Added .gitignore [aku]
* b77158a 2023-05-05 | Moved test.txt to lab [aku]
* 929f644 2023-05-05 | Added 123456 to the test.txt [aku]
* d7f681f 2023-05-05 | Added abc to the test.txt (tag: v1) [aku]
* 01b8702 2023-05-05 | Add first file (tag: v1-beta) [aku]
```

我们可以看到提交记录和原始仓库的提交是一样的，只是分支名称发生了改变 。

## origin 是什么？

`origin` 是 Git 默认使用的一个远程存储库的名称，当你在 Git 中克隆或创建一个新的本地存储库时，Git 默认会将其与名为 `origin` 的远程存储库相关联。

```powershell
# 显示本地存储库中所有远程存储库的详细信息
$ git remote
 origin
```

```powershell
# 查看详细信息
$ git remote show origin
* remote origin
Fetch URL: C:/Users/aku/Desktop/git_learning
Push  URL: C:/Users/aku/Desktop/git_learning
HEAD branch: master
Remote branches:
lgnew  tracked
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (up to date)
```

* `remote origin`: 显示您正在查看的远程存储库的名称。
* `Fetch URL`: 显示远程存储库的 URL，可以从该 URL 拉取最新代码更改。
* `Push URL`: 显示可以使用的 URL 将本地更改推送到远程存储库。
* `HEAD branch`: 显示在远程存储库上活动的默认分支。
* `Remote branch`: 显示本地存储库中正在追踪的远程分支的列表以及其与远程分支的关系。这里的 "main tracked" 表示本地存储库中的 `main` 分支正在追踪名为 `main` 的远程分支。
* `Local branch configured for 'git pull'`: 显示可以从远程存储库中拉取代码更改的本地分支以及如何与相应的远程分支合并。这里的 "main merges with remote main" 表示在运行 `git pull` 时，本地存储库中的 `main` 分支会自动与名为 `main` 的远程分支合并。
* `Local ref configured for 'git push'`: 显示更新远程存储库的本地分支以及它们将被推送到远程存储库的位置。这里的 "main pushes to main (up to date)" 表示在运行 `git push` 时，本地存储库中的 `main` 分支会被推送到名为 `main` 的远程分支，并且本地分支和远程分支是同步的。

## 查看克隆仓库中的分支

```powershell
# git branch 命令仅列出本地分支
$ git branch
* master
```

本地分支的意思是本地仓库现有的分支。

```powershell
# 查看所有分支
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/lgnew
remotes/origin/master
```

Git 拥有原始仓库中的所有提交，但远程仓库中的分支在这里不被视为本地分支。如果我们想要自己的 `lgnew` 分支，我们需要自己创建它。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alanmpan.gitbook.io/git-learning/git/27.-duo-cun-chu-ku.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
