ChatGPT解决这个技术问题 Extra ChatGPT

How to pull specific directory with git

I have a project with git, and I just want to clone or pull a specific directory, like myproject/javascript just like subversion does. make some changes, commit and push back again. It's possible?

sparse checkout might interest you (even though you still need to fetch everything): see stackoverflow.com/questions/2416815/…
"Clone" and "pull" are two different operations in git. The question asks about both, and you've gotten some answers for each one, but it's not always clear which one each answer is intended to address. Which is confusing.

A
Alan Mick

cd into the top of your repo copy git fetch git checkout HEAD path/to/your/dir/or/file Where "path/..." in (3) starts at the directory just below the repo root containing your ".../file" NOTE that instead of "HEAD", the hash code of a specific commit may be used, and then you will get the revision (file) or revisions (dir) specific to that commit.


Just realized I misunderstood the question. My reply instead addresses how to checkout a single file, which can then be modified and committed.
You addressed the title of the question, which is good enough. This just saved me, so as far as I'm concerned, this is the correct answer - especially since this was a top hit on Google for this issue.
This still gets the whole repo though. For a project 2GB in size, this doesn't save much time..
For those that read these comments and don't know how this doesn't answer the question, what this solution does is give you a copy of only that one folder, but it's not a working copy, meaning you can't modify and re-commit. So as @Morgon said, it addresses the title of the question (you CAN pull only one dir), but doesn't answer the body of the question ("make some changes, commit and push back again").
@StanHolodnak -- is not required though. It forces the bits after it to be interpreted as a path. So, yes, if you have a file with the name --help that you want to checkout, prefixing it with these hyphens would be required: git checkout HEAD -- --help
r
rolve

In an empty directory:

git init
git remote add [REMOTE_NAME] [GIT_URL]
git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- path/to/directory

this works if you only want to use remote version of a file to replace local file, i think
this pull the folder also, how to not donwload the folder, but only things inside the folder?
There isn't any need to fetch or checkout commands, we can simply make the pull request after remote add command to get the whole master branch.
As Pithkos points out in the accepted answer, this answer also will download the WHOLE remote repository to your local machine. shingara's answer is the most clear - there is no way to remotely pull a portion of a repo. However, for those who don't mind the bandwidth of the WHOLE fetch, and are interested in making a working copy of a portion of the local cache, then the accepted answer shows you how. But I just want to make it clear the the entire history of the repo (in a compressed format) must be downloaded to your machine.
Note that this solution will lose your commit history
C
Community

After much looking for an answer, not finding, giving up, trying again and so on, I finally found a solution to this in another SO thread:

How to git-pull all but one folder

To copy-paste what's there:

git init
git remote add -f origin <url>
git config core.sparsecheckout true
echo <dir1>/ >> .git/info/sparse-checkout
echo <dir2>/ >> .git/info/sparse-checkout
echo <dir3>/ >> .git/info/sparse-checkout
git pull origin master

To do what OP wants (work on only one dir), just add that one dir to .git/info/sparse-checkout, when doing the steps above.

Many many thanks to @cforbish !


I'm posting it here mostly because whenever I try to google how to do that, this is a top result, and that thread is not even close to being displayed. :( I feel bad copy-pasting, but I reckon it's SO best practices?
This is the only one clean solution. Tried the answers above. After I checked out my branch with the other answers, I couldn't pull, and had several new files that needed committing. With this solution everything looks right. But NOTE: Regardless of the branch that was checked out, git will think you are in master. Just make sure you do git branch --set-upstream-to=origin/<branch> to pull correctly
f
feralamillo

If you want to get the latest changes in a directory without entering it, you can do:

$ git -C <Path to directory> pull

K
Kheldar

Maybe this command can be helpful :

git archive --remote=MyRemoteGitRepo --format=tar BranchName_or_commit  path/to/your/dir/or/file > files.tar

"Et voilà"


This downloads a specific directory of file for archiving purposes, this doesn't pull a copy you can modify and push back.
Very cool. Probably not acceptable as solution to the OP but it's actually what I was after since I just need to search the contents of a big folder in an even bigger repo I definitely don't want to fetch, and I don't need to modify/push.
As @leetNightshade said, this is not the right way to answer this.
s
shingara

It's not possible. You need pull all repository or nothing.


Even though you need to fetch everything, would a pull on a sparse checkout working tree be of interest in this case? See stackoverflow.com/questions/2416815/…
Just to add on this, the reason why you cannot pull just a directory is because git uses data semantic tracking, not file semantic tracking, so you can seamlessly move code (or other data) in and out of files without having to tell the source tracking system (until you update of course.) Because of this, code can move in and out of directories seamlessly too, so grabbing just one directory doesn't make as much sense. Hope this helps.
@OmnipotentEntity Sorry but IMHO it does make sense. The implementation shouldn't limit the use. Git does know what directories are (at least when creating a local working copy) so there's no reason it couldn't perform some similar operation on the server and just send the relevant output. Granted, that's not currently implemented but that's due to a lack of a feature, not a fundamental impossibility. Personally I think it's a pretty important feature but hey, that's one reason I avoid git when possible.
C
Ciro Santilli Путлер Капут 六四事

git clone --filter from git 2.19 now works on GitHub (tested 2020-09-18, git 2.25.1)

I'm not sure about pull/fetch, but at least for the initial clone, this option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.

E.g., to clone only objects required for d1 of this repository: https://github.com/cirosantilli/test-git-partial-clone I can do:

git clone \
  --depth 1 \
  --filter=blob:none \
  --no-checkout \
  https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git checkout master -- d1

I have covered this in more detail at: Git: How do I clone a subdirectory only of a Git repository?

It is very likely that whatever gets implemented for git clone in that area will also have git pull analogues, but I couldn't find it very easily yet.


N
Nnaik

Tried and tested this works !

mkdir <directory name> ;  //Same directory name as the one you want to pull
cd <directory name>;
git remote add origin <GIT_URL>;
git checkout -b '<branch name>';
git config core.sparsecheckout true;
echo <directory name>/ >> .git/info/sparse-checkout;
git pull origin <pull branch name>

Hope this was helpful!


D
D M

For all that struggle with theoretical file paths and examples like I did, here a real world example: Microsoft offers their docs and examples on git hub, unfortunately they do gather all their example files for a large amount of topics in this repository:

https://github.com/microsoftarchive/msdn-code-gallery-community-s-z

I only was interested in the Microsoft Dynamics js files in the path

msdn-code-gallery-community-s-z/Sdk.Soap.js/

so I did the following

create a

msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout

file in my repositories folder on the disk

git sparse-checkout init

in that directory using cmd on windows

The file contents of

msdn-code-gallery-community-s-zSdkSoapjs\.git\info\sparse-checkout

is

Sdk.Soap.js/*

finally do a

git pull origin master

A
Abizern

Sometimes, you just want to have a look at previous copies of files without the rigmarole of going through the diffs.

In such a case, it's just as easy to make a clone of a repository and checkout the specific commit that you are interested in and have a look at the subdirectory in that cloned repository. Because everything is local you can just delete this clone when you are done.