簡體   English   中英

Git結帳使用通配符到特定修訂版

[英]Git checkout using wildcard to a specific revision

我一直在谷歌上搜索它2個小時仍然找不到解決方案。

我想將帶有通配符的所有文件簽出到特定版本。 我使用以下命令:

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}因為任何樹都應該足夠了。)然后,將其讀入臨時索引,我們使用帶有pathspec($ 2)的“git checkout”模式這是有效的,使用臨時索引。 這會寫入工作樹。

缺陷是現在更新的工作樹文件也未在索引中更新。 真正的git checkout會將這些相同的文件復制到索引中。 應該可以取消設置$GIT_INDEX_FILE並將git add到真實索引中。 這是一個練習。 請注意,它不像git add -- "$2"那樣簡單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命令也留作練習。)

首先檢查一個find命令會更容易:

find . src/app/**/*.spec.ts

查看列表是否正確。
作為解決方法,您可以執行以下操作:

find . src/app/**/*.spec.ts | xargs git checkout <sha1> -- 
# or 
find . src/app/**/*.spec.ts -exec git checkout <sha1> -- {} \; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM