Git 命令速查手册

您日常 Git 工作流程的得力助手

仓库初始化

开始一个新项目或从现有项目获取副本

git init [project-name]

在当前目录或指定目录创建一个新的本地 Git 仓库。

使用示例:

git init my-awesome-project
git clone [url]

克隆一个远程仓库到本地。

使用示例:

git clone https://github.com/user/repo.git
日常工作流

将项目中的变更保存到仓库

git status

显示工作目录和暂存区的状态。

git add [file]

将文件更改添加到暂存区。使用 "." 代替 [file] 可添加所有更改。

使用示例:

git add .
git commit -m "[message]"

将暂存区的更改提交到仓库历史。

使用示例:

git commit -m "feat: add user authentication"
git pull

从远程仓库拉取最新的变更。

git push

将本地提交推送到远程仓库。

分支管理

利用分支实现并行开发

git branch

列出所有本地分支。

git branch [branch-name]

创建一个新分支。

使用示例:

git branch new-feature
git checkout [branch-name]

切换到指定分支。

使用示例:

git checkout new-feature
git merge [branch-name]

将指定分支的历史合并到当前分支。

使用示例:

git merge new-feature
git branch -d [branch-name]

删除一个已经合并的本地分支。

使用示例:

git branch -d old-feature
远程协作

与其他开发者同步代码

git remote -v

显示所有配置的远程仓库。

git fetch [remote]

从远程仓库下载所有分支和数据,但不自动合并。

使用示例:

git fetch origin
git push [remote] [branch]

将本地分支的提交推送到远程仓库。

使用示例:

git push origin main