Skip to content

Git

Git 是常用的分布式版本控制工具,用于管理代码版本、分支协作和历史追踪。

配置用户信息

首次使用 Git 时,通常需要配置用户名和邮箱。

写法:

bash
git config --global user.name "your-name"
git config --global user.email "your-email@example.com"

效果/说明:

配置后,后续提交记录会带上对应的作者信息。

bash
git config --global user.name
git config --global user.email

初始化仓库

在当前目录初始化一个新的 Git 仓库。

写法:

bash
git init

效果/说明:

执行后会生成 .git 目录,当前目录开始被 Git 管理。

text
Initialized empty Git repository in /path/to/project/.git/

克隆远程仓库

从远程地址复制一份仓库到本地。

写法:

bash
git clone https://example.com/user/repo.git

效果/说明:

执行后会在本地创建同名项目目录,并下载仓库历史记录。

text
Cloning into 'repo'...

查看仓库状态

查看当前工作区和暂存区的变更状态。

写法:

bash
git status

效果/说明:

可以看到哪些文件被修改、哪些文件未跟踪、哪些文件已暂存。

text
On branch master
Changes not staged for commit:
  modified:   docs/index.md

Untracked files:
  docs/tools/git/index.md

查看简洁状态

适合快速查看变更列表。

写法:

bash
git status --short

效果/说明:

text
 M docs/index.md
?? docs/tools/git/index.md

常见状态含义:

标记说明
M文件被修改
A新增文件
D删除文件
??未跟踪文件

添加到暂存区

将文件变更加入暂存区,准备提交。

写法:

bash
git add docs/index.md

添加多个文件:

bash
git add docs/index.md docs/tools/git/index.md

添加当前目录所有变更:

bash
git add .

效果/说明:

添加后,文件会进入暂存区,等待 git commit 提交。

text
Changes to be committed:
  modified:   docs/index.md

提交变更

将暂存区内容提交到本地仓库。

写法:

bash
git commit -m "update docs"

效果/说明:

提交成功后会生成一个提交记录。

text
[master abc1234] update docs
 1 file changed, 10 insertions(+)

查看提交历史

查看仓库提交记录。

写法:

bash
git log

简洁查看:

bash
git log --oneline -5

效果/说明:

text
abc1234 update docs
def5678 init project

查看文件差异

查看工作区中尚未暂存的变更。

写法:

bash
git diff

查看已暂存但未提交的变更:

bash
git diff --staged

效果/说明:

diff
-旧内容
+新内容

分支管理

查看分支

写法:

bash
git branch

效果/说明:

当前分支前会显示 *

text
* master
  dev

创建分支

写法:

bash
git branch feature/docs

效果/说明:

创建后可以通过 git branch 看到新分支。

text
  feature/docs
* master

切换分支

写法:

bash
git switch feature/docs

旧版本 Git 也可以使用:

bash
git checkout feature/docs

效果/说明:

text
Switched to branch 'feature/docs'

创建并切换分支

写法:

bash
git switch -c feature/docs

效果/说明:

text
Switched to a new branch 'feature/docs'

合并分支

将指定分支合并到当前分支。

写法:

bash
git switch master
git merge feature/docs

效果/说明:

如果没有冲突,会自动完成合并。

text
Merge made by the 'ort' strategy.

删除分支

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

写法:

bash
git branch -d feature/docs

效果/说明:

text
Deleted branch feature/docs.

如果分支未合并,Git 会阻止删除,避免误删代码。

远程仓库

查看远程地址

写法:

bash
git remote -v

效果/说明:

text
origin  https://example.com/user/repo.git (fetch)
origin  https://example.com/user/repo.git (push)

添加远程地址

写法:

bash
git remote add origin https://example.com/user/repo.git

效果/说明:

添加后,本地仓库可以和远程仓库关联。

拉取远程更新

从远程仓库获取并合并更新。

写法:

bash
git pull

常用 rebase 方式:

bash
git pull --rebase

效果/说明:

本地分支会同步远程提交。

text
Already up to date.

推送到远程

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

写法:

bash
git push

首次推送新分支时:

bash
git push -u origin feature/docs

效果/说明:

text
To https://example.com/user/repo.git
   abc1234..def5678  feature/docs -> feature/docs

撤销操作

撤销工作区修改

放弃某个文件在工作区中的修改。

写法:

bash
git restore docs/index.md

效果/说明:

文件会恢复到最近一次提交或暂存的状态。此操作会丢弃本地未保存修改,使用前需要确认。

取消暂存

将文件从暂存区移回工作区。

写法:

bash
git restore --staged docs/index.md

效果/说明:

文件修改仍然保留,但不再处于待提交状态。

修改最近一次提交信息

写法:

bash
git commit --amend -m "new commit message"

效果/说明:

会修改最近一次提交信息。如果提交已经推送到远程,修改前需要谨慎。

标签管理

标签常用于标记版本号。

创建标签

写法:

bash
git tag v1.0.0

效果/说明:

text
v1.0.0

查看标签

写法:

bash
git tag

推送标签

写法:

bash
git push origin v1.0.0

忽略文件

使用 .gitignore 忽略不需要提交的文件。

写法:

gitignore
node_modules/
dist/
.env
*.log

效果/说明:

这些文件不会出现在 git status 的未跟踪列表中。

如果文件已经被 Git 跟踪,只加入 .gitignore 不会自动取消跟踪,需要先从 Git 索引中移除。

bash
git rm --cached .env

常用工作流

下面是一次常见的开发提交流程。

写法:

bash
git status --short
git add docs/tools/git/index.md
git commit -m "update git docs"
git pull --rebase
git push

效果/说明:

  1. 查看当前变更
  2. 添加需要提交的文件
  3. 创建本地提交
  4. 同步远程最新提交
  5. 推送本地提交到远程

常用命令速查

命令说明
git status查看仓库状态
git add <file>添加文件到暂存区
git commit -m "message"提交变更
git log --oneline简洁查看提交历史
git diff查看工作区差异
git branch查看本地分支
git switch <branch>切换分支
git merge <branch>合并分支
git pull拉取远程更新
git push推送到远程
二维码手机扫码查看