让 git 检查工作目录的完整性

UTF*_*F-8 5 git

git 有没有内置的方式*来检查工作目录中的文件是否神奇地**改变了***?

如果是这样:如何让 git 检查工作目录的完整性?

* 找出工作目录中的哪些文件被跟踪,只对它们进行散列,删除它们,将它们检出,再一次,再散列它们,并比较散列值并不完全算作“内置”。

** 由于宇宙射线击中硬盘、独角兽在盘子上疾驰或其他不让 FS 知道发生了变化的方式(例如编辑角色设备),文件内容发生了变化。

*** 在名称、内容或其他跟踪属性方面(例如神奇地变为可执行文件)。


请注意,这个问题是另一个问题的弱化版本。回答这个问题(您当前正在阅读的问题)更容易。如果这个问题得到解决,我将在另一个问题中链接到这个问题,并指出这个问题下面的答案没有解决的另一个问题的部分。

有人问git status我和我要求的有什么区别。git status只有当 FS 告诉它发生了变化时才会注意到变化。如果文件被神奇地损坏,FS 不知道,因此git status不会指示更改。git diffgit add --all等当然也不会。


响应使用的建议git fsck:它不起作用。

我做了什么:

christoph@christoph-laptop-16-04-2:/t$ dd if=/dev/zero bs=1M count=30 of=/tmp/con
30+0 records in
30+0 records out
31457280 bytes (31 MB, 30 MiB) copied, 0.0143967 s, 2.2 GB/s
christoph@christoph-laptop-16-04-2:/t$ mkfs.ext4 /tmp/con
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done                            
Creating filesystem with 30720 1k blocks and 7680 inodes
Filesystem UUID: 9865efe8-fb30-42ab-ace7-a8f88330bdfd
Superblock backups stored on blocks: 
    8193, 24577

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

christoph@christoph-laptop-16-04-2:/t$ sudo mount -t ext4 -o loop /tmp/con /mnt
[sudo] password for christoph: 
christoph@christoph-laptop-16-04-2:/t$ cd /mnt
christoph@christoph-laptop-16-04-2:/mnt$ sudo chown christoph .
christoph@christoph-laptop-16-04-2:/mnt$ git init
Initialized empty Git repository in /mnt/.git/
christoph@christoph-laptop-16-04-2:/mnt$ echo "some contents" > file
christoph@christoph-laptop-16-04-2:/mnt$ git add file 
christoph@christoph-laptop-16-04-2:/mnt$ git commit -m "a"
[master (root-commit) d29fbd5] a
 1 file changed, 1 insertion(+)
 create mode 100644 file
christoph@christoph-laptop-16-04-2:/mnt$ git status
On branch master
nothing to commit, working directory clean
christoph@christoph-laptop-16-04-2:/mnt$ cd ~
christoph@christoph-laptop-16-04-2:~$ sudo umount /mnt
Run Code Online (Sandbox Code Playgroud)

此时,我/tmp/con在我最喜欢的十六进制编辑器中打开,搜索单词“contents”,然后将最后的“s”替换为“z”。

christoph@christoph-laptop-16-04-2:~$ sudo mount -t ext4 -o loop /tmp/con /mnt
christoph@christoph-laptop-16-04-2:~$ cd /mnt
christoph@christoph-laptop-16-04-2:/mnt$ git status
On branch master
nothing to commit, working directory clean
christoph@christoph-laptop-16-04-2:/mnt$ cat file 
some contentz
christoph@christoph-laptop-16-04-2:/mnt$ git fsck
Checking object directories: 100% (256/256), done.
christoph@christoph-laptop-16-04-2:/mnt$ git status
On branch master
nothing to commit, working directory clean
christoph@christoph-laptop-16-04-2:/mnt$ cat file
some contentz
Run Code Online (Sandbox Code Playgroud)

有人建议我接触工作目录中的所有文件。更改他们的访问日期不会有太大问题,但这不会使 git 重新检查文件。更改它们的修改日期实际上确实让 git 再次检查这些文件,但是,它也会导致其他问题:备份应用程序再次备份文件。对于大型存储库,这可能是一个问题。

dav*_*pcj 1

我正在阅读另一个问题的答案,关于如何让 git在 github 上确认您的新行结束首选项,其中包括以下宝石:

  1. 删除索引并强制 Git 重新扫描工作目录。

    rm .git/index

  2. 重写 Git 索引以获取所有新行结尾。

    git reset

  3. 显示重写的标准化文件。

    git status

它建议保存您当前的状态,以防您有其他关心的、可能相关的事情。

特别是因为 git 重置步骤将重写文件。