Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git fetch only one directory

Tags:

git

pull

fetch

I have a developer that works on one folder and another that works on another. I would like to update the production with a specific folder I'm looking for a command like:

cd /myproject
git pull myfolder

and expect that only myfolder will be updated

Is it possible?

ok, i'll rephrase... I have one project, one branch and two developers and two folders in the project. each developer works on one folder. both make changes and push them into the branch.

I want to pull only changes of one folder into my local machine

Basic/Simple isn't it?

like image 460
fredy Avatar asked Aug 19 '13 21:08

fredy


1 Answers

Assuming what you're really asking is to remotely grab files from a git repository, without having a working tree of your own (or a clone of the repository locally), you could do this:

git archive --format=tgz --remote=<url-to-repo> master -- myfolder | tar zxvf -

This says to create a gzipped tar ball of the master branch of the repository living at <url-to-repo>, but include only the contents under myfolder. Piping that through tar will unarchive the data and put it in the current working directory.

If you're actually trying to manage a working tree and work with the other two developers, then what you're asking to do really goes against the grain of the tool. But you'd have to be much more elaborate about what you're trying to accomplish in order for someone to make a good workflow suggestion.

Update:

So it does sound as if you want to fetch just one folder's changes in a managed working tree. Sorry, but Git does not operate that way. As I said in the comments below, Git manages trees of files. It does not allow for part of a tree to be at one revision, and another part to be at a different revision like you're suggesting--perhaps you are a previous user of Subversion where this was possible?

You can get an effect that is similar to what you want, but you have to modify your working copy. I would not commit the modification unless you want to discard one of your developers work. You could to this:

$ git fetch
$ git checkout @{u} -- myfolder

git fetch will bring your remote references up to date--it will fetch the latest information from the servers. git checkout @{u} -- myfolder will make myfolder look like what exists in the tracking branch. This has two consequences:

  • You do not have all the changes on your local branch (and you didn't want them, so that's okay), and

  • myfolder will show as being modified because the content of myfolder has been updated with the latest available from the remote repository, but it is not the same as your current revision.

To get your working copy back to normal, do this:

$ git checkout -- myfolder

From there, you call follow your usual mechanism for getting your branch up-to-date with the remote (likely git pull).

This, however, is probably not how you want to operate. Despite your comment, "doesn't matter the use case", it really does matter. There are often better ways to accomplish your overall goal and manage your development, but it requires understanding what you're trying to do. At the moment, what you propose really goes against how Git is intended to work.

So let me put a few things out there for you. Research using topic branches as a way to manage the development work being done, rather than using folders for each developer in the tree. Clear, logical changes can be made on branches and the merged as you test and approve them for inclusion into production.

A good place to start is here. It contains a fair amount of information itself, and some follow on links to several different styles of Git workflows. The place where you want to end is where there is a commit that represents the version you actually put into production, without destroying the work that others are doing for you. Ideally, you'd tag what was pushed into production in some meaningful way.

I know no one likes to hear it, but training may be very helpful in your case. GitHub offers some free online classes as well as several other resources. Judging from experience, once you grasp how Git works and the problems it solves, you'll end up with a much better workflow.

like image 78
John Szakmeister Avatar answered Sep 21 '22 16:09

John Szakmeister