2

I have 4 clones of same repository of our project on my system.

The reason for this is that we have evolved in terms of technology stack in past 2 years and each of the 4 versions of this evolution requires a different folder structure & eclipse workspace setup. Each version exists on its own branch. To keep supporting old versions for customers I need to have different clones to ease my work so that I don't have to switch branches and change setup accordingly.

I mostly work offline. So, whenever I connect to network, I execute a script that runs git fetch on each of the clones one-by-one. What I want is to save some network bandwidth in this process. I want to run git fetch only once on say prime repository and rest of the repositories must download the code from prime.

I tried setting prime as remote to other repositories but it only fetches the locally checked out branches of prime in the secondary repositories. Is there at all a way to what I want to achieve?

Mukund Jalan
  • 1,145
  • 20
  • 39
  • 1
    Are you using the same branches in each of those repositories, or are those different setups basically permanently set up for different branches? – poke Jul 31 '17 at 12:08
  • @poke I have added my branchwise use-case to the description. – Mukund Jalan Jul 31 '17 at 12:36

2 Answers2

5

If each repository has the sole purpose of being there so you have a working directory for one specific (and fixed) branch, then a much better workflow would be to use a single repository with multiple working trees.

So you would only have one repository in which you would have to fetch. Instead of the default one working directory, you would then have a total of four working directories, all using the same repository as its base.

Luckily, Git supports this scenario quite well out of the box with the git worktree command: Assuming you are inside your “primary” local repository, you can just call git worktree add three times to create three more working directories with a specific branch:

git worktree add ../legacy-1 legacy-branch-1
git worktree add ../legacy-2 legacy-branch-2
git worktree add ../legacy-3 legacy-branch-3

So each of those folders (including the primary one) will be a full and independent working directory with a separate index and everything. It’s just that the underlying repository will be the same and everything will be shared including the remote and its branches. So you can fetch in any of the repositories and all will update (because it’s just a single repository).

There are a few things you need to know when working with multiple working trees. The most important one is that you cannot check out branches that are checked out in worktrees. So each folder should stay fixed to the branch it was initialized for—but I believe that already fits your workflow pretty well anyway.

poke
  • 369,085
  • 72
  • 557
  • 602
  • thanks for highlighting this great git feature. that's what probably I was looking for. It seems perfect for my use case. I am not sure how exactly it would fit in but I will have to try this out and check, which may be I'll do in a couple of days and get back to you again if required. – Mukund Jalan Aug 01 '17 at 05:47
  • For future readers - https://stackoverflow.com/questions/31935776/what-would-i-use-git-worktree-for – Mukund Jalan Aug 01 '17 at 05:47
0

You're already doing things right (I use the same setup with long lived worktrees to avoid switching branches).

To update the currently checked out branch do

git pull prime $BRANCH

To update the other branches run

git fetch prime $B1:$B1 $B2:$B2…
phd
  • 82,685
  • 13
  • 120
  • 165
  • What problem I find here is that `prime` repository must have `$BRANCH` (or `$B1` or `$B2`) locally checked-out at least once with the branch(s) being updated. I cannot directly fetch code from `origin` of `prime` to other repositories, which is what I want. – Mukund Jalan Aug 01 '17 at 09:41
  • My experience is different. I can update `prime` using `git pull origin master` (if `master` is the checked out branch in `prime`) and `git fetch origin $B1:$B1` and then update other worktrees from `prime`. What's your problem fetching/pulling from `prime`? – phd Aug 01 '17 at 09:48
  • I am not yet using worktrees. I wasn't aware of it until I read @poke's answer. I am using multiple clones for which this approach doesn't work. – Mukund Jalan Aug 02 '17 at 04:43
  • I also don't use `git worktree` — my git is not fresh enough. I also use separate clones and the approach works for me fine. In what way it doesn't work for you? – phd Aug 02 '17 at 08:19
  • I get the exception `fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository fatal: The remote end hung up unexpectedly` – Mukund Jalan Aug 04 '17 at 08:48
  • Of course. For the currently checked out branch you do `git pull prime $BRANCH`, for all other branches `git fetch prime $B1:$B1 $B2:$B2…` – phd Aug 04 '17 at 08:50