Git | 文件提交操作

概述

  • Git 仓库可以分为工作区 、版本库 、暂存区三大部分,其功能和位置如下:
名称 目录 说明
工作区 .../main 直接编辑文件的可见目录(即本地计算机上的项目目录,但不包括 .git 目录),用户在这里进行文件的创建、修改和删除操作
暂存区 .../main/.git/index 临时存储 git add (添加操作)后的变更 可以多次将文件添加到暂存区,直到准备好提交所有更改
版本库 .../main/.git 永久存储 git commit (提交操作)后的内容 包含项目的所有提交的版本历史记录。
  • git add 添加更改时 ------ 实际上把 对文件的修改记录 添加到 暂存区
  • git commit 提交更改时 ------ 实际上是把 暂存区的所有内容 提交到 当前分支

"把需要提交的文件修改首先放到暂存区,最后一次性把暂存区的所有修改内容提交到当前分支"


指令介绍

添加文件 git add
  • 语法

    sh 复制代码
    git add [-options] <文件或目录路径>
    参数 options 说明
    -A 或 --all 或 . 添加所有改动(包括新增、修改、删除的文件)
    -u 或 --update 仅添加已跟踪文件的修改(不包含新增文件)
    -p 或 --patch 交互式选择部分修改,可逐块确认是否暂存
    -f 或 --force 强制添加被 .gitignore 忽略的文件
    -i 或 --interactive 进入交互模式,手动选择操作
  • 示例

    • 一次添加单个文件:

      sh 复制代码
      git add readme.txt
    • 一次添加多个文件

      sh 复制代码
      git add file1.txt file2.txt
    • 一次添加所有改动

      sh 复制代码
      git add -A
      git add .
    • 仅添加已跟踪文件的修改(不包含新增文件)

      sh 复制代码
      git add -u
    • 交互式选择部分修改进行添加

      sh 复制代码
      $ git add -p
      diff --git a/readme.txt b/readme.txt
      index 1234567..89abcde 100644
      --- a/readme.txt
      +++ b/readme.txt
      @@ -1 +1,2 @@
       Git is a version control system.
      +Git is free software.
      Stage this hunk [y,n,q,a,d,s,e,?]? y

      操作选项:

      选项 含义 选项 含义
      y 暂存当前块 q 退出
      n 不暂存 ? 查看帮助
提交文件 git commit
  • 语法

    sh 复制代码
    git commit [-options] "commitMsg"
    参数 options 说明
    -m "commitMsg" 直接附加提交信息(必须填写)
    -a 或 --all 跳过 git add,自动提交所有已跟踪文件的修改(不包含新增文件)
    --amend 修改最近一次提交(可更改提交信息或追加文件)
    -v 或 --verbose 提交时显示详细的 Diff 变更信息
    --no-verify 绕过 Git Hooks(如 pre-commit 检查)
  • 示例

    • 基本提交

      sh 复制代码
      $ git add file.txt          				# 暂存修改
      $ git commit -m "fix Bug"				# 提交并附加信息
      [main 1a2b3c4] fix Bug
      1 file changed, 2 insertions(+), 1 deletion(-)
    • 跳过 git add,直接提交已跟踪文件的修改

      sh 复制代码
      $ git commit -am "docs: update README"

      适用场景 :仅提交已跟踪文件的修改,等同于 git add -u + git commit -m

    • 修改最近一次提交

      sh 复制代码
      git add forgotten_file.txt				# 添加漏掉的文件
      git commit --amend -m "new commit msg"	 	# 替换上次提交
查询状态 git status
  • 语法

    sh 复制代码
    git status [<-options>]
    参数 options 说明
    --long 详细格式(默认)
    -s 或 --short 简洁格式
    -b 或 --branch 显示分支信息(包含本地/远程分支关联状态)
    --show-stash 同时显示 stash 中的变更记录
    -- <pathspec> 显示指定路径下的状态
  • 示例

    sh 复制代码
    $ git status
    On branch main		# 在分支 main 上
    Your branch is up to date with 'origin/main'.
    
    # 已暂存的更改(待提交commit)
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   README.md
    
    # 已修改+未暂存的更改(待添加add)
    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
    
    # 新文件,尚未被 Git 跟踪(待添加add)
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            new-file.txt
    • Changes not staged for commit ------ 已修改但未暂存的更改

    • Changes to be committed ------ 已暂存的更改(在索引中)

    • Untracked files ------ 新文件,尚未被 Git 跟踪


添加文件到暂存区

相关指令
  • git add------ 工作区文件添加到暂存区

  • git status ------ 查看当前状态

实操
  • 编写文件 ------ 在创建好的 Git 项目下,编写一个 readme.txt 文件,内容如下:

    txt 复制代码
    Hello word!
  • 添加文件 ------ 输入命令 git add 添加文件,没有输出任何内容即代表提交成功

    sh 复制代码
    $ git add readme.txt
  • 查看状态 ------ 输入命令 git status 查看当前仓库状态

    sh 复制代码
    $ git status
    On branch master
    # 尚无任何提交记录
    No commits yet
    
    # readme.txt 被添加
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   readme.txt
  • 修改文件 ------ 修改 readme.txt,新增 LICENSE.txt 文件,通过 git status 查看状态

    sh 复制代码
    $ git status
    On branch master	# 在分支 master 上
    No commits yet		# 尚无提交记录
    
    # readme.txt 被添加
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   readme.txt
            
    # readme.txt 被修改
    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:   readme.txt
            
    # LICENSE.txt 未被添加
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
            LICENSE.txt
  • 添加更改 ------ 通过命令 git add 一次添加两个文件,通过 git status 查看状态

    sh 复制代码
    $ git add readme.txt LICENSE.txt
    sh 复制代码
    $ git status
    On branch master	# 在分支 master 上
    No commits yet		# 尚无提交记录
    
    # readme.txt的修改 和 LICENSE.txt的创建 均被添加到暂存区
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   LICENSE.txt
            new file:   readme.txt
  • 仓库状态 ------ 当前仓库状态如下,通过 git add 操作已成功把两个文件内容添加到暂存区,等待提交到分支上


提交文件到分支上

相关指令
  • git commit------ 把暂存区文件提交到本地仓库的分支上
实操
  • 提交更改 ------ 通过命令 git commit 提交更改,通过 git status 查看状态

    sh 复制代码
    $ git commit -m "understand how stage works"
    [master (root-commit) 1d25fee] understand how stage works
     2 files changed, 2 insertions(+)
     create mode 100644 LICENSE.txt
     create mode 100644 readme.txt
    sh 复制代码
    $ git status
    On branch master
    nothing to commit, working tree clean	# 提交后,工作区变为"干净"的

    可以看到,文件已经被提交成功,同时仓库的工作区变得"干净",即没有尚未提交的内容

  • 仓库状态 ------ 当前仓库状态如下,通过 git commit 操作已成功把两个文件内容提交到当前的主分支 master 上


相关推荐
u1301302 分钟前
GitHub 热榜项目:月榜(2026-09-30)
人工智能·github
m4Rk_2 小时前
【论文阅读】Agent 记忆机制(86):Skill-Pro——用 Non-Parametric PPO 将交互经验演化为可复用技能
论文阅读·人工智能·学习·开源·github
别动我齐刘海2 小时前
简历技术栈全面复习——UDP / TCP / CAN / ZMQ / Protobuf 通信工程
网络·c++·python·tcp/ip·机器学习·udp·github
data analyse 4563 小时前
埋点工具的私有化部署成本高吗?
前端·数据分析·github
miofly6 小时前
GitHub 今日推荐|company-brain:在 Slack 里给团队装个会主动干活的 AI
开源·github
用户3831703705410 小时前
PowerFS 是用 Rust 写的、面向 AI/GPU 训练的下一代分布式文件系统
github
钮钴禄·爱因斯晨10 小时前
用iPhone操作Codex CLI:ToDesk远程终端让我接回了开发环境
github
miofly12 小时前
GitHub 日榜趋势速报 | 2026-10-01
开源·github
Flynt13 小时前
Claude Code 103 秒删掉 4.8 万个文件之后,我把自己的仓库"删"了一遍:git 能救的比你想的少
git·ai编程·claude
阿里嘎多学长13 小时前
2026-09-30 GitHub 热点项目精选
开发语言·程序员·github·代码托管