【git】工作场景下的 工作区 <-> 暂存区<-> 本地仓库 命令实战 具体案例

🚀 Git 工作区 → 暂存区 → 本地仓库 → 回退实战

Git 的核心流程 是:

👉 工作区(Working Directory)git add暂存区(Staging Area)git commit本地仓库(Local Repository)

如果想回退,可以从本地仓库 → 暂存区 → 工作区


📌 1. 创建一个 Git 仓库

bash 复制代码
mkdir my_project && cd my_project
git init  # 初始化 Git 仓库

git init 会创建一个 .git 目录,用于管理 Git 版本控制信息。


📌 2. 创建一个文件(工作区)

bash 复制代码
echo "Hello Git!" > hello.txt

此时,hello.txt工作区,但 Git 还不知道它的存在。

查看状态:

bash 复制代码
git status

输出:

复制代码
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

Git 发现 hello.txt,但它还没有被跟踪。


📌 3. 添加到暂存区

bash 复制代码
git add hello.txt

再次查看状态:

bash 复制代码
git status

输出:

复制代码
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello.txt

hello.txt 进入了暂存区,但还没有提交到本地仓库。


📌 4. 提交到本地仓库

bash 复制代码
git commit -m "初次提交"

输出:

复制代码
[main (root-commit) 1a2b3c4] 初次提交
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

hello.txt 现在存储到了本地仓库,Git 记录了它的快照。


📌 5. 修改文件并回退

(1) 修改 hello.txt
bash 复制代码
echo "Git is awesome!" >> hello.txt

查看状态:

bash 复制代码
git status

输出:

复制代码
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   hello.txt

hello.txt工作区被修改了。

(2) 回退修改

如果不想要这次修改:

bash 复制代码
git restore hello.txt

再次 git status

复制代码
nothing to commit, working tree clean

hello.txt 回到了本地仓库的版本

(3) 误 add 了,如何回退?
bash 复制代码
git add hello.txt  # 错误地加入暂存区
git restore --staged hello.txt  # 从暂存区撤回到工作区

这样 hello.txt 又变回未 add 之前的状态。

即:

在没有commit之前,想要回退修改都是用git restore

  • 撤销修改git restore <file>
  • 撤销 addgit restore --staged <file>
(4) 误 commit 了,如何回退?

如果误提交到本地仓库:

bash 复制代码
git reset --soft HEAD~1  # 退回到暂存区 也可以用git log显示出所有提交记录,
# 复制想要回到的commit id 使用git reset --soft <commit id> 这里HEAD~1表示回退到上一个commit状态
git reset HEAD  # 退回到工作区

这样就撤回了 commit,但代码不会丢失。

commit之后的回退,都是用git reset


📌 6. 总结:Git 各区域操作

操作 命令
工作区 → 暂存区 git add <file>
暂存区 → 本地仓库 git commit -m "提交信息"
本地仓库 → 暂存区(撤回提交) git reset --soft HEAD~1
暂存区 → 工作区(撤回 add git restore --staged <file>
工作区回退(撤销修改) git restore <file>

** git reset HEADgit restore --staged 的区别**

命令 作用 影响的区域 适用场景
git reset HEAD 取消所有已暂存的修改 撤销暂存区(但保留工作区的修改) 取消 git add .
git restore --staged file.txt 取消某个文件的 git add 撤销暂存区(但不影响工作区) 取消某个文件的 git add
git restore file.txt 还原工作区文件 丢弃未暂存的修改,让文件回到 HEAD 版本 撤销误改的文件

总结:

  • git reset HEAD 作用于 整个暂存区 ,适合批量撤销 git add
  • git restore --staged file.txt 只作用于 单个文件 ,适合撤销特定文件的 git add

Git实战:

案例:开发一个简单的登录功能

假设你正在开发一个网站的登录功能,你需要修改以下文件:

  • index.html: 添加登录表单
  • style.css: 添加登录表单的样式
  • script.js: 添加登录表单的验证逻辑

一、准备工作

  1. 克隆远程仓库:假设你已经克隆了公司项目的代码仓库到本地。
  2. 创建开发分支 :从主分支 (main) 创建一个新的分支 feature/login,并切换到该分支。
bash 复制代码
git checkout -b feature/login

二、开发登录功能

  1. 修改代码 :在 index.htmlstyle.cssscript.js 文件中进行代码编写和修改。
  2. 查看工作区状态 :使用 git status 命令查看工作区中哪些文件被修改了。
bash 复制代码
git status
  • 你会看到类似以下的输出:

    On branch feature/login
    Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git restore <file>..." to discard changes in working directory)
    modified: index.html
    modified: style.css
    modified: script.js

    no changes added to commit (use "git add" and/or "git commit -a")

  • 这表示 index.htmlstyle.cssscript.js 文件被修改了,但还没有添加到暂存区。

三、将修改添加到暂存区

  1. 添加文件到暂存区 :使用 git add 命令将修改后的文件添加到暂存区。
bash 复制代码
git add index.html style.css script.js
  • 也可以使用 git add . 添加所有修改过的文件。
  1. 再次查看工作区状态 :使用 git status 命令查看工作区状态。
bash 复制代码
git status
  • 你会看到类似以下的输出:

    On branch feature/login
    Changes to be committed:
    (use "git restore --staged <file>..." to unstage)
    modified: index.html
    modified: style.css
    modified: script.js

  • 这表示 index.htmlstyle.cssscript.js 文件已经被添加到暂存区,准备提交到本地仓库。

四、将修改提交到本地仓库

  1. 提交代码 :使用 git commit 命令将暂存区的文件提交到本地仓库。
bash 复制代码
git commit -m "完成登录功能开发"
  • 提交信息应该简洁明了,描述这次提交的内容。
  1. 查看提交历史 :使用 git log 命令查看提交历史。
bash 复制代码
git log
  • 你会看到类似以下的输出:

    commit 1234567890abcdef1234567890abcdef12345678 (HEAD -> feature/login)
    Author: yourname [email protected]
    Date: Mon Oct 30 12:00:00 2023 +0800

    复制代码
      完成登录功能开发
  • 这表示你已经成功将修改提交到了本地仓库。

五、回退到之前的版本

假设你在开发过程中发现代码出现了问题,想要回退到之前的版本。

  1. 查看提交历史 :使用 git log 命令查看提交历史,找到你想要回退到的版本的 commit id。
bash 复制代码
git log
  • 你会看到类似以下的输出:

    commit 1234567890abcdef1234567890abcdef12345678 (HEAD -> feature/login)
    Author: yourname [email protected]
    Date: Mon Oct 30 12:00:00 2023 +0800

    复制代码
      完成登录功能开发

    commit 0987654321fedcba0987654321fedcba09876543
    Author: yourname [email protected]
    Date: Mon Oct 30 11:00:00 2023 +0800

    复制代码
      初始化项目
  • 假设你想要回退到 "初始化项目" 的版本,commit id 是 0987654321fedcba0987654321fedcba09876543

  1. 回退到指定版本 :使用 git reset 命令回退到指定版本。
bash 复制代码
git reset --hard 0987654321fedcba0987654321fedcba09876543
  • 这会将工作区、暂存区和本地仓库都回退到指定版本。
  1. 查看工作区状态 :使用 git status 命令查看工作区状态。
bash 复制代码
git status
  • 你会看到类似以下的输出:

    On branch feature/login
    nothing to commit, working tree clean

  • 这表示你已经成功回退到了指定版本。


https://github.com/0voice

相关推荐
LuckyLay12 分钟前
LeetCode算法题(Go语言实现)_15
算法·leetcode·golang
人大博士的交易之路2 小时前
龙虎榜——20250328
大数据·人工智能·数学建模·数据挖掘·程序员创富·涨停回马枪
程序猿chen3 小时前
第一重·筑基篇:SpringBoot丹田气海贯通要诀
java·spring boot·git·后端·java-ee·tomcat·github
moonsims3 小时前
无人机进行航空数据收集对于分析道路状况非常有用-使用无人机勘测高速公路而不阻碍交通-
大数据·无人机
景天科技苑3 小时前
【go微服务】如何快速掌握grpc开发
开发语言·微服务·golang·grpc框架·grpc加密传输·go grpc
人间打气筒(Ada)3 小时前
云原生技术赋能企业数字化转型:实战案例与架构演进
大数据·云原生·架构
豪越大豪3 小时前
豪越消防一体化安全管控平台:消防管理智能化
大数据·运维开发
漫途科技3 小时前
广告牌变“高空炸弹“?智能预警终端筑起安全防线!
大数据·科技·物联网·安全
demonlg01124 小时前
Go 语言标准库中encoding/xml模块详细功能介绍与示例
xml·开发语言·golang
小技工丨4 小时前
【数据仓库】星型模型和维度建模什么区别?
大数据·数据仓库