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


相关推荐
CoderJia程序员甲2 小时前
GitHub 热榜项目 - 日榜(2025-12-25)
git·ai·开源·llm·github
A13247053123 小时前
进程管理入门:查看和控制Linux进程
linux·运维·服务器·网络·chrome·github
-拟墨画扇-3 小时前
Git | 文件修改操作
大数据·git·gitee·github·gitcode
-拟墨画扇-3 小时前
Git | 版本控制操作
大数据·git·gitee·github
一条闲鱼_mytube3 小时前
GitHub K8S CI/CD 部署指南
github
lyx_20164 小时前
尝试理清楚Github fork, tag, release和PR
github
nono牛4 小时前
MTK平台Android init.rc服务详解实例
android·gitee
GA6666665 小时前
PowerWiki:基于 Git 的知识管理系统
git
睡觉待开机5 小时前
vscode+gitee+picgo实现稳定图床教程
ide·vscode·gitee