38

I'm rather new to git, and am trying to set up my repository the right way.

Basically my app is a platform of sorts, so implementations of this platform are based on the master branch, but have some small modifications to those files as well as some additional files.

I tried setting it up as branches, so I have a master branch, implementation_1 and implementation_2.

But as far as I can tell, that would mean that locally all the branches are stored in one directory, with their separation being only through git.

What I would like is to have 3 local directories, master,imp_1, and imp_2. If I make a change to one of the core files in the imp_1 directory, I want to be able to merge that change into the master branch and from there into imp_2.

I'm beginning to think these need to be 3 different repositories (the implementations being forks of the core). Is that the way to go? In that case, how would I go about handling the above scenario?

Iter Ator
  • 8,226
  • 20
  • 73
  • 164
kbanman
  • 4,223
  • 6
  • 32
  • 40
  • 1
    `git-new-workdir` might do the trick (See http://thejspr.com/blog/work-on-multiple-branches-with-git-workdir/) – user456584 Jun 05 '14 at 21:48

3 Answers3

31

Branches are first class citizens in Git. They are not "emulated" as branches like in older VCS such SVN, CVS, etc.

If you really need three different directories, because you want to have three distinct development environment, make 3 clones:

  • one in a directory called master
  • one in a directory called imp_1
  • one in a directory called imp_2

But to really understand branches, you can read "Pros and cons of different branching models in DVCS".


Or, since Git 2.5 (July 2015, 4 years after the OP's question), as I detailed in Multiple working directories with Git?, use git worktree.

That will be one clone, multiple folders (one per branch).
With Git 2.7+, you can then list those folders:

$ git worktree list
/path/to/bare-source            (bare)
/path/to/linked-worktree        abcd1234 [master]
/path/to/other-linked-worktree  1234abc  (detached HEAD)
SpaceMonkey55
  • 438
  • 4
  • 14
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Are there any pros and cons for 3 different directories vs. keeping 3 branches in the same local repo ? – lysergic-acid Sep 23 '12 at 16:22
  • 5
    @lysergic-acid the main pro is that you can work on each branch in parallel at the same time, since each branch is cloned in its own directory. – VonC Sep 23 '12 at 16:42
  • Yes, but is there any problem with working with a single dir? for example, say i make an experimental branch to test some stuff out, and have some files uncommitted in the work tree, can i freely checkout another branch to continue the main line of work? (maybe this is not the best example, but i am trying to see if some things may limit use of a single dir). – lysergic-acid Sep 24 '12 at 04:11
  • 1
    @lysergic-acid everybody usually works in a single directory. Git won't touch untracked files when changing branch. Or you can stash them: http://stackoverflow.com/questions/835501/git-how-do-you-stash-an-untracked-file – VonC Sep 24 '12 at 05:43
6

You can do this using git worktree that was introduced in Git 2.5, circa July 2015.

git clone -b master <repo> master
cd master

# checking out implementation_1 in ../imp_1
git worktree add ../imp_1 implementation_1

# creating branch implementation_2 at master~2 as you check it out in ../imp2:
git worktree add -b implementation_2 ../imp_2 master~2

And voilà! You're done.

With this setup, you'll have only one .git folder (in master/.git). Note that any branch can be checked out in a single worktree at a time.

Cimbali
  • 11,012
  • 1
  • 39
  • 68
6

you can create three clones of your repository and merge between those. forks in git are no different from branches the way they are merged and such. you can easily do:

git merge ../master
git pull ../imp_2

when cloning locally git will hardlink your objects and even save disk space

knittl
  • 246,190
  • 53
  • 318
  • 364