In our daily needs, we may just want to pull the specified files or folders in a certain warehouse to the local demand, here is the operation method
1. Create a folder
Generally speaking, before cloning the remote warehouse, a new folder will be created locally on your computer for storage. You can manually create a folder by yourself, or you can use the command:
// mkdir:创建文件夹
// 在当前目录下面创建文件夹 test
mkdir test
// 在当前目录下创建目录a,在目录a中创建目录b,以此类推,可以创建一个多层目录
mkdir -p test/b/c/d
2. Go to the created directory
Here is an example of the test file created above
cd /test
3. Initialize the local git warehouse
git init
4. Associate the local warehouse with the remote warehouse you want to clone
git remote add -f origin [email protected]:xxx/xxx.git
// 例如:
git remote add -f origin https://gitee.com/dfdff/test.git
git remote add -f origin [email protected]:yifhsdw/test.git
5. Enable sparse detection
You can specify file cloning only after it is enabled
git config core.sparsecheckout true
6. Write the file or folder to be pulled in the sparse-checkout file
echo "clone_file" >> .git/info/sparse-checkout
// 例如仓库test下面的testone文件夹
echo "test/testone" >> .git/info/sparse-checkout
7. Finally perform the clone operation
git pull origin master
Notice:
When I was pulling, the following prompt appeared: fatal: Couldn't find remote ref master;
This is the main line, not the master branch. The master branch was not found. The solution:
git branch -a
remotes/origin/main // 可以看到 主线名称是main
// 重新执行下拉取命令:
git pull origin main