Skip to content

Git Plumbing & Porcelain API

Git 版本控制系统常用命令参考,涵盖仓库初始化、克隆、提交、分支、合并、变基与远程仓库管理。

1 class · 8 methods

Git Commands

8 methods

Git 命令行工具集,提供版本控制的核心操作。

git init [directory]

在指定目录初始化一个新的 Git 仓库,创建 .git 子目录。

Parameters

NameTypeDescription
directorystring目标目录路径,默认为当前目录。

Returns

在目标目录创建 .git 仓库元数据,命令退出码 0 表示成功。

Example

git
git init my-project
cd my-project
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"
git clone <repo> [dir]

从远程仓库克隆完整副本到本地,包含全部历史记录。

Parameters

NameTypeDescription
repostring远程仓库 URL(HTTPS/SSH/Git 协议)。
directorystring本地目标目录名,默认为仓库名。

Returns

在本地创建仓库副本并检出默认分支,退出码 0 表示成功。

Example

git
git clone https://github.com/user/repo.git
cd repo

git clone https://github.com/user/repo.git my-local-dir

git clone --depth 1 https://github.com/user/repo.git
git add <pathspec>

将工作区的修改加入暂存区,为下次提交做准备。

Parameters

NameTypeDescription
pathspecstring文件或路径模式,支持通配符,如 .、-A、-u。

Returns

更新暂存区索引,无标准输出,退出码 0 表示成功。

Example

git
git add .
git add src/index.ts
git add -A
git add -u
git add src/*.ts
git commit -m <msg>

将暂存区的内容提交到本地仓库,生成新的提交对象。

Parameters

NameTypeDescription
messagestring提交说明信息。

Returns

返回新提交的哈希值与分支信息,如 [main abc1234] ...。

Example

git
git commit -m "Fix login bug"
git commit -m "Fix login bug" -m "Detailed description here"
git commit --amend -m "Updated message"
git branch [name]

列出、创建或删除分支。

Parameters

NameTypeDescription
namestring分支名;省略时列出所有分支。

Returns

无参数时输出分支列表,创建/删除返回退出码。

Example

git
git branch
git branch feature/login
git branch -a
git branch -d feature/old
git branch -m old-name new-name
git merge <commit>

将指定分支或提交合并到当前分支。

Parameters

NameTypeDescription
commitstring要合并的分支名或提交哈希。

Returns

合并结果信息;冲突时输出冲突文件列表,退出码非 0。

Example

git
git checkout main
git merge feature/login
git merge --no-ff feature/login
git merge --abort
git rebase <upstream>

将当前分支的提交在指定上游分支之上重新应用,得到线性历史。

Parameters

NameTypeDescription
upstreamstring上游分支名,如 main、origin/main。

Returns

变基成功输出状态信息;冲突时进入交互式冲突解决流程。

Example

git
git checkout feature/login
git rebase main
git rebase -i HEAD~3
git rebase --continue
git rebase --abort
git remote add <name> <url>

添加一个新的远程仓库引用。

Parameters

NameTypeDescription
namestring远程仓库名,通常为 origin。
urlstring远程仓库 URL。

Returns

无输出;成功时退出码 0,已存在时报错。

Example

git
git remote add origin https://github.com/user/repo.git
git remote -v
git remote remove origin
git remote set-url origin https://github.com/user/new-repo.git