我从许多不同的来源克隆回购。有时可能会混淆谁是这些回购中的一个所有人都坐在我的git文件夹中。为了解决这个问题,我已经开始手动创建一个包含repo所有者名称的子文件夹,然后在该子文件夹中进行克隆。有没有办法自动执行此操作?让git读取所有者名称,然后克隆到/?
答案 0 :(得分:1)
git clone
为目标选择第二个参数:
git clone https://github.com/username/repo username-repo
您可以使用shell脚本为某些可识别的提供程序自动执行,例如:
github-clone() {
local dest="$(printf %s "$1" | sed -n 's#^.*[/:]\([[:alnum:]-]\+\)/\([[:alnum:]-]\+\)\(\.git\)\?$#\1-\2#p')"
if [[ -z "$dest" ]]; then
git clone "$1"
else
git clone "$1" "$dest"
fi
}
$ github-clone https://github.com/username/repo
Cloning into 'username-repo'...
答案 1 :(得分:0)
您可以通过以下方式使用此手工制作的Bash别名clone
(见下文):
$ clone https://github.com/coq/coq.git ~/git/
+ git clone https://github.com/coq/coq.git /home/user/git/coq/coq
Cloning into '/home/user/git/coq/coq'...
$ clone git@github.com:coq/coq.git .
+ git clone git@github.com:coq/coq.git ./coq/coq
Cloning into './coq/coq'...
它适用于HTTPS和SSH GitHub存储库:
alias clone='f(){ [[ $# -ne 2 ]] && { echo "\$ clone git@github.com:user/repo.git ."; return 1; } || ( tmp="${1##*github.com?}"; set -x; git clone "$1" "${2%/}/${tmp%.git}" ) }; f'
但是如果你不想自动添加GitHub仓库的用户名,并希望同时推广到与GitHub不相关的其他Git URL,你可以用{{tmp="${1##*github.com?}"
替换tmp="${1##*/}"
1}}:
alias clone='f(){ [[ $# -ne 2 ]] && { echo "\$ clone git@github.com:user/repo.git ."; return 1; } || ( tmp="${1##*/}"; set -x; git clone "$1" "${2%/}/${tmp%.git}" ) }; f'
E.g:
$ clone https://github.com/coq/coq.git ~/git/other-user
+ git clone https://github.com/coq/coq.git /home/user/git/other-user/coq
Cloning into '/home/user/git/other-user/coq'...