将文件夹与 git 存储库的子文件夹进行比较

Bol*_*wyn 6 git

我有一个now从 git 存储库导出的文件夹,以及 git 存储库本身:

/
/now/
/repository.git/
Run Code Online (Sandbox Code Playgroud)

我想将文件now与存储库的当前状态进行比较。虽然这很容易用--git-dir=/repository.gitand完成--work-tree=/now

git --git-dir=/repository.git --work-tree=/now status
Run Code Online (Sandbox Code Playgroud)

对我来说不是很明显,什么时候now是git 存储库中内容的子文件夹

有没有办法——除了再次检查存储库并检查diff这些东西——以git某种方式比较存储库中的文件夹和相应的子文件夹?(我知道,那个 git 并不是围绕单个文件中的更改的想法,因此我实际上希望有一个消息灵通的no。)

Chr*_*sen 2

您可以仅使用存储库子目录的内容填充临时索引文件,然后将现有文件与其进行比较。

diff-sub() { : usage: diff-sub repo treeish-in-repo external-dir
  (
    GIT_DIR="$1" GIT_WORK_TREE="$3"
    GIT_INDEX_FILE=/tmp/.git-index--now-sub.tmp
    if test -e "$GIT_INDEX_FILE"; then
      echo "already exists: $GIT_INDEX_FILE"
      exit 1
    fi
    export GIT_DIR GIT_INDEX_FILE GIT_WORK_TREE
    git read-tree "$2" &&
    git diff
    ec=$?
    rm -f "$GIT_INDEX_FILE"
    exit "$ec"
  )
}
: diff sub from master in /repository.git against /now
diff-sub /repository.git master:sub /now
Run Code Online (Sandbox Code Playgroud)

注意:如果您运行其他与正常完整树进行比较的 Git 命令(例如git statusgit diff --cached代替git diff),那么您将看到奇怪的结果,因为索引和工作树都只包含正常完整树的一部分。