首页 > 系统相关 >在 shell 中 git clone 仓库的子目录

在 shell 中 git clone 仓库的子目录

时间:2023-02-06 14:12:43浏览次数:49  
标签:shell url clone echo project git path

  1. 复制以下代码到shell的配置文件(如:~/.bashrc 或 ~/.zshrc)中
# specify a directory to git clone
gitcs(){
    
    # extract the content in the url to each variables
    #url="https://github.com/tensorflow/models/tree/master/orbit/examples"

    if [[ -z $1 ]]; then
        echo "usage: gitcs git-clone-url"
        return 1
    fi

    url="$1"
    project_path="${url%/tree/*}"
    echo "project_path: $project_path"

    str="${project_path#*://*/}"
    dir="${str%/*}-${str#*/}"
    echo "dir: $dir"

    str="${url#*/tree/}"
    #echo "$str"
    subdir="${str#*/}"
    echo "subdir: $subdir"

    branch="${str%%/*}"
    echo "$branch"

    create directory and clone
    mkdir "$dir"
    cd "$dir"
    git init 
    git remote add origin "$project_path" # 增加远端的仓库地址
    git config core.sparsecheckout true # 设置Sparse Checkout 为true 
    echo "$subdir" >> .git/info/sparse-checkout # 将要部分clone的目录相对根目录的路径写入配置文件
    git pull origin "$branch" #pull下来代码

}
  1. 读取配置文件
$ source ~/.zshrc
  1. 使用方式
$ gitcs https://github.com/tensorflow/models/tree/master/orbit/examples

参考:

https://zhuanlan.zhihu.com/p/54581830

标签:shell,url,clone,echo,project,git,path
From: https://www.cnblogs.com/3yude/p/17095221.html

相关文章

  • 'powershell'/'netsh'不是内部或外部命令,也不是可运行的程序或批处理文件
    笔者win10系统;昨天在配置openssh的时候在cmd里遇到了这两个报错。上网查了一下,解决方法分别是:'powershell':在环境变量path后添加powershell的路径(C:\Windows\System......
  • 朋友圈那串神秘字符背后的开源项目「GitHub 热点速览」
    ​如果你这周没刷到类似“npub1sg6plzptd64u62a878hep2kev88swjh3tw00gjsfl8f237...”的一串字符,那就说明本期GitHubTrending周榜的内容非常适合你。这是前推特创始......
  • Git 全局代理设置
    【待补充】...git设置和取消代理(github.com) 查看配置 gitconfig--list HowtosetimportantGitconfigglobalproperties(theserverside.com)......
  • Shell函数
    Shell函数一、Shell函数函数的作用就是把程序里需要多次使用的部分代码列出来,然后为这部分代码起个名字,其它所有的重复调用这部分代码都只用调用这个名字就可以(类似于别......
  • Linux系统Shell脚本第六章:文件三剑客之sed
    一、文本三剑客之sed1.基本用法sed[选项]...'{自身脚本语法};....'[inputfile...]2、sed脚本语法及命令①sed脚本语法:地址+sed自己脚本命令,地址即范围例如全文或......
  • shell脚本循环语句
    shell脚本循环语句一、for循环语句for语句需要定义一个变量和取值列表,根据不同的取值执行相同的命令,知道变量值用完。取值列表里包含多个属性相同的对象,例如:IP地址,通信......
  • Linux系统Shell脚本第五章:shell数组、正则表达式及文件三剑客之AWK
    一、shell数组1.数组分类①关联数组:必须声明才可以使用,命令:delare -A  数组名  ②普通数组:利用数字下标节约变量,可以不声明也可以声明,命令:delare-a 数组名d......
  • shell脚本之条件语句
    shell脚本之条件语句一、条件测试操作1、测试命令------test可以对特定条件进行测试,并根据返回值来判断条件是否成立(返回值为0表示条件成立,反之不成立)。使用test命令......
  • Linux系统Shell脚本第四章:shell函数
    一、shell函数1.函数的作用定义较为复杂的但是需要重复使用的内容,以便再次使用可以直接调用函数节约时间,提高效率2.函数使用步骤①首先是定义函数②其次是调用函数(......
  • Git使用教程
    1.下载与安装下载地址:安装步骤直接下一步下一步就行。2.Git环境配置gitconfig--globaluser.name"YourName"gitconfig--globaluser.email"[email protected]......