LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-13-2022, 10:06 AM   #1
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,540
Blog Entries: 3

Rep: Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410
Tracking just a single file in a Git repository?


I am wondering if it is possible to work with just a single file from a specific Git repository, in as few steps as possible.

Effectively, I'd like to be able to check out just the one file into a specific directory where it can be used while not checking out the rest of the repository. The checked out file needs to be edited from time to time and then the edits committed and pushed back into the repository.

Is there a way to check out just a single file for use and editing without dragging the rest of the repository along with it?
 
Old 11-13-2022, 10:48 AM   #2
teckk
Senior Member
 
Registered: Oct 2004
Distribution: Arch
Posts: 4,448
Blog Entries: 5

Rep: Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548Reputation: 1548
According to this:
https://www.delftstack.com/howto/git...specific-file/
https://wiki.archlinux.org/title/Git
 
Old 11-13-2022, 02:22 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,540

Original Poster
Blog Entries: 3

Rep: Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410
That's still working from having a clone of the full repository. I've been doing that so far by cloning / pulling and then linking to the specific file from the actual directory it is needed in. I'd just would like to find a simpler, less brittle approach.
 
Old 11-13-2022, 02:57 PM   #4
GazL
LQ Veteran
 
Registered: May 2008
Distribution: CRUX 3.7
Posts: 6,568

Rep: Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668Reputation: 4668
I think the closest you'll get to that is doing a "shallow clone". At least that way you don't need to pull the full history, but I don't believe there's any way to avoid having a local repo all together.
 
Old 11-13-2022, 06:30 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,736
Blog Entries: 13

Rep: Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784Reputation: 4784
I don't get this.

What's wrong with getting one clone of the repo?

If you always edit just that one file, then you do.

Commit and push. One file. git clean -fdx to clean any junk out.

There's no need to be more complicated.

I'm sure you know better being the actual editor. The world I live in, I have to check that it compiles properly and use a private branch.
 
Old 11-13-2022, 07:04 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
Short answer is no you can't do that.
 
Old 11-13-2022, 08:41 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,612

Rep: Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680
Yes you can do this with more recent git using sparse-checkout
Code:
git clone --no-checkout --filter=blob:none --sparse https://somwhere.org:somerepo.git
cd somerepo
git sparse-checkout init --cone
git sparse-checkout add thefileIwant.txt
Evo2.
 
2 members found this post helpful.
Old 11-13-2022, 08:42 PM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,612

Rep: Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680
Quote:
Originally Posted by dugan View Post
Short answer is no you can't do that.
Yes you can. See https://www.git-scm.com/docs/git-sparse-checkout
 
Old 11-13-2022, 08:44 PM   #9
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,612

Rep: Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680
Quote:
Originally Posted by rtmistler View Post
I don't get this.

What's wrong with getting one clone of the repo?
Some repos can be *HUGE*. A repo that I work with has multiple GB of binary files I never need. Being able clone just what I need saves significant disk space, network traffic, and time.
 
Old 11-14-2022, 04:38 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,540

Original Poster
Blog Entries: 3

Rep: Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410
Quote:
Originally Posted by evo2 View Post
Yes you can do this with more recent git using sparse-checkout
Thanks. I've gone through https://www.git-scm.com/docs/git-sparse-checkout, too.

I'm still experimenting, and spent quite some time on it, but I think an extra step will be needed the one file to actually show up:

Code:
cd ~
git clone --no-checkout --filter=blob:none --sparse ssh://git.example.com:someRepo.git
cd someRepo
git sparse-checkout init --cone
git sparse-checkout add path/to/theFileIWant.txt
At that point pulling produces the message that everything is up to date, but the directories and the file are not created locally. I don't see an option for that with git-sparse-checkout and doing git-checkout, as in git checkout HEAD or git checkout master, pulls in too much.

I do see that ~/someRepo/.git/info/sparse-checkout contains the chosen file and paths, but what is needed to actually have them present?

Last edited by Turbocapitalist; 11-14-2022 at 04:43 AM.
 
Old 11-14-2022, 05:00 AM   #11
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,612

Rep: Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680Reputation: 1680
Off the top of my head, 'git pull'.AFK right now. Will check im about 12 hours.

Last edited by evo2; 11-14-2022 at 05:02 AM.
 
Old 11-14-2022, 09:25 AM   #12
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120

The git sparse-checkout docs say "init" is deprecated, and "set" can handle paths, and it also suggests "cone mode" is for entire directories.

So maybe instead of those last two lines you want something like "git sparse-checkout set --no-cone path/to/file" ?

 
Old 11-14-2022, 09:34 AM   #13
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,540

Original Poster
Blog Entries: 3

Rep: Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410Reputation: 3410
Quote:
Originally Posted by boughtonp View Post
So maybe instead of those last two lines you want something like "git sparse-checkout set --no-cone path/to/file" ?
Thanks. The following seems to do the job:

Code:
cd ~
git clone --no-checkout --filter=blob:none --sparse ssh://git.example.com:someRepo.git
cd someRepo
git sparse-checkout set --no-cone path/to/file
git checkout
It fetched only the file in question, and parent directories, without any other files or directories.

Now to re-read the documentation about what actually went on.
 
  


Reply

Tags
git


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Gitpod git-bolts git-IDE onto GitHub for in-browser code git-editing LXer Syndicated Linux News 0 09-05-2018 05:50 AM
LXer: Creating And Managing Git Repository - Git Series Part 2 LXer Syndicated Linux News 0 08-29-2018 05:34 PM
@FedEx Tracking: pasting correct tracking # causes error; typing same works mike3644 Linux - Software 7 02-15-2018 04:55 PM
LXer: Git Tracking Relationships: Use the Full Power of Git Branches LXer Syndicated Linux News 0 08-19-2014 09:42 AM
[SOLVED] Can't install Git repo (I don't git git ) Nemus Linux - Software 3 05-20-2011 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration