Git Commands
8 methodsGit 命令行工具集,提供版本控制的核心操作。
git init [directory]在指定目录初始化一个新的 Git 仓库,创建 .git 子目录。
Parameters
| Name | Type | Description |
|---|---|---|
| directory | string | 目标目录路径,默认为当前目录。 |
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
| Name | Type | Description |
|---|---|---|
| repo | string | 远程仓库 URL(HTTPS/SSH/Git 协议)。 |
| directory | string | 本地目标目录名,默认为仓库名。 |
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.gitgit add <pathspec>将工作区的修改加入暂存区,为下次提交做准备。
Parameters
| Name | Type | Description |
|---|---|---|
| pathspec | string | 文件或路径模式,支持通配符,如 .、-A、-u。 |
Returns
更新暂存区索引,无标准输出,退出码 0 表示成功。
Example
git
git add .
git add src/index.ts
git add -A
git add -u
git add src/*.tsgit commit -m <msg>将暂存区的内容提交到本地仓库,生成新的提交对象。
Parameters
| Name | Type | Description |
|---|---|---|
| message | string | 提交说明信息。 |
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
| Name | Type | Description |
|---|---|---|
| name | string | 分支名;省略时列出所有分支。 |
Returns
无参数时输出分支列表,创建/删除返回退出码。
Example
git
git branch
git branch feature/login
git branch -a
git branch -d feature/old
git branch -m old-name new-namegit merge <commit>将指定分支或提交合并到当前分支。
Parameters
| Name | Type | Description |
|---|---|---|
| commit | string | 要合并的分支名或提交哈希。 |
Returns
合并结果信息;冲突时输出冲突文件列表,退出码非 0。
Example
git
git checkout main
git merge feature/login
git merge --no-ff feature/login
git merge --abortgit rebase <upstream>将当前分支的提交在指定上游分支之上重新应用,得到线性历史。
Parameters
| Name | Type | Description |
|---|---|---|
| upstream | string | 上游分支名,如 main、origin/main。 |
Returns
变基成功输出状态信息;冲突时进入交互式冲突解决流程。
Example
git
git checkout feature/login
git rebase main
git rebase -i HEAD~3
git rebase --continue
git rebase --abortgit remote add <name> <url>添加一个新的远程仓库引用。
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | 远程仓库名,通常为 origin。 |
| url | string | 远程仓库 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