7

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?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
fredy
  • 575
  • 2
  • 9
  • 20
  • Your question isn't very clear. Let's say you have folders `dev1`, `dev2`, and `production`. Are you asking how to bring in all the changes from `production` from another branch, or are you asking how to essentially set the contents of `production` to one of the dev folders? Either way, the way you're approaching this sounds like your working against the tool. – John Szakmeister Aug 19 '13 at 23:54
  • 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 – fredy Aug 21 '13 at 13:46
  • Are you using Git to manage a working copy? Or are you really just looking to get the contents of a folder in the repository onto your machine? Judging by your comment to PherricOxide, it sounds like the latter. – John Szakmeister Aug 23 '13 at 00:02

3 Answers3

6

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.

Community
  • 1
  • 1
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
  • I have a local repository which is up to date. Now both developers did changes, each in his folder. i want to "grab" only the changes from one folder Can i do it? – fredy Aug 25 '13 at 21:33
  • If it's up-to-date, don't you already have his changes then? What have you tried? You should update your question with that information. – John Szakmeister Aug 27 '13 at 08:18
  • lets say i pulled everything to my PC. then the two folders were updated. today i want to get only the changes from one folder. can i do it? – fredy Aug 28 '13 at 12:51
  • I feel like you're saying the same thing over and over--and I don't mean that to be offensive, it's just that your responses are adding much clarity to the problem. What are you trying to accomplish--beyond just trying to get changes in a single folder? *Why* do you only want the changes from one folder? What problem are you trying to solve? It it because you're unhappy with the changes in the other folder and what you really want is to revert them? Why aren't you and your developers working on branches instead of in folders if you want more control over what goes into production? – John Szakmeister Aug 28 '13 at 13:03
  • Thanks for not giving up..... i have a stable installation on my computer. i want to test only the changes of one developer - meaning getting the changes of only one folder, not both of them. How do i update my environment with only one folder (doesn't matter the use case. can i pull from git only the changes of one folder?) – fredy Aug 29 '13 at 14:19
1

Do you have some sort of production branch? Assuming a dev and production branch...

# Get all the changes from the dev branch
git checkout dev
git pull origin dev

# Checkout the production branch
git checkout production

# Checkout the dev version of the folder you want
git checkout dev myfolder

# Commit all the changes that are now showing up.

This is sort of a terrible way to do things, because you're losing the change history when you checkout and recommit just that folder.

PherricOxide
  • 15,493
  • 3
  • 28
  • 41
  • Thanks but maybe i wasn't very clear... i want to get only the updates of one folder. i do not want to do any checkout. Lets say both developer made some commits to the same branch but i want to get only changes of one folder – fredy Aug 19 '13 at 23:22
  • Your 3rd checkout command is incorrect. It should be `git checkout dev -- myfolder` (the `dev` and `myfolder` are swapped). – John Szakmeister Aug 19 '13 at 23:47
0

Yes, it's easy to git fetch only one git sub-directory. Suppose you have the following project directory in your remote git-repo.

project/  
  ├── code  
  └── data  

For the moment assume that you have cloned the project to your local filesystem as a git repository.

$ cd project
$ rm -rf code 

#now we try to fetch the "code" directory.
$ git fetch 
$ git checkout HEAD code

In case you have nested directory structure like,

project/
├── code
│   ├── dev
│   └── test
└── data

Then in this case you should use the specific directory that you want,

$ git checkout HEAD code/dev
kmario23
  • 57,311
  • 13
  • 161
  • 150