How to pull specific directory with git

不打扰是莪最后的温柔 提交于 2019-11-26 06:13:32

问题


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?


回答1:


  1. cd into the top of your repo copy
  2. git fetch
  3. 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.




回答2:


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



回答3:


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 !




回答4:


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




回答5:


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à"




回答6:


git clone --filter from Git 2.19

This option will actually skip fetching unneeded objects from the server.

I have written a detailed answer explaining current support status at: 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.




回答7:


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.




回答8:


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

$ git -C <Path to directory> pull


来源:https://stackoverflow.com/questions/2425059/how-to-pull-specific-directory-with-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!