Git如何检测整个文件夹已删除/移动

标签 git

Git 基于内容而不是文件,所以我目前理解以下行为,但我想知道是否有特殊选项或 hack 来检测此类事情:

git init
mkdir -p foo/bar
echo "test" foo/a.txt
echo "test2" foo/bar/b.txt
git add -A
git commit -m "test"

rm -fr foo
git add -A
git commit -m "delete whole dir"
git log --name-status

当我检查日志时,Git 不会清楚地告诉我 foo 已被删除,但所有文件 foo/a.txtfoo/bar/b.txt 已删除

commit d1513a9b36cd546371a194e798566c49e779e3a9
Date:   Tue Sep 8 16:58:21 2015 +0200

    delete whole dir

D       foo/a.txt
D       foo/bar/b.txt

commit 135f7ae52dfddcee5eeb7bdfa9f0d5c924fed3af
Date:   Tue Sep 8 16:58:10 2015 +0200

    test

A       foo/a.txt
A       foo/bar/b.txt

因此,如果我创建以下提交:

mkdir -p foo/bar
echo "test" > foo/a.txt
echo "test2" > foo/bar/b.txt
echo "test3" > foo/bar/c.txt
git add -A
git commit -m "test2"

rm -f foo/a.txt foo/bar/b.txt
git add -A
git commit -m "delete just 2 files"
git log --name-status

name-status commit delete whole dirdelete just 2 files 之间是相似的

commit 92564fb59464fd6bba2766a6d488c2ff8ca967ea
Date:   Tue Sep 8 17:03:09 2015 +0200

    delete just 2 files

D       foo/a.txt
D       foo/bar/b.txt

commit 3b13f27e960c4ec66464e8a1e0d23f038a872564
Date:   Tue Sep 8 17:02:50 2015 +0200

    test2

A       foo/a.txt
A       foo/bar/b.txt
A       foo/bar/c.txt

当整个目录被删除时,有什么方法可以检测到差异吗?

最佳答案

假设您的存储库中的示例树结构为:

Initial commit

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

    new file:   a/b/c/d/e/q.txt
    new file:   a/b/c/m.txt
    new file:   a/b/c/n.txt
    new file:   a/b/f/o.txt
    new file:   a/b/f/p.txt
    new file:   a/b/k.txt
    new file:   a/b/z.txt
    new file:   a/x.txt
    new file:   a/y.txt

让我们进行以下提交:

commit 0bb4d4d50072c1eac1c3cb2b14b670deba8ee31b
Author: Site User <user@site.com>
Date:   Tue Sep 8 19:27:58 2015 +0100

    removed a/b/c/d/e/q.txt

D       a/b/c/d/e/q.txt

-

commit 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a
Author: Site User <user@site.com>
Date:   Tue Sep 8 19:28:55 2015 +0100

    removed a/b/c/m.txt and a/b/c/n.txt (full a/b/c)

D       a/b/c/m.txt
D       a/b/c/n.txt

-

commit 3af8ace473944996fb8b21135106360305e8b89a
Author: Site User <user@site.com>
Date:   Tue Sep 8 19:30:30 2015 +0100

    added a/b/g/w.txt

A       a/b/g/w.txt

-

提交 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a 是看到文件夹“a/b/c”消失的那个,因为其中的最后一个文件已被删除。

要在删除文件夹 a/b/c 时查找提交的 SHA#,您可以使用以下组合找到涉及“a/b/c”文件夹的最后一次提交:

#> git log --name-status -- a/b/c 

#> git ls-tree -r [commit] -- a/b/c

类似于:

for cmt in $(git log --pretty=%H -- a/b/c); \
    do X=$(git ls-tree -r "${cmt}" -- a/b/c); \
    [[ -z "${X}" ]] && echo "The folder a/b/c has been deleted in commit: ${cmt}"; \
done

输出:

The folder a/b/c has been deleted in commit: 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a

以简单的BASH脚本形式(我命名为deleted.sh,应该有可执行权限):

#!/bin/bash

P="$1"
GIT=$(which git)

for cmt in $($GIT log --pretty=%H -- "${P}"); do
    X=$($GIT ls-tree -r "${cmt}" -- "${P}");
    [[ -z "${X}" ]] && echo "The folder a/b/c has been deleted in commit: ${cmt}";
done

用法:

./deleted.sh a/b/c

输出:

The folder a/b/c has been deleted in commit: 3b53f16eb2fd7d3d605180ccabcfa71eb9e9225a

它是如何工作的

第一次提交,回溯历史,树中没有与文件夹路径匹配的文件,应该这样做。

这就是 shell 脚本所做的。

它在历史中向后迭代,检索与文件夹中任何文件相关的所有 SHA#,然后在这些提交中找到第一个没有与提供的路径匹配的文件的文件,在 git 树。

该提交即将出现在要检查的列表中这一事实确保其中包含涉及该文件夹的更改。

事实是没有文件匹配其树中的文件夹(过滤后的“ls-tree”返回空字符串),确保它是该文件夹中最后一个文件已被删除的提交。

关于Git如何检测整个文件夹已删除/移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461245/

相关文章:

git - 在 Visual Studio 2013 中使用本地 Git 和(远程)TFS

git - git checkouts 到底是什么意思?

git - 初始克隆后扩展 `git-p4` 客户端规范

git - 将 git 存储库 merge 到子目录中时出错

git - 同时提交到多个分支

Git - 删除像它一样的旧提交,并且它的更改从未发生过

git - 将旧分支 merge 到 develop 时非常奇怪的 git 行为

git - 如何创建多个 GIT 实例?

git - 如何判断我的 git repo 是否有冲突?

git - 关于 "Gerrit Change Number"的官方文档