git-状态管理untracked、unmodified、modified、Staged

1、基础知识

1.1、状态简介

代码仓库对文件的管理主要是通过文件状态信息来进行管理的。git工具在对文件进行版本管理的过程中,文件主要有以下几种状态:untracked、unmodified、modified、Staged

1.2、文件状态变化图 

1.3、状态解析

1.3.1、未跟踪(untracked)

未被追踪的状态,也就是说,该文件是第一次放到本地工作目录中,这时文件和本地工作目录没有任何关系,所以该文件处于未被追踪的文件。也就是说文件跟代码仓库没有关系,没有纳入的到代码版本管理的序列中,所以说虽然文件在目录中,但是还不能称为它在我们的版本管理的工作目录里面。
我们通过 git add 命令将文件推送到暂存区域中,文件状态从untracked变为staged

1.3.2、未修改(unmodified)

未被修改的状态,将文件从暂存区域拉到本地仓库中

1.3.3、已修改(modified)

对本地仓库中的未被修改的文件进行编辑,就会将unmodified 状态的文件重新拉回到工作目录区域中,文件状态由staged变为 modified

1.3.4、已暂存(Staged)

暂存区域中的 statged 状态的文件,通过 git commit 命令将文件提交到本地仓库,状态变更为unmodified

2、实践

2.1、查看状态

要确定本地仓库文件当前状态,可以用 git status 命令。
# cd /data/git
# git status
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
结果显示:
 当前目录下没有任何文件,而且当前的git状态表示:我们当前处于默认的master的分支,暂时没有任何要提交的意味
 
注意:
 该命令必须在代码仓库(包含.git)目录下执行

2.2、Untracked状态示例

2.2.1、增加一个文件示例

# 增加一个文件
# echo "first file" > first.txt
# git status
On branch master
Initial commit
Untracked files:
 (use "git add <file>..." to include in what will be committed)
 first.txt
nothing added to commit but untracked files present (use "git add" to track)
可以看到:first.txt文件名出现在了 "Untracked files"信息部分,说明它处于Untracked状态,该状态表明git并未对该文件进行版本控制,最后提示我们可以使用 git add 命令将该文件添加到版本控制。

2.2.2、从暂存区删除提交的文件示例

echo "rm cache file" > cache.txt
git add cache.txt
git status
git rm --cache cache.txt
git status


# 增加暂缓区前
root@localhost:/data/git# git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   cache.txt

# 删除缓冲区后
root@localhost:/data/git# git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        cache.txt

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

结果显示:我们增加一个新的文件,使用add命令将文件转换成Staged状态,然后执行 git rm --cache cache.txt,文件状态又变回了untrack的状态

2.3、Staged状态示例

2.3.1、文件增加至暂存区 、移动暂存区

# 根据上条命令的信息显示,我们可以使用 git add 命令添加文件到git版本控制,命令如下
git add first.txt

注意:
 git add 后面应该是文件|目录路径。如果是目录,表示该目录下所有文件都被追踪

# 检查文件状态
# git status
On branch master
No commits yet
Changes to be committed:
 (use "git rm --cached <file>..." to unstage)
 new file:   first.txt
 结果显示:
"Changes to be committed"部分信息显示,就表明是 first.txt 文件已纳入到版本控制的已暂存区了,new file 就是他的标志,当前的文件状态是 staged(已暂存)。

# 移除暂存区 我们可以使用 git rm
--cached <file> 命令将文件从暂存区删除这个文件,让文件从staged状态重新变成untracked状态。

2.3.2、查看暂存区的一些文件信息

# 我们还可以通过git ls-files --stage 来查看暂存区的一些文件信息
# git ls-files --stage
100644 303ff981c488b812b6215f7db7920dedb3b59d9a 0       first.txt

2.3.3、.git目录的变化

# 提交文件前
root@localhost:/data/git# ll .git/
drwxr-xr-x 7 root root 4096 Jun  9 12:33 ./
drwxr-xr-x 3 root root 4096 Jun  9 12:32 ../
-rw-r--r-- 1 root root   23 Jun  9 10:04 HEAD
drwxr-xr-x 2 root root 4096 Jun  9 10:04 branches/
-rw-r--r-- 1 root root   92 Jun  9 10:04 config
-rw-r--r-- 1 root root   73 Jun  9 10:04 description
drwxr-xr-x 2 root root 4096 Jun  9 10:04 hooks/
drwxr-xr-x 2 root root 4096 Jun  9 10:04 info/
drwxr-xr-x 5 root root 4096 Jun  9 12:33 objects/
drwxr-xr-x 4 root root 4096 Jun  9 10:04 refs/

# 提交文件后
root@localhost:/data/git# ll .git/
drwxr-xr-x 7 root root 4096 Jun  9 12:33 ./
drwxr-xr-x 3 root root 4096 Jun  9 12:32 ../
-rw-r--r-- 1 root root   23 Jun  9 10:04 HEAD
drwxr-xr-x 2 root root 4096 Jun  9 10:04 branches/
-rw-r--r-- 1 root root   92 Jun  9 10:04 config
-rw-r--r-- 1 root root   73 Jun  9 10:04 description
drwxr-xr-x 2 root root 4096 Jun  9 10:04 hooks/
-rw-r--r-- 1 root root  104 Jun  9 12:33 index
drwxr-xr-x 2 root root 4096 Jun  9 10:04 info/
drwxr-xr-x 5 root root 4096 Jun  9 12:33 objects/
drwxr-xr-x 4 root root 4096 Jun  9 10:04 refs/

# 文件对象目录有索引数据
root@localhost:/data/git# tree .git/objects/
.git/objects/
├── 30
│   └── 3ff981c488b812b6215f7db7920dedb3b59d9a
├── info
└── pack

2.3.4、总结

在本地仓库的.git 目录下多了一个index文件,这就是git的索引文件。当我们在工作区中进行了任意的操作之后git都会向这个文件中输入操作信息,当我们用git status查询的时候,就会用index来和版本
库中的最新版本进行比较,以此确定哪些文件是什么状态。
# 检查一下对象文件
# file .git/objects/30/3ff981c488b812b6215f7db7920dedb3b59d9a .git/objects/30/3ff981c488b812b6215f7db7920dedb3b59d9a: VAX COFF executable not stripped - version 15616 # 查看对象文件内容 git cat-file -p .git/objects/30/3ff981c488b812b6215f7db7920dedb3b59d9a

2.4、Unmodified状态示例

2.4.1、commit操作

当暂存区文件的内容确定不会更改,我们就可以使用git commit命令,来提交该文件到本地仓库了。
git commit
-m "first commit" git log
注意:git commit的时候一定要在
-m 参数后面加上注释信息

2.4.2、执行后的解析

root@localhost:/data/git# git commit -a -m "test"
[master (root-commit) 34f12e1] test
 1 file changed, 1 insertion(+)
 create mode 100644 first.txt

root@localhost:/data/git# git log
commit 34f12e17327702e1996f685108464a8102cc0433 (HEAD -> master)
Author: 277667028@qq.com <277668028@qq.com>
Date:   Fri Jun 9 14:01:06 2023 +0000

    test

结果显示:
git commit后,会显示当前是在master分支提交的,本次提交的完整 SHA-1 校验和是39d0004,
以及在本次提交中,文件的编辑情况,其实,每一次commit就是对当前项目做了一次整体快照。

可以看到当前的暂存区已经没有任何文件可以提交了,文件的状态已经变成了 staged状态了,我们的commit操作,生成了一个commit id,下面还有我们提交时候给的注释以及当前用户的信息,执行commit时候的时间点。

2.4.3、.git目录变化

# add文件后
root@localhost:/data/git# ll .git/
drwxr-xr-x 2 root root 4096 Jun  9 13:57 branches/
-rw-r--r-- 1 root root   73 Jun  9 13:57 description
drwxr-xr-x 2 root root 4096 Jun  9 13:57 hooks/
-rw-r--r-- 1 root root  137 Jun  9 14:03 index
drwxr-xr-x 2 root root 4096 Jun  9 13:57 info/
drwxr-xr-x 7 root root 4096 Jun  9 14:01 objects/
drwxr-xr-x 4 root root 4096 Jun  9 13:57 refs/

# commit文件后
root@localhost:/data/git# ll .git/
-rw-r--r-- 1 root root    5 Jun  9 14:01 COMMIT_EDITMSG
-rw-r--r-- 1 root root   23 Jun  9 13:57 HEAD
-rw-r--r-- 1 root root   41 Jun  9 14:01 ORIG_HEAD
drwxr-xr-x 2 root root 4096 Jun  9 13:57 branches/
-rw-r--r-- 1 root root   92 Jun  9 13:57 config
-rw-r--r-- 1 root root   73 Jun  9 13:57 description
drwxr-xr-x 2 root root 4096 Jun  9 13:57 hooks/
-rw-r--r-- 1 root root  137 Jun  9 14:03 index
drwxr-xr-x 2 root root 4096 Jun  9 13:57 info/
drwxr-xr-x 3 root root 4096 Jun  9 14:01 logs/
drwxr-xr-x 7 root root 4096 Jun  9 14:01 objects/
drwxr-xr-x 4 root root 4096 Jun  9 13:57 refs/

# 数据对象
root@localhost:/data/git# ll .git/objects/
drwxr-xr-x 2 root root 4096 Jun  9 14:01 34/
drwxr-xr-x 2 root root 4096 Jun  9 13:59 9d/
drwxr-xr-x 2 root root 4096 Jun  9 14:01 a0/
drwxr-xr-x 2 root root 4096 Jun  9 13:57 info/
drwxr-xr-x 2 root root 4096 Jun  9 13:57 pack/


结果显示:
当commit之后,.git 目录下多了两个文件 COMMIT_EDITMSG 和 logs,具体内容如下:
COMMIT_EDITMSG 保存上一次commit的-m注释信息。
logs 保存commit提交的综合历史记录信息。

root@localhost:/data/git# tree .git/logs/
.git/logs/
├── HEAD       # 记录当前分支的最新commit的综合信息
└── refs       # 最新HEAD信息来源的地方仓库的 refs/heads/master
    └── heads
        └── master

2.5、modified状态示例

# 接下来我们修改一下已经提交过的文件
echo 'change' > first.txt 

# 检查文件状态
# git status
On branch master
Changes not staged for commit:
 (use "git add <file>..." to update what will be committed)
 (use "git checkout -- <file>..." to discard changes in working directory)
 modified:   first.txt
no changes added to commit (use "git add" and/or "git commit -a")

结果显示:
 "Changes not staged for commit"部分信息表示,first.txt 文件刚才被修改了,
 文件的状态从staged状态变成了modified状态,也就是说我们将它从本地仓库拉回到了工作目录,
 但是修改后的文件还没有被放到暂存区,需要暂存的话,再来执行git add命令即可检查修改内容

# 我们可以使用 git diff命令来检查刚才修改了哪些东西
# git diff
diff --git a/first.txt b/first.txt
index 303ff98..0835e4f 100644
--- a/first.txt
+++ b/first.txt
@@ -1 +1 @@
-first file
+change

注意:此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,就是修改后还没有暂存起来的变化内容再次提交文件,将所有修改后的内容保存
git add first.txt
git commit -m "change first"
git log
tree .git/logs/

注意:对暂存后的文件查看改变内容,我们可以使用 git diff --staged|cached 来检查

 

posted @ 2023-06-09 22:44  小粉优化大师  阅读(710)  评论(0编辑  收藏  举报