根据 user2394284的建议,这是一个创建存储库临时克隆的小脚本:
/usr/bin/git-tmp-clone或者~/bin/git-tmp-clone
#!/bin/bash
gitTopLevel=$(git rev-parse --show-toplevel)
# Unique name for the temporary clone.
totalhash=$(tar -c "$gitTopLevel/.git" 2> /dev/null | sha256sum | head -c8)
tmprepo="/tmp/$(basename $(pwd))_${totalhash}"
git clone "$gitTopLevel" ${tmprepo}
# Start an interactive shell in the clone. Pass any
# arguments as initial commands to be executed.
/bin/bash --init-file <(echo "cd ${tmprepo}; $@")
# Clean up the clone.
rm -rf ${tmprepo} && echo "Deleted ${tmprepo}"
(这个脚本不太健壮,但它似乎在 Ubuntu 上对我有用。)
您可以使用它来挑选例如当前分支上的最后一次提交到另一个分支,通过运行
git-tmp-clone "git checkout TARGET_BRANCH
&& git cherry-pick $(git rev-parse --short @)
&& git push origin HEAD"
(请注意,在此示例中,在创建克隆之前在原始rev-parse
存储库中评估!这就是它指向最近提交的原因。根据需要进行调整。)