gpt4 book ai didi

使用通配符对特定修订版进行 Git check out

转载 作者:太空狗 更新时间:2023-10-29 13:19:45 25 4
gpt4 key购买 nike

我已经在谷歌上搜索了 2 个小时,仍然找不到解决方案。

我想将所有带有通配符的文件 check out 到特定修订版。我使用了以下命令:

git checkout 663090b1c 'src/app/**/*.spec.ts'

但是它说:

error: pathspec 'src/app/**/*.spec.ts' did not match any file(s) known to git.

谁能帮帮我?

最佳答案

我相信这应该可行,但显然行不通。这是在 Documentation/**/*.txt 上使用最新 Git(您可以在 https://github.com/git/git 获得)的效果。

我构建了 Git,然后使用 Git 系统(在 2.21,略低于这个版本 2.22.0.545.g9c9b961d7e)来执行此操作:

$ rm Documentation/*/*.txt
$ git status --short | head
D Documentation/RelNotes/1.5.0.1.txt
D Documentation/RelNotes/1.5.0.2.txt
D Documentation/RelNotes/1.5.0.3.txt
D Documentation/RelNotes/1.5.0.4.txt
D Documentation/RelNotes/1.5.0.5.txt
D Documentation/RelNotes/1.5.0.6.txt
D Documentation/RelNotes/1.5.0.7.txt
D Documentation/RelNotes/1.5.0.txt
D Documentation/RelNotes/1.5.1.1.txt
D Documentation/RelNotes/1.5.1.2.txt
$ git checkout -- 'Documentation/**/*.txt'
$ git status --short | head
$

然而,现在:

$ ./git checkout HEAD -- 'Documentation/**/*.txt'
error: pathspec 'Documentation/**/*.txt' did not match any file(s) known to git

看起来 pathspecs 在与索引一起使用时有效,但在与提交哈希一起使用时无效。

作为解决方法——请注意,此解决方法有些缺陷,但您可以进一步修补它——您可以首先将所需的提交读入临时索引,然后从临时索引执行 git checkout指数。以下脚本完全未经测试。

#! /bin/sh
# git-co-c-ps: git checkout <commit> <pathspec>
# work around a bug in which combining <commit> and <pathspec> does not work
. git-sh-setup
case $# in
2) ;;
*) die "usage: $0 <commit> <pathspec>";;
esac

hash=$(git rev-parse "$1^{commit}") || exit 1
export GIT_INDEX_FILE=$(mktemp) || exit 1
rm -f $GIT_INDEX_FILE
trap "rm -f $GIT_INDEX_FILE" 0 1 2 3 15
git read-tree "$hash" || exit 1
git checkout -- "$2"

这里的(未经测试的)想法是将“$1”指定的提交读入临时索引。 (我们可以将 ^{commit} 放宽为 ^{tree},因为任何树结构都应该足够了。)然后,将其读入临时索引后,我们使用“git checkout”模式和有效的路径规范($2),使用临时索引。这将写入工作树。

缺陷是现在更新的工作树文件没有在索引中也更新。真正的 git checkout 会将这些相同的文件复制到索引中。应该可以取消设置 $GIT_INDEX_FILEgit add 添加的文件到真实索引。这留作练习。请注意,它不像 git add -- "$2" 那样微不足道,因为一些工作树文件可能在工作树中 因为它们被读入了git read-tree 操作的临时索引,而是因为它们已经在工作树中。 (有些可能已被跟踪和修改但尚未上演/添加,而其他一些可能未被跟踪。)

另一个可能更好的解决方法是在临时索引上使用 git ls-files 来查找感兴趣的文件。使用 xargs 或类似方法将这些文件名传递给在没有临时索引的情况下运行的 git checkout:

files_file=$(mktemp)
trap "rm -f $files_file" 0 1 2 3 15
(
export GIT_INDEX_FILE=$(mktemp) || exit 125
rm -f $GIT_INDEX_FILE
trap "rm -f $GIT_INDEX_FILE" 0 1 2 3 15
git read-tree $hash
git ls-files -z -- "$2"
) > $files_file
# if the inner shell exited with an error, exit now
case $? in 125) exit 1;; esac
xargs -0 ...

(填写其余部分以制作可用的 Git shell 命令也留作练习。)

关于使用通配符对特定修订版进行 Git check out ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57193458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com