git pull 某一个文件或文件夹

QQ: 2967732156
背景:

在使用Oracle VM VirtualBox,进行Linux开发时,随着使用内存越来越少,空间已不足拉取整个代码库。

Ubuntu1604内存够,Ubuntu18.04内存不够。

思路:

第一步:从问题本身出发,先能满足编译需要。

方法1,手动从Ubuntu16.04上拉取最新代码,删掉不需要的文件,仅保留Ubuntu18.04需要,然后一次scp

方法2,从Ubuntu16.04上拉取最新代码,在Ubuntu18.04,多次scp所需文件

第二步:考虑自动化脚本。

方法1:从Ubuntu16.04上拉取最新代码,Ubuntu18.04上编写脚本一次执行,传输所需文件

方法2:打个疑问“git 能不能实现文件或文件夹的拉取,而且只拉取最新的?” 查阅资料,整理解决方案并验证

方法:
建立一个空的git 本地仓库:
$ git init new.project && cd new.project

在本地仓库里面启用sparse-checkout:
$ git config core.sparseCheckout true

设定你要checkout 哪些文件夹(如/xxx/*表示xxx文件夹下所有文件)(也可以用vim直接打开 .git/info/sparse-checkout写入)
$ echo '/xxx/*' >> .git/info/sparse-checkout

设定remote (要从哪里clone/pull?):
$ git remote add origin git://github.com/xxx/xxx.git

然后就可以开始pull 了(这边可以加上后面说的shallow pull,加上–depth=n
$ git pull origin master
整理:

命令行方式

git init project && cd project
git config core.sparseCheckout true
git remote add origin ssh://[email protected]:XXXXXX/SDK/SDK.git 
echo '/complieLinux.sh' >> .git/info/sparse-checkout
echo '/sources/include/*' >> .git/info/sparse-checkout
echo '/sources/config/*' >> .git/info/sparse-checkout
echo '/sources/json/*' >> .git/info/sparse-checkout
echo '/sources/log/*' >> .git/info/sparse-checkout
echo '/sources/sensor/*' >> .git/info/sparse-checkout
echo '/sources/platforms/*' >> .git/info/sparse-checkout
echo '/sources/src/*' >> .git/info/sparse-checkout
echo '/sources/3rdparty/libjpeg-turbo-2.0.2/*' >> .git/info/sparse-checkout
echo '/sources/3rdparty/opencv/bulid/include_3.4.16/*' >> .git/info/sparse-checkout
echo '/sources/3rdparty/opencv/bulid/x64/ubuntu18.04/lib_3416/*' >> .git/info/sparse-checkout
echo '/sources/3rdparty/algorithm/PC_ImgPreProc/ImgPreProcess/*' >> .git/info/sparse-checkout
echo '/sources/3rdparty/algorithm/imgpreproc/include/*' >> .git/info/sparse-checkout

录取全部记录
git pull origin v3

只拉取最近一次的变更,忽略以前的变更记录,在拉取时可以加参数depth
git pull --depth=1 origin v3

sparse-checkout文件内容:

/complieLinux.sh 
/sources/include/* 
/sources/config/* 
/sources/json/* 
/sources/sensor/* 
/sources/platforms/* 
/sources/3rdparty/libjpeg-turbo-2.0.2/* 
/sources/3rdparty/opencv/bulid/include_3.4.16/* 
/sources/3rdparty/opencv/bulid/x64/ubuntu18.04/lib_3416/* 
/sources/3rdparty/algorithm/PC_ImgPreProc/ImgPreProcess/* 
/sources/3rdparty/algorithm/imgpreproc/include/* 

优化:

使用python编写build.py

# coding:utf-8
########################################################################
# File Name: build.py
# Purpose: git 拉取单个文件或特定文件夹
# Usage: python build.py
# Author: Neal
# Mail: [email protected] 
# Created Time: 2023
########################################################################


#import 引入模块  相当于inclued + 导入外部库吧
#os 模块提供了非常丰富的方法用来处理文件和目录
import os
print ('start')
def gitpull():
    
    os.system('git init project')
    os.chdir('project')
  
    os.system('git config core.sparseCheckout true')
    os.system('git remote add origin 
    ssh://[email protected]:XXXXXX/SDK/SDK.git ')
    os.system('echo /complieLinux.sh >> .git/info/sparse-checkout')
    os.system('echo /sources/include/* >> .git/info/sparse-checkout')
    os.system('echo /sources/config/* >> .git/info/sparse-checkout')
    os.system('echo /sources/json/* >> .git/info/sparse-checkout')
    os.system('echo /sources/log/* >> .git/info/sparse-checkout')
    os.system('echo /sources/sensor/* >> .git/info/sparse-checkout')
    os.system('echo /sources/platforms/* >> .git/info/sparse-checkout')
    os.system('echo /sources/src/* >> .git/info/sparse-checkout')
    os.system('echo /sources/3rdparty/libjpeg-turbo-2.0.2/* >> .git/info/sparse-checkout')
    os.system('echo /sources/3rdparty/opencv/bulid/include_3.4.16/* >> .git/info/sparse-checkout')
    os.system('echo /sources/3rdparty/opencv/bulid/x64/ubuntu18.04/lib_3416/* >> .git/info/sparse-checkout')
    os.system('echo /sources/3rdparty/algorithm/PC_ImgPreProc/ImgPreProcess/* >> .git/info/sparse-checkout')
    os.system('echo /sources/3rdparty/algorithm/imgpreproc/include/* >> .git/info/sparse-checkout')


    os.system('git pull --depth=1 origin v3')
gitpull()
print ('end')
补充知识点:

python os.system("cd 目录") 不起效

使用os提供的os.chdir(‘目录’)

总结:

Ubuntu18.04已验证OK

你可能感兴趣的:(GIT,Python,git,python)