Git 从入门到精通

55 阅读5分钟

一、Git 的核心概念

核心概念:

工作区 (Working Directory):编辑文件的地方。 暂存区 (Staging Area / Index):一个中间区域,存放准备下次提交的更改。 本地仓库 (Local Repository):存放项目所有版本数据的地方,位于 .git 目录。 远程仓库 (Remote Repository):托管在网络服务器(如 GitHub, GitLab)上的项目仓库。

文件在 Git 中的生命周期:
工作区 -> git add -> 暂存区 -> git commit -> 本地仓库 -> git push -> 远程仓库

二、基础命令

1. 配置命令 git config

# 设置全局用户名和邮箱(对所有项目生效)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 查看所有配置项
git config --list

# 为特定项目设置不同的用户名/邮箱(在该项目目录下运行,去掉 --global)
git config user.name "Your Project Name"

2. 初始化与克隆 git init & git clone

# 在当前目录初始化一个新的 Git 仓库
git init

# 克隆一个远程仓库到本地(最常用的方式)
git clone https://github.com/username/repository.git

3. 检查状态与查看更改 git status & git diff

查看工作区和暂存区的状态。

# 查看当前仓库状态(哪些文件被修改、已暂存等)
git status

# 查看工作区中尚未暂存的更改(比较 工作区 和 暂存区)
git diff

# 查看已暂存但未提交的更改(比较 暂存区 和 本地仓库)
git diff --staged
# 或者
git diff --cached

4. 添加与提交 git add & git commit

将更改记录到仓库中。

# 将指定文件添加到暂存区
git add <file-name>

# 添加当前目录下所有更改的文件到暂存区(小心使用)
git add .

# 将暂存区的内容提交到本地仓库,并添加提交信息
git commit -m "Your descriptive commit message"

# 一个更便捷的组合:跳过 git add,直接提交所有已跟踪文件的更改
# (注意:不会提交新文件,即未跟踪的文件)
git commit -am "Your commit message"

5. 查看历史 git log

查看提交历史。

# 查看详细的提交历史
git log

# 查看简洁的历史(单行显示)
git log --oneline

# 查看带有分支、标签和合并历史的图形化日志
git log --graph --oneline --decorate --all

三、分支管理命令

分支是 Git 的杀手锏功能,允许你在不影响主线的同时进行工作。

1. 查看、创建与切换分支 git branch & git checkout / git switch

# 查看所有本地分支(当前分支前有 * 号)
git branch

# 查看所有分支(包括远程分支)
git branch -a

# 创建一个新分支
git branch <new-branch-name>

# 切换到指定分支
git checkout <branch-name>
# 或者(较新的命令,语义更清晰)
git switch <branch-name>

# 创建并立即切换到新分支
git checkout -b <new-branch-name>
# 或者
git switch -c <new-branch-name>

2. 合并分支 git merge

将一个分支的更改整合到当前分支。

# 先切换到要合并到的目标分支(如 main)
git switch main

# 将特性分支(如 feature-branch)合并到当前分支(main)
git merge feature-branch

3. 删除分支 git branch -d

# 删除一个已合并的分支(安全删除)
git branch -d <branch-name>

# 强制删除一个分支(即使它还没有被合并)
git branch -D <branch-name>

4. 变基 git rebase (高级)

另一种整合分支的方法,可以创造更线性的历史。

# 先切换到特性分支
git switch feature-branch

# 将特性分支变基到主分支上
git rebase main

注意: rebase 会重写历史,切勿对已推送至公共仓库的分支执行 rebase


四、远程协作命令

与远程仓库(如 GitHub)交互。

1. 查看与管理远程仓库 git remote

# 查看已配置的远程仓库(通常名为 origin)
git remote -v

# 添加一个新的远程仓库
git remote add <remote-name> <url>
# 例如:git remote add upstream https://github.com/original/repo.git

# 修改远程仓库的 URL
git remote set-url origin <new-url>

2. 获取与拉取 git fetch & git pull

# 从远程仓库下载所有数据,但不会自动合并到工作区
git fetch origin

# 下载并立即合并到当前分支(git fetch + git merge)
git pull origin main

# 下载并立即使用变基方式合并(git fetch + git rebase)
git pull --rebase origin main

3. 推送 git push

将本地提交上传到远程仓库。

# 将当前分支推送到与之关联的远程分支
git push

# 将本地指定分支推送到远程仓库
git push origin <local-branch-name>

# 推送并设置上游关联(首次推送时常用)
git push -u origin <branch-name>
# 之后就可以直接使用 `git push`

# 删除远程分支
git push origin --delete <remote-branch-name>

五、撤销与回退命令

谨慎使用,尤其是涉及历史重写的操作。

1. 撤销工作区修改 git restore / git checkout --

# 撤销对工作区文件的修改,恢复到最近一次 git commit 或 git add 的状态
git restore <file-name>
# 旧写法(仍有效)
git checkout -- <file-name>

2. 撤销暂存区的修改(取消 add)git restore --staged / git reset

# 将文件从暂存区撤回到工作区,但保留工作区的修改
git restore --staged <file-name>
# 旧写法
git reset HEAD <file-name>

3. 回退提交 git reset (危险!)

回退到某个提交,可以修改历史。

# 软重置:只回退仓库历史,暂存区和工作区不变(代码保留)
git reset --soft <commit-hash>

# 混合重置(默认):回退仓库和暂存区,但工作区不变(代码保留,但需重新 add)
git reset --mixed <commit-hash>

# 硬重置(危险!):彻底回退,仓库、暂存区、工作区全部回退,指定提交之后的更改全部丢弃!
git reset --hard <commit-hash>
# 例如:回退到上一个提交 git reset --hard HEAD^

4. 撤销提交(生成新提交)git revert (安全!)

创建一个新的提交来撤销之前的某个提交,不会重写历史,更安全。

# 撤销指定的提交,并生成一个新的撤销提交
git revert <commit-hash>