如何从git仓库安全地签出临时工作树? [英] How to safely checkout a temporary working tree from a git repository?

查看:502
本文介绍了如何从git仓库安全地签出临时工作树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的回答另一个问题我提出了一个解决方案,它需要检出临时工作树,但必须安全地执行,而不以任何方式破坏源存储库。特别是,该操作必须安全地与其他git操作同时运行。我相信这是可以做到的,没有在答案中详细说明,然后必须提供一个评论,说明如何做到这一点。这个自我回答的问题填补了这个空白。

解决方案

下面的脚本将一个工作树检入当前目录,而不会影响源存储库(特别是它可以在没有写入权限的存储库上执行,例如属于另一个用户的存储库)。

  git-checkout -tmp-worktree repo_path [revision] 


$ b

示例:

  git-checkout -tmp-worktree〜/ myrepo1 
git-checkout -tmp-worktree〜/ myrepo1 HEAD ^^

git-checkout- tmp-worktree

 #!/ usr / bin / env bash 

if [[$#-eq 0 || $#$ gt;
echo $>& 2用法:$(basename$ 0)repo_path [revision]
exit 1
fi

repo =$ {1}
revision =$ {2:-HEAD}

tmpdir =$(mktemp -d)
traprm -rf'$ tmpdir'EXIT
GIT_INDEX_FILE =$ tmpdir / indexgit --git-dir =$ repo/。git checkout$ revision。


In my answer to another question I proposed a solution that required checking out a temporary working tree, but it had to be performed safely, without clobbering the source repository in any way. In particular, the operation must be safe to run concurrently with other git operations. I believed that it could be done, and didn't elaborate it in the answer, and then had to provide a comment showing how to do that. This self-answered question fills the gap.

解决方案

Below script checks out a working tree into the current directory without affecting the source repository in any way (in particular it can be executed on a repository to which there is no write access, e.g. a repository belonging to another user).

Usage:

git-checkout-tmp-worktree repo_path [revision]

Examples:

git-checkout-tmp-worktree ~/myrepo1
git-checkout-tmp-worktree ~/myrepo1 HEAD^^

git-checkout-tmp-worktree:

#!/usr/bin/env bash

if [[ $# -eq 0 || $# -gt 2 ]]
then
    echo >&2 "Usage: $(basename "$0") repo_path [revision]"
    exit 1
fi

repo="${1}"
revision="${2:-HEAD}"

tmpdir="$(mktemp -d)"
trap "rm -rf '$tmpdir'" EXIT
GIT_INDEX_FILE="$tmpdir/index" git --git-dir="$repo"/.git checkout "$revision" .

这篇关于如何从git仓库安全地签出临时工作树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆