新手学Git:以一个小游戏项目为例,完成初始化、提交、查看历史与恢复版本

新手从0学Git:以一个小游戏项目为例,教你完成初始化、提交、查看历史与恢复版本

  • 前言
  • [一、Git 到底是什么?](#一、Git 到底是什么?)
  • [二、新手一开始最需要掌握哪些 Git 指令?](#二、新手一开始最需要掌握哪些 Git 指令?)
  • 三、这些指令的作用:
  • [四、案例:我的小游戏接入 Git管理的案例流程](#四、案例:我的小游戏接入 Git管理的案例流程)
    • [1. 先确认 Git 已经安装成功](#1. 先确认 Git 已经安装成功)
    • [2. 进入项目目录](#2. 进入项目目录)
    • [3. 初始化 Git 仓库](#3. 初始化 Git 仓库)
    • [4. 查看项目当前状态](#4. 查看项目当前状态)
    • [五、`.gitignore` 声明哪些文件不需要提交。](#五、.gitignore 声明哪些文件不需要提交。)
    • 六、把项目文件加入准备提交区
    • [七、使用git commit -m ""来提交](#七、使用git commit -m ""来提交)
    • [八、配置Git 作者信息。](#八、配置Git 作者信息。)
    • 九、正式完成第一次提交
    • 十、查看历史版本与当前状态
    • 十:新手如果想恢复和回退版本,该怎么做?
      • [情况 1:还没提交,只是想撤销当前修改](#情况 1:还没提交,只是想撤销当前修改)
      • [情况 2:已经提交过多个版本,想恢复到某次旧提交](#情况 2:已经提交过多个版本,想恢复到某次旧提交)
      • [情况 3:强制回退到某次提交(硬回退)](#情况 3:强制回退到某次提交(硬回退))
  • [后记:复盘新手需要掌握的git命令及 Git 工作流](#后记:复盘新手需要掌握的git命令及 Git 工作流)
    • [1. 当项目第一次稳定跑起来时](#1. 当项目第一次稳定跑起来时)
    • [2. 每次完成一个阶段性成果时](#2. 每次完成一个阶段性成果时)
    • [3. 不确定的时候,先看状态](#3. 不确定的时候,先看状态)
    • [4. 想看项目历史时](#4. 想看项目历史时)
    • [5. 版本回退:](#5. 版本回退:)
      • [5.1 还没提交,只是想撤销当前修改](#5.1 还没提交,只是想撤销当前修改)
      • [5.2 已经提交过,想恢复到历史某个版本](#5.2 已经提交过,想恢复到历史某个版本)

前言

最近我在vibe coding做一个小游戏项目。但项目跑起来以后,我再继续修改就开始担心:

如果我继续修改项目,万一改坏了,怎么回到之前的版本?

这时候我才真正开始学习 Git

所以这篇文章,我直接结合我自己的真实操作过程,写一篇适合新手入门的 Git 最小实践教程

一、Git 到底是什么?

如果你是新手,我建议先把它理解成一句更朴素的话:

Git 是一个给项目存档的工具。

比如你现在有一个小游戏项目:

  • 当前项目很满意,可以先存一个档
  • 后面继续修改,如果改坏了,可以回到之前那个档
  • 修改了很多次,也可以查看每次修改的历史记录

特别现在大家都用 AI 编程、经常遇到以下场景:

  • vibe coding
  • AI 帮你频繁改代码
  • 前端页面反复调整
  • 小项目快速迭代
  • 恢复旧版本需求强烈

越让 AI 帮自己频繁改代码,就越需要使用Git,因为:AI 改得越快,你越需要一个稳定的版本回退机制。

二、新手一开始最需要掌握哪些 Git 指令?

新手一开始只需要掌握下面这些指令:

bash 复制代码
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

git init
git status
git add .
git commit -m "..."
git log --oneline

以及一个非常重要的文件:

text 复制代码
.gitignore

三、这些指令的作用:

  1. git config --global user.name / user.email 作用:配置 Git 提交作者信息

    • Git 每次提交时都会记录作者名字和邮箱。如果没有设置,第一次提交时通常会失败。
  2. git init在当前项目目录下初始化 Git 仓库。(可以把它理解成从这一刻开始,把这个项目交给 Git 管理。)

  3. git status查看当前项目状态 。它会告诉你:

    • 哪些文件是新文件
    • 哪些文件修改过
    • 哪些文件已经加入准备提交区
    • 当前工作区是否干净
  4. .gitignore文件:告诉 Git 哪些文件或目录不要纳入版本管理 (不然每次版本控制,版本备份的内容都太多太大了)。常见不应该提交的内容包括:

    • 系统垃圾文件
    • 缓存文件
    • 构建产物
    • 本机配置
    • 日志
    • 私密配置文件
  5. git add .: 把当前项目中需要提交的改动,加入"准备提交区" 。(这一步还不是正式保存,只是告诉 Git:这些内容,这次我要提交。)

  6. git commit -m "...":正式保存一个版本,并写下这次提交说明 。例如:
    *

    bash 复制代码
      git commit -m "小游戏初始版本"
    • 意思:我把当前项目状态保存成一个正式版本,名字叫"小游戏初始版本"。'''
  7. git log --oneline:作用:查看提交历史 。它可以帮助你快速看到:

    • 提交编号
    • 提交说明
    • 项目历史演化过程

四、案例:我的小游戏接入 Git管理的案例流程

1. 先确认 Git 已经安装成功

python 复制代码
git --version

'''
# 输出结果
git version 2.53.0.windows.3
'''

说明 Git 已经安装完成,可以正常使用。


2. 进入项目目录

我进入项目所在目录:

python 复制代码
cd D:\coding\codex
pwd

'''
# 输出结果
D:\coding\codex
'''

这一步的意义在于:

Git 的初始化、提交、查看状态,都是针对当前目录进行的。

所以必须先进入正确的项目文件夹。


3. 初始化 Git 仓库

python 复制代码
git init

'''
# 输出
Initialized empty Git repository in D:/coding/codex/.git/
'''

当前项目下新建了隐藏的.git文件夹,说明当前项目已经被 Git 接管了。


4. 查看项目当前状态

python 复制代码
git status

'''
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        GIT_GUIDE.md
        README.md
        RECOVERY_REPORT.md
        android-app/
        app.js
        icons/
        index.html
        manifest.webmanifest
        styles.css
        sw.js

nothing added to commit but untracked files present (use "git add" to track)
'''

输出里出现了很多 Untracked files,比如:

  • README.md
  • RECOVERY_REPORT.md
  • android-app/
  • app.js
  • icons/
  • index.html
  • manifest.webmanifest
  • styles.css
  • sw.js

这代表:

Git 已经看到了这些文件,但它们目前还没有正式纳入版本管理。


五、.gitignore 声明哪些文件不需要提交。

对于一个真实项目来说,不是所有文件都应该提交。所以可以在这个项目下新建一个文件.gitignore,并在该文件下填入哪些文件不需要提交来方便项目管理。其本质就是:

让 Git 只管理真正有价值的项目内容。

我让AI帮我生成了一份 .gitignore,内容如下:

gitignore 复制代码
# OS / editor
.DS_Store
Thumbs.db

# Python local caches
__pycache__/
*.pyc

# Android Studio / Gradle local files
android-app/.gradle/
android-app/.idea/
android-app/build/
android-app/app/build/
android-app/local.properties

# Logs
*.log

在这个声明里,也就是文件内容里,git帮我排除了哪些不需要提交的内容:

  • 系统文件
  • Python 缓存
  • Android 本地构建目录
  • 本机配置
  • 日志文件

事实上:

  1. 适合git提交的内容:
    • 代码文件
    • 配置文件
    • 资源文件
    • 项目说明文档
    • 真正属于项目本身的目录结构
  2. 不适合提交的内容:
    • 缓存
    • 日志
    • 自动生成文件
    • 本地配置
    • 私密环境变量
    • 临时文件

六、把项目文件加入准备提交区

.gitignore 准备好之后,运行:

python 复制代码
git add .

'''
# 输出内容
warning: in the working copy of '.gitignore', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'GIT_GUIDE.md', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'RECOVERY_REPORT.md', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/README.md', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/build.gradle', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/proguard-rules.pro', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/AndroidManifest.xml', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/assets/www/app.js', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/assets/www/index.html', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/assets/www/manifest.webmanifest', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/assets/www/styles.css', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/assets/www/sw.js', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/java/com/switchrush/app/MainActivity.kt', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/res/values/strings.xml', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/app/src/main/res/values/themes.xml', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/build.gradle', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/gradle.properties', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/gradle/wrapper/gradle-wrapper.properties', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/gradlew', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'android-app/settings.gradle', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'app.js', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'index.html', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'manifest.webmanifest', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'styles.css', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'sw.js', LF will be replaced by CRLF the next time Git touches it
'''

这一步的意思是:

把当前目录下这次要提交的内容全部加入准备提交区。

然后我再次运行git status命令检查一下:

python 复制代码
git status

'''
# 输出内容:
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
        new file:   GIT_GUIDE.md
        new file:   README.md
        new file:   RECOVERY_REPORT.md
        new file:   android-app/README.md
        new file:   android-app/app/build.gradle
        new file:   android-app/app/proguard-rules.pro
        new file:   android-app/app/src/main/AndroidManifest.xml
        new file:   android-app/app/src/main/assets/www/app.js
        new file:   android-app/app/src/main/assets/www/icons/icon-192.png
        new file:   android-app/app/src/main/assets/www/icons/icon-512.png
        new file:   android-app/app/src/main/assets/www/index.html
        new file:   android-app/app/src/main/assets/www/manifest.webmanifest
        new file:   android-app/app/src/main/assets/www/styles.css
        new file:   android-app/app/src/main/assets/www/sw.js
        new file:   android-app/app/src/main/java/com/switchrush/app/MainActivity.kt
        new file:   android-app/app/src/main/res/drawable/app_icon.png
        new file:   android-app/app/src/main/res/values/strings.xml
        new file:   android-app/app/src/main/res/values/themes.xml
        new file:   android-app/build.gradle
        new file:   android-app/gradle.properties
        new file:   android-app/gradle/wrapper/gradle-wrapper.jar
        new file:   android-app/gradle/wrapper/gradle-wrapper.properties
        new file:   android-app/gradlew
        new file:   android-app/gradlew.bat
        new file:   android-app/settings.gradle
        new file:   app.js
        new file:   icons/icon-192.png
        new file:   icons/icon-512.png
        new file:   index.html
        new file:   manifest.webmanifest
        new file:   styles.css
        new file:   sw.js

'''

这时显示:

text 复制代码
Changes to be committed:

并列出了一系列 new file

这意味着:

当前这些文件已经进入了"准备提交区"。


七、使用git commit -m ""来提交

运行:

python 复制代码
git commit -m "小游戏初始版本"

'''
# 输出内容:
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '81001@��ѡ4-i3620h-4060.(none)')
'''

报错:

text 复制代码
Author identity unknown

git需要每次提交的时候命名一下提交的作者和相关信息,这个错误的意思是:git不知道这次提交是谁做的。

八、配置Git 作者信息。

因为之前没配置Git作者信息,导致第一次提交失败,所以,我继续执行:

bash 复制代码
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

然后检查配置:

bash 复制代码
git config --global --list

得到结果:

text 复制代码
user.name=你的名字
user.email=你的邮箱

这说明 Git 作者信息已经配置成功。


九、正式完成第一次提交

之后我重新执行:

python 复制代码
git commit -m "小游戏初始版本"

'''
# 输出内容:
[main (root-commit) 85a5f91] 小游戏初始版本
 33 files changed, 3056 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 GIT_GUIDE.md
 create mode 100644 README.md
 create mode 100644 RECOVERY_REPORT.md
 create mode 100644 android-app/README.md
 create mode 100644 android-app/app/build.gradle
 create mode 100644 android-app/app/proguard-rules.pro
 create mode 100644 android-app/app/src/main/AndroidManifest.xml
 create mode 100644 android-app/app/src/main/assets/www/app.js
 create mode 100644 android-app/app/src/main/assets/www/icons/icon-192.png
 create mode 100644 android-app/app/src/main/assets/www/icons/icon-512.png
 create mode 100644 android-app/app/src/main/assets/www/index.html
 create mode 100644 android-app/app/src/main/assets/www/manifest.webmanifest
 create mode 100644 android-app/app/src/main/assets/www/styles.css
 create mode 100644 android-app/app/src/main/assets/www/sw.js
 create mode 100644 android-app/app/src/main/java/com/switchrush/app/MainActivity.kt
 create mode 100644 android-app/app/src/main/res/drawable/app_icon.png
 create mode 100644 android-app/app/src/main/res/values/strings.xml
 create mode 100644 android-app/app/src/main/res/values/themes.xml
 create mode 100644 android-app/build.gradle
 create mode 100644 android-app/gradle.properties
 create mode 100644 android-app/gradle/wrapper/gradle-wrapper.jar
 create mode 100644 android-app/gradle/wrapper/gradle-wrapper.properties
 create mode 100644 android-app/gradlew
 create mode 100644 android-app/gradlew.bat
 create mode 100644 android-app/settings.gradle
 create mode 100644 app.js
 create mode 100644 icons/icon-192.png
 create mode 100644 icons/icon-512.png
 create mode 100644 index.html
 create mode 100644 manifest.webmanifest
 create mode 100644 styles.css
 create mode 100644 sw.js
'''

这次成功完成第一次提交,输出:

text 复制代码
[main (root-commit) 85a5f91] 小游戏初始版本

说明:

  • 项目完成了第一次提交
  • 提交编号是 85a5f91
  • 当前项目已经有了一个正式版本

十、查看历史版本与当前状态

提交成功后,我运行:

python 复制代码
git log --oneline

'''
# 输出内容:
85a5f91 (HEAD -> main) 小游戏初始版本
'''

再运行:

python 复制代码
git status

'''
# 输出:
On branch main
nothing to commit, working tree clean
'''

表示:

  • 当前没有未保存修改
  • 工作区很干净
  • 当前版本已经被成功保存

十:新手如果想恢复和回退版本,该怎么做?

情况 1:还没提交,只是想撤销当前修改

如果你刚改了一堆内容,但还没有 commit,这时想撤销修改,可以运行:

bash 复制代码
git restore .

它的含义是:

把当前工作区的未提交修改全部恢复到上一次提交后的状态。

适合这种场景:

  • AI 改完你不满意
  • 你自己试错失败了
  • 只是想回到最近一次提交

情况 2:已经提交过多个版本,想恢复到某次旧提交

先查看历史:

bash 复制代码
git log --oneline

例如看到:

text 复制代码
d444444 修复手机端布局
c333333 修改按钮样式
b222222 缩小游戏界面
85a5f91 小游戏初始版本

如果你想恢复到 85a5f91 对应的版本,可以这样做:

bash 复制代码
git restore --source 85a5f91 -- .
git add .
git commit -m "恢复到 小游戏初始版本"

这个其实不是真回退,而是生成了一次新的恢复提交,这不会破坏历史。


情况 3:强制回退到某次提交(硬回退)

例如:

bash 复制代码
git reset --hard 85a5f91

这条命令有些危险,会直接把项目强制退回到那个版本,可能让你丢失当前未保存的修改。


后记:复盘新手需要掌握的git命令及 Git 工作流

新手一开始只需要掌握下述命令,足以覆盖大多数日常开发场景:

  • git init:给项目开通存档系统
  • git status:看当前状态
  • .gitignore:排除不该进版本管理的内容
  • git add .:把这次改动放到准备区
  • git commit -m "...":正式存档
  • git log --oneline:查看历史
  • git restore:恢复版本

之后参考如下的最小工作流即可。

1. 当项目第一次稳定跑起来时

先初始化仓库并保存一个版本:

python 复制代码
git init
git status
'''
- 然后项目下生成一个 .gitignore 文件,把那些不应该进入版本管理的内容排除掉。
- .gitignore 文件的作用:告诉 Git哪些内容不需要存档,不要把它们提交进去。
'''
git add .
git commit -m "项目初始版本"

2. 每次完成一个阶段性成果时

例如:

  • 页面能跑了
  • 游戏核心功能实现了
  • 手机端适配完成了
  • 一个 bug 修好了

都应该保存一次版本:

bash 复制代码
git status
git add .
git commit -m "写清楚这次完成了什么"

例如:

bash 复制代码
git commit -m "完成首页布局"
git commit -m "实现小游戏核心逻辑"
git commit -m "修复手机端按钮错位"

3. 不确定的时候,先看状态

这是新手最值得养成的习惯。(因为新手最容易慌的时候,其实就是"我现在到底改了什么,我自己也不清楚"。)

bash 复制代码
git status

它能帮你快速确认:

  • 哪些文件改过
  • 哪些文件还没加入暂存区
  • 哪些文件已经准备提交
  • 当前工作区是否干净

4. 想看项目历史时

bash 复制代码
git log --oneline

它会帮你快速看到:

  • 每次提交的编号
  • 每次提交的说明
  • 项目是怎么一步一步变成现在这样的

这也是 Git 很有价值的一点:它不只是"备份",还记录你整个项目的演进。


5. 版本回退:

5.1 还没提交,只是想撤销当前修改

bash 复制代码
git restore .

适合场景:

  • AI 刚改完,但效果很差
  • 自己试错之后想全部撤回
  • 想恢复到最近一次提交后的状态

5.2 已经提交过,想恢复到历史某个版本

先看历史:

bash 复制代码
git log --oneline

再找到你想回去的那个版本,进行恢复。

如果你想保留历史记录,比较推荐这种做法:

bash 复制代码
git restore --source 提交编号 -- .
git add .
git commit -m "恢复到某个旧版本"

这其实就相当于又新提交了一次。

如果你想强制直接回到旧版本,也可以用:

bash 复制代码
git reset --hard 提交编号

硬回退强制回退某个版本,比较危险,使用时谨慎一些。


相关推荐
DianSan_ERP2 小时前
淘宝订单接口集成中如何正确处理消费者敏感信息的安全与合规问题?
大数据·运维·网络·人工智能·安全·servlet
iiiiyu2 小时前
常用API(StringJoiner类 & Math类 & System类)
java·大数据·开发语言·数据结构·编程语言
rayyy93 小时前
Git 忽略已提交过的文件夹 完整步骤
git
常宇杏起3 小时前
AI安全进阶:AI系统日志审计与安全监控技巧
大数据·人工智能·安全
2501_948114243 小时前
星链4SAPI中转枢纽深度技术解构:架构优势、工程实践与演进脉络
大数据·人工智能·ai·架构
YoseZang3 小时前
【手工】git的使用 - 密钥生成和多账户使用(config文件)
git
韭菜钟3 小时前
Git 代理与内网 Gitea 共存方案(无需 no_proxy)
git·gitea
赞奇科技Xsuperzone3 小时前
零售行业桌面端算力升级方案(含最新GPU选型指南)
大数据·人工智能·零售
AniShort3 小时前
从单兵作战到工业化量产!AniShort重构AI短剧生产革命
大数据·人工智能·重构