max*_*152 17 git git-clone git-checkout
我想在git存储库中检索特定文件的所有先前版本.
我看到有可能通过checkout命令获得一个特定版本,但我想要它们全部.带有depth选项的git clone命令似乎不允许我克隆子文件夹("无效的存储库名称").
你知道它是否可能以及如何?
谢谢
Dmi*_*yas 23
OP希望检索所有版本,但答案无法提供.特别是如果文件有数百个修订(所有建议都太手册).唯一的半工作解决方案是@Tobias在评论中提出的,但建议bash循环将以随机顺序构建文件,并且当用于对我们的存储库时它会生成数百个空文件.其中一个原因是"rev-list --all --objects"会列出不同的对象(包括树 - 但对我们来说是无用的).
我开始使用Tobias的解决方案,添加计数器,清理一下并最终以下面列出的bash脚本的形式重新发明轮子.
该脚本将:
- 将所有文件版本提取到/ tmp/all_versions_exported
- 获取1个参数 - git repo中文件的相对路径
- 给出结果文件名数字前缀(可排序)
- 在结果文件中提及检查的文件名(告诉苹果除了桔子) :)
- 在结果文件名中提及提交日期(参见下面的输出示例)
- 不创建空结果文件
cat/usr/local/bin/git_export_all_file_versions
#!/bin/bash
# we'll write all git versions of the file to this folder:
EXPORT_TO=/tmp/all_versions_exported
# take relative path to the file to inspect
GIT_PATH_TO_FILE=$1
# ---------------- don't edit below this line --------------
USAGE="Please cd to the root of your git proj and specify path to file you with to inspect (example: $0 some/path/to/file)"
# check if got argument
if [ "${GIT_PATH_TO_FILE}" == "" ]; then
echo "error: no arguments given. ${USAGE}" >&2
exit 1
fi
# check if file exist
if [ ! -f ${GIT_PATH_TO_FILE} ]; then
echo "error: File '${GIT_PATH_TO_FILE}' does not exist. ${USAGE}" >&2
exit 1
fi
# extract just a filename from given relative path (will be used in result file names)
GIT_SHORT_FILENAME=$(basename $GIT_PATH_TO_FILE)
# create folder to store all revisions of the file
if [ ! -d ${EXPORT_TO} ]; then
echo "creating folder: ${EXPORT_TO}"
mkdir ${EXPORT_TO}
fi
## uncomment next line to clear export folder each time you run script
#rm ${EXPORT_TO}/*
# reset coutner
COUNT=0
# iterate all revisions
git rev-list --all --objects -- ${GIT_PATH_TO_FILE} | \
cut -d ' ' -f1 | \
while read h; do \
COUNT=$((COUNT + 1)); \
COUNT_PRETTY=$(printf "%04d" $COUNT); \
COMMIT_DATE=`git show $h | head -3 | grep 'Date:' | awk '{print $4"-"$3"-"$6}'`; \
if [ "${COMMIT_DATE}" != "" ]; then \
git cat-file -p ${h}:${GIT_PATH_TO_FILE} > ${EXPORT_TO}/${COUNT_PRETTY}.${COMMIT_DATE}.${h}.${GIT_SHORT_FILENAME};\
fi;\
done
# return success code
echo "result stored to ${EXPORT_TO}"
exit 0
Run Code Online (Sandbox Code Playgroud)
用法示例:
cd /home/myname/my-git-repo
git_export_all_file_versions docs/howto/readme.txt
result stored to /tmp/all_versions_exported
ls /tmp/all_versions_exported
0001.17-Oct-2016.ee0a1880ab815fd8f67bc4299780fc0b34f27b30.readme.txt
0002.3-Oct-2016.d305158b94bedabb758ff1bb5e1ad74ed7ccd2c3.readme.txt
0003.29-Sep-2016.7414a3de62529bfdd3cb1dd20ebc1a977793102f.readme.txt
0004.28-Sep-2016.604cc0a34ec689606f7d3b2b5bbced1eece7483d.readme.txt
0005.28-Sep-2016.198043c219c81d776c6d8a20e4f36bd6d8a57825.readme.txt
0006.9-Sep-2016.5aea5191d4b86aec416b031cb84c2b78603a8b0f.readme.txt
<and so on and on . . .>
Run Code Online (Sandbox Code Playgroud)
编辑:如果你看到这样的错误:
致命的:不是有效的对象名3e93eba38b31b8b81905ceaa95eb47bbaed46494:readme.txt
这意味着你没有从你的git项目的根文件夹启动脚本.
小智 12
德米特里提供的脚本实际上解决了这个问题,但它有一些问题导致我使其适应我的需要.特别:
git show由于我的默认日期格式设置,使用了break.您可以在我的github repo中查看我的修改的最新版本,或者在撰写本文时的版本:
#!/bin/sh
# based on script provided by Dmitry Shevkoplyas at http://stackoverflow.com/questions/12850030/git-getting-all-previous-version-of-a-specific-file-folder
set -e
if ! git rev-parse --show-toplevel >/dev/null 2>&1 ; then
echo "Error: you must run this from within a git working directory" >&2
exit 1
fi
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <relative path to file> [<output directory>]" >&2
exit 2
fi
FILE_PATH="$1"
EXPORT_TO=/tmp/all_versions_exported
if [ -n "$2" ]; then
EXPORT_TO="$2"
fi
FILE_NAME="$(basename "$FILE_PATH")"
if [ ! -d "$EXPORT_TO" ]; then
echo "Creating directory '$EXPORT_TO'"
mkdir -p "$EXPORT_TO"
fi
echo "Writing files to '$EXPORT_TO'"
git log --diff-filter=d --date-order --reverse --format="%ad %H" --date=iso-strict "$FILE_PATH" | grep -v '^commit' | \
while read LINE; do \
COMMIT_DATE=`echo $LINE | cut -d ' ' -f 1`; \
COMMIT_SHA=`echo $LINE | cut -d ' ' -f 2`; \
printf '.' ; \
git cat-file -p "$COMMIT_SHA:$FILE_PATH" > "$EXPORT_TO/$COMMIT_DATE.$COMMIT_SHA.$FILE_NAME" ; \
done
echo
exit 0
Run Code Online (Sandbox Code Playgroud)
输出的一个例子:
$ git_export_all_file_versions bin/git_export_all_file_versions /tmp/stackoverflow/demo
Creating directory '/tmp/stackoverflow/demo'
Writing files to '/tmp/stackoverflow/demo'
...
$ ls -1 /tmp/stackoverflow/demo/
2017-05-02T15:52:52-04:00.c72640ed968885c3cc86812a2e1aabfbc2bc3b2a.git_export_all_file_versions
2017-05-02T16:58:56-04:00.bbbcff388d6f75572089964e3dc8d65a3bdf7817.git_export_all_file_versions
2017-05-02T17:05:50-04:00.67cbdeab97cd62813cec58d8e16d7c386c7dae86.git_export_all_file_versions
Run Code Online (Sandbox Code Playgroud)
git rev-list --all --objects -- path/to/file.txt
Run Code Online (Sandbox Code Playgroud)
列出与回购路径相关的所有Blob
获取文件的特定版本
git cat-file -p commitid:path/to/file.txt
Run Code Online (Sandbox Code Playgroud)
(委员会可以是任何东西