一种更好的 git clone 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12058705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A better way to do git clone
提问by Saurabh Kumar
This is lazy programmer request. I would like to create a shell script automate the following process:
这是懒惰的程序员要求。我想创建一个 shell 脚本来自动执行以下过程:
git clone <remote-repo-url>
cd <cloned-folder>
open <cloned-folder>
So the idea here is to clone a URL and then immediately cd
into the cloned-folder
. The trick here is to identify the cloned-folder
from the url pattern.
所以这里的想法是克隆一个 URL,然后立即cd
进入cloned-folder
. 这里的技巧是cloned-folder
从 url 模式中识别。
For now we can assume that url structure is in this pattern .../<cloned-folder>.git
i.e. the url.
现在我们可以假设 url 结构采用这种模式,.../<cloned-folder>.git
即 url。
I am wondering if we can actually do with using awk
or some tools like that. The part i am stuck is finding a appropriate regex
, I guess.
我想知道我们是否真的可以使用 usingawk
或类似的工具。regex
我想我被卡住的部分是找到一个合适的。
USE CASE: Here the use case is if you clone a url, you want to be in the repofolder as soon as possible. The is the pre-requirement if you want to run any git
command like git log
or mate .
which we do 99% of the time.
使用例:这里使用的情况是,如果你克隆一个网址,要尽快将在repofolder。如果您想运行任何git
类似git log
或mate .
我们 99% 的时间都在执行的命令,这是先决条件。
Thanks in advance.
提前致谢。
回答by dbr
bash function to do this (works in zsh also):
bash 函数来执行此操作(也适用于 zsh):
function lazyclone {
url=;
reponame=$(echo $url | awk -F/ '{print $NF}' | sed -e 's/.git$//');
git clone $url $reponame;
cd $reponame;
}
The awk
command prints the part after the last /
(e.g from http://example.com/myrepo.git
to myrepo.git
). The sed
command removes the trailing .git
该awk
命令打印最后一个之后的部分/
(例如 from http://example.com/myrepo.git
to myrepo.git
)。该sed
命令删除尾随.git
Usage:
用法:
$ pwd
~/
$ lazyclone https://github.com/dbr/tvdb_api.git
tvdb_api
Cloning into 'tvdb_api'...
remote: Counting objects: 1477, done.
remote: Compressing objects: 100% (534/534), done.
remote: Total 1477 (delta 952), reused 1462 (delta 940)
Receiving objects: 100% (1477/1477), 268.48 KiB | 202 KiB/s, done.
Resolving deltas: 100% (952/952), done.
$ pwd
~/tvdb_api
回答by jordanm
With git clone, you can specify the folder to clone to, instead of allowing it to be named automatically.
使用 git clone,您可以指定要克隆到的文件夹,而不是让它自动命名。
dir=myclone
git clone git://somerepo "$dir"
cd "$dir"
open "$dir"
回答by u2278997
An improvement from @dbr's answer:
来自@dbr 的回答的改进:
function lazyclone {
reponame=${1##*/}
reponame=${reponame%.git}
git clone "" "$reponame";
cd "$reponame";
}
With the help of Bash's parameter expansion, we can get rid of awk
and sed
.
借助 Bash 的参数扩展,我们可以去掉awk
和sed
。
回答by Louis Maddox
The bash parameter version is quite nice, I went with basename
cuz it seemed cleaner
bash 参数版本非常好,我选择了basename
因为它看起来更干净
function gclocd {
# git clone and enter the cloned file
if [[ -z "" ]]; then
reponame=$(basename "" ".git");
else
reponame=;
fi
git clone "" "$reponame";
cd "$reponame";
}
I use short memorable tab-completable function names for a bunch of things like this and put them in my .bashrc
as aliases/functions, e.g. gcmmt
for git commit -m
. lazy
may well already be a prefix for things in your shell, which means it's that little bit more to type each time...
我为一堆这样的东西使用了简短的、令人难忘的制表符可完成函数名称,并将它们.bashrc
作为别名/函数放在我的中,例如gcmmt
for git commit -m
. lazy
很可能已经是 shell 中事物的前缀,这意味着每次键入的内容都要多一点...
回答by Hongbo Liu
Instead of getting directory name from url, we could just cd
into the latest created directory. Here is the corresponding shell script:
我们可以直接cd
进入最新创建的目录,而不是从 url 获取目录名称。下面是对应的shell脚本:
gclcd() {
git clone --recursive $* && cd "$(ls -t | head -1)"
}