Git基础04 — 文件状态的相关基础命令

前言

前面我们说所谓的 版本控制,其实指对文件历史版本修改的控制,因此了解文件的状态非常重要。

在 Git 中,有这么几种文件状态:

  • Untracked:未跟踪状态。此文件在项目目录中但并没有被添加到 git 库,通过 git add 命令可将其状态变为 staged
  • Unmodified:文件已经添加到了 git 库但文件内容未修改。如果文件内容被修改了,则状态变为 Modified。
  • Modified:文件已经修改。可使用 git restore撤销所做的修改。可以将新修改的文件通过 git add 将状态变为 Staged 并放入到暂存区,如果你要提交到本地库,直接使用 git commit
  • Staged:暂存状态,可使用 git commit 将其提交到本地库中,提交成功后,文件状态变为 Unmodified。对于全新添加的文件,可使用 git rm --cached 命令将文件恢复到未跟踪的状态。

命令说明

  • git add – 将新文件或对已有文件的修改保存到暂存区
  • git status – 查看当前项目目录中文件的状态
  • git commit – 提交到本地库
  • git rm – 将文件从工作区或暂存区里删除
  • git log – 查询除版本回退以外的相关日志内容

文件状态实验

在前面的文章,我在 E 盘符中新建 git-test 目录并让其初始化为 git 项目。我们将在这个项目目录上进行实验。

新添加的文件初始状态是 Untracked ,如下:

PS > cd E:\git-test\

PS > new-Item tmp-file.txt

    Directory: E:\git-test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            2025/3/1    10:48              0 tmp-file.txt

PS > git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        tmp-file.txt

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

如输出文本信息所示,我们当前在 master 分支上,文件状态为 Untracked。

将文件保存到暂存区并使文件状态变更为 Staged:

PS > git add ./

PS > git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   tmp-file.txt

在 GNU/Linux 和 Windows 中有两个引用符号:

  • . – 表示引用当前目录本身
  • .. – 表示引用当前目录的上一级目录

此时文件状态为 Staged,根据需要,您可以使用 git rm --cached 将文件状态变更为 Untracked,也可以使用 git commit 将所做的修改提交到本地库。

PS > git commit -m "测试文件"
[master (root-commit) 9a70355] 测试文件
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tmp-file.txt

PS > git status
On branch master
nothing to commit, working tree clean

上面的步骤中,commit 之后,文件提交到了本地库,状态为 Unmodified,一旦做了修改,则状态变更为 modified。这种状态下,您可以使用 git add 将所作的修改保存到暂存区,也可以撤销所做的修改,如下:

# 写入文本内容
PS > echo "test" > E:\git-test\tmp-file.txt

# 查看文件的内容
PS > cat E:\git-test\tmp-file.txt
test

PS > git status
On branch master
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:   tmp-file.txt

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

PS > git restore ./

PS > git status
On branch master
nothing to commit, working tree clean

# 文件内容空白
PS > cat E:\git-test\tmp-file.txt
信息
在 Windows 中,既可以用 “/” 正斜杠表示路径,也可以用 “\” 反斜杠表示路径
问题
本地仓库放在什么地方? 在项目目录的 .git 隐藏目录下

修改最后一次提交

有时候我们 commit 提交完后才发现漏掉了几个文件没有添加到暂存区,或者是提交信息写错了。 此时,可以运行带有 --amend 选项的提交命令来重新提交,比如:

Shell > git commit --amend

对于提交信息而言,如果自上次提交以来你还未做任何修改(例如,在上次提交后马上执行了此命令), 那么快照会保持不变,而你所修改的只是提交信息。

对于遗漏的文件而言,比如第一次 commit 后发现遗漏了某些文件,可以像下面这样操作:

Shell > git commit -m 'initial commit'

Shell > git add forgotten_file

Shell > git commit --amend

最终你只会有一个提交——第二次提交将代替第一次提交的结果。

删除相关

Shell > git rm <File Name>

将文件从工作区或暂存区里删除。如果只是删除暂存区的文件,则使用 git rm --cached <File Name>。使用 -r 选项表示递归删除

文件重命名

在项目目录中,我们并不是直接使用 GNU/Linux 或 Windows PowerShell 中的 mv 命令进行改名,而是使用 git mv 命令。

正确的文件重命名:

PS > cd E:\git-test\

PS > ls

    Directory: E:\git-test

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            2025/3/1    11:24              0 tmp-file.txt

PS > echo "First Line" > .\tmp-file.txt

PS > git add ./

PS > git commit -m "First commit"
[master ae6cc9d] First commit
 1 file changed, 1 insertion(+)

PS > git mv .\tmp-file.txt .\Tmp-File.txt

PS > git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        renamed:    tmp-file.txt -> Tmp-File.txt

# 查看状态可知,文件目前存放在暂存区,提交到本地库即可
PS > git commit -m "将tmp-file.txt改名为 Tmp-File.txt"

修改最后一次提交的信息:

PS > git commit --amend -m "tmp-file.txt --> Tmp-File.txt"

PS > git log --oneline
47d9368 (HEAD -> master) tmp-file.txt --> Tmp-File.txt
ae6cc9d First commit
9a70355 测试文件
Avatar photo

关于 陸風睿

GNU/Linux 从业者、开源爱好者、技术钻研者,撰写文档既是兴趣也是工作内容之一。Q - "281957576";WeChat - "jiulongxiaotianci",Github - https://github.com/jimcat8
用一杯咖啡支持我们,我们的每一篇[文档]都经过实际操作和精心打磨,而不是简单地从网上复制粘贴。期间投入了大量心血,只为能够真正帮助到您。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇