> For the complete documentation index, see [llms.txt](https://avincheng.gitbook.io/notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avincheng.gitbook.io/notebook/devops/git/chang-jian-git-cao-zuo.md).

# 常见 Git 操作

## 创建 Git 仓库

### 在远程仓库初始化

在 Git 仓库托管平台（Github、Gitee 等）创建 Git 仓库，并使用 README 初始化，此时远程仓库拥有 master 分支，有提交记录。

克隆远程仓库到本地：

```
git clone <repository> [<directory>]
```

> **Tips:**
>
> `<repository>` 为远程仓库 HTTPS 或 SSH 格式地址。只有配置了远程仓库托管平台的 SSH key 才能使用 SSH 格式地址。
>
> `<directory>` 为需要新建的项目目录或者已有的空白项目目录。默认使用远程仓库名称在当前目录创建项目目录。

克隆到本地时，会将远程仓库标记为 origin，并自动创建和 origin/master 有追踪关系的本地 master 分支。

### 在本地仓库初始化

在 Git 仓库托管平台（Github、Gitee 等）创建 Git 仓库（不初始化），此时远程仓库空白，无任何分支，无任何提交记录。

进入本地已经初始化过的 Git 仓库（本地 Git 仓库已有 master 分支，有提交记录），添加远程仓库，并标记为 origin：

```
git remote add origin <repository>
```

将本地 master 分支推送到 origin，并建立和 origin/master 的追踪关系：

```
git push -u origin master:master
```

### 合并两个已初始化过的仓库

在 Git 仓库托管平台（Github、Gitee 等）创建 Git 仓库，并使用 README 初始化，此时远程仓库拥有 master 分支，有提交记录。

进入本地已经初始化过的 Git 仓库（本地 Git 仓库已有 master 分支，有提交记录），添加远程仓库，并标记为 origin：

```
git remote add origin <repository>
```

拉取 origin 仓库数据到本地：

```
git fetch origin
```

基于 origin/master 分支创建本地临时分支：

```
git branch temp origin/master
```

基于本地 temp 分支合并当前分支的变更到当前分支：

```
git rebase temp
```

删除本地临时分支 temp：

```
git branch -d temp
```

将本地 master 分支推送到 origin，并建立和 origin/master 的追踪关系：

```
git push -u origin master:master
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://avincheng.gitbook.io/notebook/devops/git/chang-jian-git-cao-zuo.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.
