Tha*_*yen 5 git revision checkout wildcard
我已经在Google上搜索了2个小时,但仍然找不到解决方案。
我想将所有带有通配符的文件签出到特定版本。我使用以下命令:
git checkout 663090b1c 'src/app/**/*.spec.ts'
Run Code Online (Sandbox Code Playgroud)
但它说:
error: pathspec 'src/app/**/*.spec.ts' did not match any file(s) known to git.
Run Code Online (Sandbox Code Playgroud)
谁能帮我?
我认为这应该有效,但显然无效。这是在上使用最新Git的效果(您可以从https://github.com/git/git获得)Documentation/**/*.txt。
我构建了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
$
Run Code Online (Sandbox Code Playgroud)
现在,但是:
$ ./git checkout HEAD -- 'Documentation/**/*.txt'
error: pathspec 'Documentation/**/*.txt' did not match any file(s) known to git
Run Code Online (Sandbox Code Playgroud)
似乎pathspecs与索引一起使用时有效,但与commit哈希一起使用时则无效。
作为一种解决方法-请注意,该解决方法有些缺陷,尽管您可以对其进行进一步修补-您可以首先将所需的提交读入临时索引,然后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"
Run Code Online (Sandbox Code Playgroud)
这里的(未测试的)想法是将“ $ 1”指定的提交读入一个临时索引。(我们可以放宽^{commit}到只是^{tree}因为任何树十岁上下就足够了。)然后,将具有读取到一个临时指数,我们用“混帐结账”的模式与pathspec($ 2)这样的作品,使用临时索引。这将写入工作树。
缺陷在于,现在更新的工作树文件也未在索引中更新。一个Real git checkout会将这些相同的文件复制到索引中。应该可以取消设置$GIT_INDEX_FILE并将git 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 ...
Run Code Online (Sandbox Code Playgroud)
(其余的工作也要作为练习来完成,以使之成为可用的Git shell命令。)
首先检查 find 命令会更容易:
find . src/app/**/*.spec.ts
Run Code Online (Sandbox Code Playgroud)
看看列表是否正确。
作为解决方法,您可以执行以下操作:
find . src/app/**/*.spec.ts | xargs git checkout <sha1> --
# or
find . src/app/**/*.spec.ts -exec git checkout <sha1> -- {} \;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |